vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: ['**/*.{vue,htm,html,css,sss,less,scss,sass}'],
  68. fix: false, // 是否自动修复
  69. cache: true, // 是否缓存
  70. emitErrors: true,
  71. failOnError: false
  72. }),
  73. new CompressionPlugin({
  74. algorithm: 'gzip', // 使用gzip压缩
  75. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  76. minRatio: 0.8, // 压缩率小于0.8才会压缩
  77. threshold: 10240, // 对超过10k的数据压缩
  78. deleteOriginalAssets: false // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  79. })
  80. ]
  81. },
  82. chainWebpack(config) {
  83. // 启用预加载,提高首屏加载速度
  84. config.plugin('preload').tap(() => [
  85. {
  86. rel: 'preload',
  87. // 忽略 runtime.js
  88. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  89. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  90. include: 'initial'
  91. }
  92. ]);
  93. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  94. // config.plugins.delete('prefetch');
  95. // 设置 svg-sprite-loader
  96. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  97. config.module
  98. .rule('icons')
  99. .test(/\.svg$/)
  100. .include.add(resolve('src/icons'))
  101. .end()
  102. .use('svg-sprite-loader')
  103. .loader('svg-sprite-loader')
  104. .options({
  105. symbolId: 'icon-[name]'
  106. })
  107. .end();
  108. config.when(NODE_ENV !== 'development', () => {
  109. config
  110. .plugin('ScriptExtHtmlWebpackPlugin')
  111. .after('html')
  112. .use('script-ext-html-webpack-plugin', [
  113. {
  114. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  115. inline: /runtime\..*\.js$/
  116. }
  117. ])
  118. .end();
  119. config.optimization.splitChunks({
  120. chunks: 'all',
  121. cacheGroups: {
  122. libs: {
  123. name: 'chunk-libs',
  124. test: /[\\/]node_modules[\\/]/,
  125. priority: 10,
  126. chunks: 'initial' // 仅打包最初依赖的第三方
  127. },
  128. elementUI: {
  129. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  130. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  131. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  132. },
  133. commons: {
  134. name: 'chunk-commons',
  135. test: resolve('src/components'), // 可以自定义规则
  136. minChunks: 3, // 最小公用数
  137. priority: 5,
  138. reuseExistingChunk: true
  139. }
  140. }
  141. });
  142. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  143. config.optimization.runtimeChunk('single');
  144. });
  145. }
  146. };