vue.config.js 4.5 KB

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