vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict'
  2. const webpack = require('webpack');
  3. const path = require('path')
  4. const resolve = dir => path.resolve(__dirname, dir)
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin');
  6. const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
  7. const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
  8. // 打包分析
  9. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
  10. module.exports = {
  11. transpileDependencies: ['webpack-dev-server/client'],
  12. // 部署应用时的基本 URL
  13. publicPath: '/',
  14. // 输出文件目录
  15. outputDir: 'dist',
  16. // eslint-loader 是否在保存的时候检查
  17. lintOnSave: false,
  18. assetsDir: '', // 相对于outputDir的静态资源(js、css、img、fonts)目录
  19. //是否使用包含运行时编译器的 Vue 构建版本
  20. runtimeCompiler: true,
  21. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建
  22. productionSourceMap: false,
  23. chainWebpack: config => {
  24. config
  25. .entry(['./src/main'])
  26. .add('babel-polyfill');
  27. config.resolve.symlinks(true); // 修复热更新失效
  28. // 添加别名
  29. config.resolve.alias
  30. .set('@', resolve('src'))
  31. // .set('assets', resolve('src/assets'))
  32. // .set('components', resolve('src/components'));
  33. // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
  34. config.plugin('html').tap(args => {
  35. //修复 Lazy loading routes Error
  36. args[0].chunksSortMode = 'none';
  37. return args;
  38. })
  39. //压缩图片
  40. // config.module
  41. // .rule("images")
  42. // .use("image-webpack-loader")
  43. // .loader("image-webpack-loader")
  44. // .options({
  45. // bypassOnDebug: true
  46. // })
  47. // .end()
  48. // 打包分析, 打包之后自动生成一个名叫report.html文件(可忽视) 查看打包数据使用
  49. if (IS_PROD) {
  50. config.plugin("webpack-report").use(BundleAnalyzerPlugin, [{
  51. analyzerMode: "static"
  52. }]);
  53. }
  54. },
  55. css: {
  56. // extract: true, //解决css热更新失败问题
  57. sourceMap: false,
  58. modules: false,
  59. loaderOptions: {
  60. less: {
  61. javascriptEnabled: true,
  62. }
  63. }
  64. },
  65. //需要npm install style-resources-loader ; 然后执行 vue add style-resources-loader 选择预编译less
  66. pluginOptions: {
  67. 'style-resources-loader': {
  68. preProcessor: 'less',
  69. patterns: ['src/assets/common/css/theme.less']
  70. }
  71. },
  72. devServer: { //跨域
  73. port: "8002", //端口号
  74. open: true, //配置自动启动浏览器
  75. hot: true,
  76. proxy: { // 配置跨域处理 可以设置多个
  77. '/simulation/oauth': {
  78. target: 'http://60.205.245.110:7001', // online-new
  79. changeOrigin: true,
  80. secure: false, // 如果是https接口,需要配置这个参数
  81. pathRewrite: {
  82. '^/simulation/oauth': '/simulation/oauth'
  83. }
  84. },
  85. '/simulation/resource/common': {
  86. target: 'http://60.205.245.110:8001', // online-new
  87. changeOrigin: true,
  88. secure: false, // 如果是https接口,需要配置这个参数
  89. pathRewrite: {
  90. '^/simulation/resource/common': '/simulation/resource/common'
  91. }
  92. },
  93. '/simulation/resource/server': {
  94. target: 'http://60.205.245.110:8005', // online-new
  95. changeOrigin: true,
  96. secure: false, // 如果是https接口,需要配置这个参数
  97. pathRewrite: {
  98. '^/simulation/resource/server': '/simulation/resource/server'
  99. }
  100. },
  101. '/simulation/resource/video': {
  102. target: 'http://60.205.245.110:8007', // online-new
  103. changeOrigin: true,
  104. secure: false, // 如果是https接口,需要配置这个参数
  105. pathRewrite: {
  106. '^/simulation/resource/server': '/simulation/resource/server'
  107. }
  108. },
  109. }
  110. },
  111. configureWebpack: config => {
  112. const plugins = [];
  113. plugins.push(
  114. new webpack.ProvidePlugin({
  115. $: "jquery",
  116. jQuery: "jquery",
  117. "windows.jQuery": "jquery",
  118. 'window.Quill': 'quill/dist/quill.js',
  119. 'Quill': 'quill/dist/quill.js'
  120. })
  121. )
  122. if (IS_PROD) {
  123. //开启 gzip 压缩
  124. plugins.push(
  125. new CompressionWebpackPlugin({
  126. filename: '[path].gz[query]',
  127. algorithm: 'gzip',
  128. test: productionGzipExtensions,
  129. threshold: 10240,
  130. minRatio: 0.8
  131. })
  132. );
  133. }
  134. config.plugins = [
  135. ...config.plugins,
  136. ...plugins
  137. ];
  138. },
  139. }