vue.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. const path = require("path");
  3. // 代码压缩
  4. const TerserPlugin = require('terser-webpack-plugin')
  5. // gzip压缩
  6. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  7. // 是否为生产环境
  8. const isProduction = process.env.NODE_ENV !== 'development'
  9. function resolve (dir) {
  10. return path.join(__dirname, dir);
  11. }
  12. console.log(process.env)
  13. module.exports = {
  14. productionSourceMap: false,
  15. lintOnSave: false,
  16. configureWebpack: {
  17. output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】
  18. filename: `[name].${new Date().getTime()}.js`,
  19. chunkFilename: `[name].${new Date().getTime()}.js`
  20. }
  21. },
  22. chainWebpack: config => {
  23. // 使用svg-sprite-loader加载icon图标
  24. config.module
  25. .rule("svg")
  26. .exclude.add(resolve("src/icons"))
  27. .end();
  28. config.module
  29. .rule("icons")
  30. .test(/\.svg$/)
  31. .include.add(resolve("src/icons"))
  32. .end()
  33. .use("svg-sprite-loader")
  34. .loader("svg-sprite-loader")
  35. .options({
  36. symbolId: "icon-[name]"
  37. })
  38. .end();
  39. // 图片压缩
  40. if (isProduction) {
  41. // 删除预加载
  42. config.plugins.delete('preload');
  43. config.plugins.delete('prefetch');
  44. // 压缩代码
  45. config.optimization.minimize(true);
  46. // 分割代码
  47. config.optimization.splitChunks({
  48. chunks: 'all'
  49. })
  50. }
  51. },
  52. configureWebpack: config => {
  53. // 生产环境相关配置
  54. if (isProduction) {
  55. // 代码压缩
  56. config.plugins.push(
  57. new TerserPlugin({
  58. terserOptions: {
  59. //生产环境自动删除console
  60. compress: {
  61. warnings: false, // 若打包错误,则注释这行
  62. drop_debugger: true,
  63. drop_console: true,
  64. pure_funcs: ['console.log']
  65. }
  66. },
  67. sourceMap: false,
  68. parallel: true
  69. })
  70. )
  71. // gzip压缩
  72. const productionGzipExtensions = ['html', 'js', 'css']
  73. config.plugins.push(
  74. new CompressionWebpackPlugin({
  75. filename: '[path].gz[query]',
  76. algorithm: 'gzip',
  77. test: new RegExp(
  78. '\\.(' + productionGzipExtensions.join('|') + ')$'
  79. ),
  80. threshold: 10240, // 只有大小大于该值的资源会被处理 10240
  81. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  82. deleteOriginalAssets: false // 删除原文件
  83. })
  84. )
  85. // 公共代码抽离
  86. config.optimization = {
  87. splitChunks: {
  88. cacheGroups: {
  89. vendor: {
  90. chunks: 'all',
  91. test: /node_modules/,
  92. name: 'vendor',
  93. minChunks: 1,
  94. maxInitialRequests: 5,
  95. minSize: 0,
  96. priority: 100
  97. },
  98. common: {
  99. chunks: 'all',
  100. test: /[\\/]src[\\/]js[\\/]/,
  101. name: 'common',
  102. minChunks: 2,
  103. maxInitialRequests: 5,
  104. minSize: 0,
  105. priority: 60
  106. },
  107. styles: {
  108. name: 'styles',
  109. test: /\.(sa|sc|c)ss$/,
  110. chunks: 'all',
  111. enforce: true
  112. },
  113. runtimeChunk: {
  114. name: 'manifest'
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. };