vue.config.js 4.9 KB

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