vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const path = require('path');
  2. const StyleLintPlugin = require('stylelint-webpack-plugin');
  3. const CompressionPlugin = require('compression-webpack-plugin');
  4. const defaultSettings = require('./src/settings.js');
  5. function resolve(dir) {
  6. return path.join(__dirname, './', dir);
  7. }
  8. const name = defaultSettings.title;
  9. const NODE_ENV = process.env.NODE_ENV;
  10. const port = process.env.port || process.env.npm_config_port || 7878;
  11. let proxy = {};
  12. if (NODE_ENV === 'development') {
  13. proxy = {
  14. [process.env.VUE_APP_BASE_API]: {
  15. target: 'http://gcls.helxsoft.cn/',
  16. changeOrigin: true,
  17. pathRewrite: {
  18. ['^' + process.env.VUE_APP_BASE_API]: ''
  19. }
  20. },
  21. [process.env.VUE_APP_PDF]: {
  22. target: 'https://file-kf.helxsoft.cn/',
  23. changeOrigin: true,
  24. pathRewrite: {
  25. ['^' + process.env.VUE_APP_PDF]: ''
  26. }
  27. }
  28. };
  29. }
  30. // 配置项说明 https://cli.vuejs.org/config/
  31. module.exports = {
  32. publicPath: NODE_ENV === 'development' ? '/' : './',
  33. outputDir: 'dist',
  34. assetsDir: 'static',
  35. lintOnSave: false,
  36. runtimeCompiler: true,
  37. productionSourceMap: false,
  38. devServer: {
  39. port,
  40. open: true, // 默认false true自动打开网页
  41. overlay: {
  42. warnings: false,
  43. errors: true
  44. },
  45. proxy
  46. },
  47. css: {
  48. loaderOptions: {
  49. scss: {
  50. // 为 scss 配置共享全局变量
  51. additionalData: '@import "./src/styles/variables.scss";'
  52. }
  53. }
  54. },
  55. configureWebpack: {
  56. name,
  57. // 配置路径别名
  58. resolve: {
  59. alias: {
  60. '@': resolve('src')
  61. }
  62. },
  63. devtool: NODE_ENV === 'development' ? 'source-map' : '',
  64. plugins: [
  65. // stylelint 配置
  66. new StyleLintPlugin({
  67. files: ['src/**/*.{vue,html,css,scss}'],
  68. fix: true, // 是否自动修复
  69. failOnError: false
  70. }),
  71. new CompressionPlugin({
  72. algorithm: 'gzip', // 使用gzip压缩
  73. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  74. minRatio: 0.8, // 压缩率小于0.8才会压缩
  75. threshold: 10240, // 对超过10k的数据压缩
  76. deleteOriginalAssets: false // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  77. })
  78. ]
  79. },
  80. chainWebpack(config) {
  81. // 启用预加载,提高首屏加载速度
  82. config.plugin('preload').tap(() => [
  83. {
  84. rel: 'preload',
  85. // 忽略 runtime.js
  86. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  87. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  88. include: 'initial'
  89. }
  90. ]);
  91. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  92. // config.plugins.delete('prefetch');
  93. // 设置 svg-sprite-loader
  94. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  95. config.module
  96. .rule('icons')
  97. .test(/\.svg$/)
  98. .include.add(resolve('src/icons'))
  99. .end()
  100. .use('svg-sprite-loader')
  101. .loader('svg-sprite-loader')
  102. .options({
  103. symbolId: 'icon-[name]'
  104. })
  105. .end();
  106. config.when(NODE_ENV !== 'development', () => {
  107. config
  108. .plugin('ScriptExtHtmlWebpackPlugin')
  109. .after('html')
  110. .use('script-ext-html-webpack-plugin', [
  111. {
  112. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  113. inline: /runtime\..*\.js$/
  114. }
  115. ])
  116. .end();
  117. config.optimization.splitChunks({
  118. chunks: 'all',
  119. cacheGroups: {
  120. libs: {
  121. name: 'chunk-libs',
  122. test: /[\\/]node_modules[\\/]/,
  123. priority: 10,
  124. chunks: 'initial' // 仅打包最初依赖的第三方
  125. },
  126. elementUI: {
  127. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  128. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  129. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  130. },
  131. commons: {
  132. name: 'chunk-commons',
  133. test: resolve('src/components'), // 可以自定义规则
  134. minChunks: 3, // 最小公用数
  135. priority: 5,
  136. reuseExistingChunk: true
  137. }
  138. }
  139. });
  140. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  141. config.optimization.runtimeChunk('single');
  142. });
  143. }
  144. };