vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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: 8081,
  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. 'vue-router': 'VueRouter',
  24. },
  25. resolve: {
  26. alias: {
  27. '@': resolve('src')
  28. }
  29. }
  30. },
  31. chainWebpack(config) {
  32. if (isProduction) {
  33. // ============压缩图片 start============
  34. // config.module
  35. // .rule('images')
  36. // .use('image-webpack-loader')
  37. // .loader('image-webpack-loader')
  38. // .options({ bypassOnDebug: true })
  39. // .end();
  40. // ============压缩图片 end============
  41. // 移除 prefetch 插件
  42. config.plugins.delete('prefetch')
  43. // 移除 preload 插件
  44. config.plugins.delete('preload');
  45. config.plugin('provide').use(webpack.ProvidePlugin, [{
  46. 'window.Quill': 'quill'
  47. }])
  48. // 生产环境,开启js\css压缩
  49. config.plugin('compressionPlugin').use(new CompressionWebpackPlugin({
  50. test: /\.(js|css|less|map)$/, // 匹配文件名
  51. threshold: 1024, // 对超过10k的数据压缩
  52. minRatio: 0.8,
  53. }))
  54. config
  55. .when(isProduction,
  56. config => {
  57. config
  58. .plugin('ScriptExtHtmlWebpackPlugin')
  59. .after('html')
  60. .use('script-ext-html-webpack-plugin', [{
  61. // `runtime` must same as runtimeChunk name. default is `runtime`
  62. inline: /runtime\..*\.js$/
  63. }])
  64. .end()
  65. config
  66. .optimization.splitChunks({
  67. chunks: 'all',
  68. minSize: 1000000,
  69. maxSize: 3000000,
  70. cacheGroups: {
  71. libs: {
  72. name: 'chunk-libs',
  73. test: /[\\/]node_modules[\\/]/,
  74. priority: 10,
  75. chunks: 'initial' // only package third parties that are initially dependent
  76. },
  77. elementUI: {
  78. name: 'chunk-elementUI', // split elementUI into a single package
  79. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  80. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  81. },
  82. commons: {
  83. name: 'chunk-commons',
  84. test: resolve('src/components'), // can customize your rules
  85. minChunks: 3, // minimum common number
  86. priority: 5,
  87. reuseExistingChunk: true
  88. }
  89. }
  90. })
  91. config.optimization.runtimeChunk('single'),
  92. config.optimization.providedExports = true,
  93. config.optimization.usedExports = true,
  94. config.optimization.innerGraph = true,
  95. config.optimization.sideEffects = true,
  96. {
  97. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  98. to: './' //到根目录下
  99. }
  100. }
  101. )
  102. }
  103. },
  104. }