vue.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. config.plugins.push(
  59. new UglifyJsPlugin({
  60. uglifyOptions: {
  61. //生产环境自动删除console
  62. compress: {
  63. drop_debugger: true,
  64. drop_console: true,
  65. pure_funcs: ['console.log']
  66. }
  67. },
  68. sourceMap: false,
  69. parallel: true
  70. })
  71. )
  72. config.optimization.splitChunks = {
  73. cacheGroups: {
  74. vendors: {
  75. chunks: 'all',
  76. name: "chunk-vendors",
  77. test: /[\\/]node_modules[\\/]/,
  78. chunks: "initial",
  79. priority: 2,
  80. reuseExistingChunk: true,
  81. enforce: true
  82. },
  83. elementUI: {
  84. chunks: 'all',
  85. name: "stabled-elementui",
  86. test: /[\\/]node_modules[\\/]element-ui[\\/]/,
  87. priority: 3,
  88. reuseExistingChunk: true,
  89. enforce: true
  90. },
  91. echarts: {
  92. chunks: 'all',
  93. name: "stabled-echarts",
  94. test: /[\\/]node_modules[\\/]echarts[\\/]/,
  95. priority: 4,
  96. reuseExistingChunk: true,
  97. enforce: true
  98. },
  99. vue: {
  100. name: 'stabled-vue',
  101. test: /[\\/]node_modules[\\/]vue[\\/]/,
  102. chunks: "all",
  103. priority: 5,
  104. reuseExistingChunk: true,
  105. enforce: true
  106. },
  107. vuex: {
  108. name: 'stabled-vue',
  109. test: /[\\/]node_modules[\\/]vuex[\\/]/,
  110. chunks: "all",
  111. priority: 6,
  112. reuseExistingChunk: true,
  113. enforce: true
  114. },
  115. 'vue-router': {
  116. name: 'stabled-vue',
  117. test: /[\\/]node_modules[\\/]vue-router[\\/]/,
  118. chunks: "all",
  119. priority: 7,
  120. reuseExistingChunk: true,
  121. enforce: true
  122. },
  123. zrender: {
  124. name: "stabled-zrender",
  125. test: /[\\/]node_modules[\\/]zrender[\\/]/,
  126. chunks: "all",
  127. priority: 8,
  128. reuseExistingChunk: true,
  129. enforce: true
  130. }
  131. }
  132. }
  133. },
  134. }