vue.config.js 4.2 KB

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