vue.config.js 3.3 KB

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