vue.config.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict'
  2. const path = require("path");
  3. const webpack = require('webpack')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir);
  7. }
  8. // 是否为生产环境
  9. const isProduction = process.env.ENV !== 'development'
  10. module.exports = {
  11. lintOnSave: false,//禁用代码检测
  12. productionSourceMap: false,
  13. configureWebpack: {
  14. name: '广州市建设工程消防协会',
  15. // 生产环境,则添加不参与打包的包名和依赖包的名称
  16. externals: {
  17. 'vue': 'Vue',
  18. 'axios': 'axios',
  19. 'element-ui': 'ELEMENT',
  20. 'vant': 'Vant',
  21. 'vue-router': 'VueRouter',
  22. },
  23. resolve: {
  24. alias: {
  25. '@': resolve('src')
  26. }
  27. }
  28. },
  29. chainWebpack(config) {
  30. if (isProduction) {
  31. // 移除 prefetch 插件
  32. config.plugins.delete('prefetch')
  33. // 移除 preload 插件
  34. config.plugins.delete('preload');
  35. config.plugin('provide').use(webpack.ProvidePlugin, [{
  36. 'window.Quill': 'quill'
  37. }])
  38. // 生产环境,开启js\css压缩
  39. config.plugin('compressionPlugin').use(new CompressionWebpackPlugin({
  40. test: /\.(js|css|less|map)$/, // 匹配文件名
  41. threshold: 1024, // 对超过10k的数据压缩
  42. minRatio: 0.8,
  43. }))
  44. config
  45. .when(isProduction,
  46. config => {
  47. config
  48. .plugin('ScriptExtHtmlWebpackPlugin')
  49. .after('html')
  50. .use('script-ext-html-webpack-plugin', [{
  51. // `runtime` must same as runtimeChunk name. default is `runtime`
  52. inline: /runtime\..*\.js$/
  53. }])
  54. .end()
  55. config
  56. .optimization.splitChunks({
  57. chunks: 'all',
  58. minSize: 1000000,
  59. maxSize: 3000000,
  60. cacheGroups: {
  61. libs: {
  62. name: 'chunk-libs',
  63. test: /[\\/]node_modules[\\/]/,
  64. priority: 10,
  65. chunks: 'initial' // only package third parties that are initially dependent
  66. },
  67. elementUI: {
  68. name: 'chunk-elementUI', // split elementUI into a single package
  69. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  70. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  71. },
  72. commons: {
  73. name: 'chunk-commons',
  74. test: resolve('src/components'), // can customize your rules
  75. minChunks: 3, // minimum common number
  76. priority: 5,
  77. reuseExistingChunk: true
  78. }
  79. }
  80. })
  81. config.optimization.runtimeChunk('single'),
  82. config.optimization.providedExports = true,
  83. config.optimization.usedExports = true,
  84. config.optimization.innerGraph = true,
  85. config.optimization.sideEffects = true,
  86. {
  87. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  88. to: './' //到根目录下
  89. }
  90. }
  91. )
  92. }
  93. },
  94. }