vue.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 配置文件地址
  3. * https://cli.vuejs.org/zh/config/#vue-config-js
  4. */
  5. const IS_PROD = ["production", "prod"].includes(process.env.NODE_ENV);
  6. const path = require('path');
  7. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
  8. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  9. const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
  10. module.exports = {
  11. /**
  12. * 部署应用包时基本的URL,与webpack中的output.publicPath一致
  13. * 默认情况下是部署到一个域名的根路径上,
  14. * 也可以设置成'',或者相对路径'./';这样所有的资源都会被链接相对路径,就可以部署到任意路径
  15. */
  16. publicPath: './',
  17. /**
  18. * 生产环境打包生成的文件夹,构建之前会清除构建目录,与webpack中的output.path一致
  19. */
  20. outputDir: 'dist',
  21. /**
  22. * 放置生成的静态文件
  23. */
  24. assetsDir: 'assets',
  25. /**
  26. * 默认情况下生成的静态文件包含hash值更好的控制缓存,html必须是动态生成,如果不是动态生成可以置为false
  27. * 与webpack中的output.filename中的名字加hash一样
  28. */
  29. filenameHashing: true,
  30. /**
  31. * 如果不需要生产环境的source map,可以设置为false加快构建
  32. */
  33. productionSourceMap: false,
  34. /**
  35. * 所有webpack-dev-server的选项都支持
  36. * https://webpack.docschina.org/configuration/dev-server/#devserveroverlay
  37. */
  38. devServer: {
  39. port: '8897',
  40. open: true,
  41. progress: true,
  42. overlay: {
  43. warnings: false,
  44. errors: true
  45. }
  46. },
  47. // webpack配置
  48. chainWebpack: config => {
  49. // 是否将符号链接(symlink)解析到它们的符号链接位置
  50. config.resolve.symlinks(true)
  51. if (IS_PROD) {
  52. config.plugin("webpack-report").use(BundleAnalyzerPlugin, [
  53. {
  54. analyzerMode: "static"
  55. }
  56. ]);
  57. config.optimization.delete("splitChunks");
  58. }
  59. },
  60. configureWebpack: config => {
  61. if (process.env.VUE_APP_MODE === 'production') {
  62. // 生产环境
  63. config.mode = 'production'
  64. } else {
  65. // 开发环境
  66. config.mode = 'development'
  67. }
  68. Object.assign(config, {
  69. resolve: {
  70. alias: {
  71. '@': path.resolve(__dirname, './src')
  72. }
  73. }
  74. })
  75. const plugins = []
  76. if (IS_PROD) {
  77. config.optimization ={
  78. splitChunks: {
  79. cacheGroups: {
  80. common: {
  81. name: "chunk-common",
  82. chunks: "all",
  83. minChunks: 2,
  84. maxInitialRequests: 5,
  85. minSize: 0,
  86. priority: 1,
  87. reuseExistingChunk: true,
  88. enforce: true
  89. },
  90. vendors: {
  91. name: "chunk-vendors",
  92. test: /[\\/]node_modules[\\/]/,
  93. chunks: "initial",
  94. priority: 2,
  95. reuseExistingChunk: true,
  96. enforce: true
  97. },
  98. antDesignVueUI: {
  99. name: "chunk-antdesignvueUI",
  100. test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
  101. chunks: "all",
  102. priority: 3,
  103. reuseExistingChunk: true,
  104. enforce: true
  105. },
  106. echarts: {
  107. name: "chunk-echarts",
  108. test: /[\\/]node_modules[\\/](vue-)?echarts[\\/]/,
  109. chunks: "all",
  110. priority: 4,
  111. reuseExistingChunk: true,
  112. enforce: true
  113. }
  114. }
  115. }
  116. }
  117. plugins.push(
  118. new CompressionWebpackPlugin({
  119. filename: "[path].gz[query]",
  120. algorithm: "gzip",
  121. test: productionGzipExtensions,
  122. threshold: 10240,
  123. minRatio: 0.8
  124. })
  125. )
  126. }
  127. config.plugins = [...config.plugins, ...plugins];
  128. }
  129. }