vue.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const path = require('path')
  2. function resolve(dir) {
  3. return path.join(__dirname, dir)
  4. }
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  6. const DEV = process.env.NODE_ENV == 'production';
  7. module.exports = {
  8. // 选项...
  9. publicPath: './',
  10. productionSourceMap: false,
  11. css: {
  12. extract: false,
  13. sourceMap: true,
  14. loaderOptions: {},
  15. },
  16. lintOnSave: false,
  17. devServer: {
  18. overlay: {
  19. warning: false,
  20. errors: false
  21. },
  22. disableHostCheck: true,
  23. },
  24. chainWebpack: (config) => {
  25. // 移除 prefetch 插件
  26. config.plugins.delete('prefetch')
  27. // 移除 preload 插件
  28. config.plugins.delete('preload');
  29. // ============压缩图片 start============
  30. config.module
  31. .rule('images')
  32. .use('image-webpack-loader')
  33. .loader('image-webpack-loader')
  34. .options({bypassOnDebug: true})
  35. .end()
  36. // ============压缩图片 end============
  37. //配置 svg-sprite-loader
  38. // 第一步:让其他svg loader不要对src/icons进行操作
  39. config.module
  40. .rule('svg')
  41. .exclude.add(resolve('src/icons/svg')) //注意:路径要具体到存放的svg的路径下,不然会报错
  42. .end()
  43. // 第二步:使用svg-sprite-loader 对 src/icons下的svg进行操作
  44. config.module
  45. .rule('icons')
  46. .test(/\.svg$/)
  47. .include.add(resolve('src/icons/svg')) //注意:路径要具体到存放的svg的路径下,不然会报错
  48. .end()
  49. .use('svg-sprite-loader')
  50. .loader('svg-sprite-loader')
  51. //定义规则 使用时 <svg class="icon"> <use xlink:href="#icon-svg文件名"></use> </svg>
  52. .options({
  53. symbolId: 'icon-[name]'
  54. })
  55. .end()
  56. },
  57. configureWebpack: config => {
  58. if(DEV){
  59. config.plugins.push(
  60. new UglifyJsPlugin({
  61. uglifyOptions: {
  62. //生产环境自动删除console
  63. compress: {
  64. drop_debugger: true,
  65. drop_console: true,
  66. pure_funcs: ['console.log']
  67. }
  68. },
  69. sourceMap: false,
  70. parallel: true
  71. })
  72. )
  73. }
  74. config.optimization.splitChunks = {
  75. cacheGroups: {
  76. vendors: {
  77. chunks: 'all',
  78. name: "chunk-vendors",
  79. test: /[\\/]node_modules[\\/]/,
  80. chunks: "initial",
  81. priority: 2,
  82. reuseExistingChunk: true,
  83. enforce: true
  84. },
  85. elementUI: {
  86. chunks: 'all',
  87. name: "stabled-elementui",
  88. test: /[\\/]node_modules[\\/]element-ui[\\/]/,
  89. priority: 3,
  90. reuseExistingChunk: true,
  91. enforce: true
  92. },
  93. echarts: {
  94. chunks: 'all',
  95. name: "stabled-echarts",
  96. test: /[\\/]node_modules[\\/]echarts[\\/]/,
  97. priority: 4,
  98. reuseExistingChunk: true,
  99. enforce: true
  100. },
  101. vue: {
  102. name: 'stabled-vue',
  103. test: /[\\/]node_modules[\\/]vue[\\/]/,
  104. chunks: "all",
  105. priority: 5,
  106. reuseExistingChunk: true,
  107. enforce: true
  108. },
  109. vuex: {
  110. name: 'stabled-vue',
  111. test: /[\\/]node_modules[\\/]vuex[\\/]/,
  112. chunks: "all",
  113. priority: 6,
  114. reuseExistingChunk: true,
  115. enforce: true
  116. },
  117. 'vue-router': {
  118. name: 'stabled-vue',
  119. test: /[\\/]node_modules[\\/]vue-router[\\/]/,
  120. chunks: "all",
  121. priority: 7,
  122. reuseExistingChunk: true,
  123. enforce: true
  124. },
  125. zrender: {
  126. name: "stabled-zrender",
  127. test: /[\\/]node_modules[\\/]zrender[\\/]/,
  128. chunks: "all",
  129. priority: 8,
  130. reuseExistingChunk: true,
  131. enforce: true
  132. }
  133. }
  134. }
  135. },
  136. }