vue.config.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const HardSourceWebpackPlugin = require("hard-source-webpack-plugin");
  2. const isProduction = process.env.NODE_ENV === "production";
  3. // 代理列表
  4. const PROXY_LIST = {
  5. "/dev": {
  6. target: "http://127.0.0.1:8001",
  7. changeOrigin: true,
  8. pathRewrite: {
  9. "^/dev": ""
  10. }
  11. },
  12. "/pro": {
  13. target: "https://show.cool-admin.com",
  14. changeOrigin: true,
  15. pathRewrite: {
  16. "^/pro": "/api"
  17. }
  18. }
  19. };
  20. module.exports = {
  21. publicPath: "/",
  22. lintOnSave: true,
  23. productionSourceMap: false,
  24. parallel: require("os").cpus().length > 1,
  25. css: {
  26. extract: isProduction,
  27. sourceMap: false,
  28. loaderOptions: {
  29. sass: {
  30. prependData: `@import "@/assets/css/common.scss";`
  31. }
  32. }
  33. },
  34. devServer: {
  35. port: 9000,
  36. open: false,
  37. compress: false,
  38. overlay: {
  39. warnings: false,
  40. errors: true
  41. },
  42. disableHostCheck: true,
  43. proxy: PROXY_LIST
  44. },
  45. chainWebpack: config => {
  46. // 设置环境变量
  47. config.plugin("define").tap(args => {
  48. args[0]["process.env"].PROXY_LIST = JSON.stringify(PROXY_LIST);
  49. return args;
  50. });
  51. // 设置 svg
  52. config.module.rule("svg").uses.clear();
  53. config.module
  54. .rule("svg-sprite-loader")
  55. .test(/.svg$/)
  56. .exclude.add(/node_modules/)
  57. .end()
  58. .use("svg-sprite-loader")
  59. .loader("svg-sprite-loader")
  60. .options({
  61. symbolId: "[name]"
  62. });
  63. // 生产模式下
  64. if (isProduction) {
  65. // 去掉元素之间空格
  66. config.module
  67. .rule("vue")
  68. .use("vue-loader")
  69. .loader("vue-loader")
  70. .tap(options => {
  71. options.compilerOptions.preserveWhitespace = true;
  72. return options;
  73. })
  74. .end();
  75. // 移除 prefetch 插件
  76. config.plugins.delete("prefetch-index");
  77. // 移除 preload 插件,避免加载多余的资源
  78. config.plugins.delete("preload-index");
  79. config.optimization.minimizer("terser").tap(args => {
  80. // 去掉注释
  81. args[0].terserOptions.output = {
  82. comments: false
  83. };
  84. return args;
  85. });
  86. // 分割模块
  87. config.optimization.splitChunks({
  88. chunks: "all",
  89. maxInitialRequests: Infinity,
  90. minSize: 300000,
  91. automaticNameDelimiter: "-",
  92. cacheGroups: {
  93. vendor: {
  94. test: /[\\/]node_modules[\\/]/,
  95. name(module) {
  96. const packageName = module.context.match(
  97. /[\\/]node_modules[\\/](.*?)([\\/]|$)/
  98. )[1];
  99. return `chunk.${packageName.replace("@", "")}`;
  100. },
  101. priority: 10
  102. }
  103. }
  104. });
  105. }
  106. },
  107. configureWebpack: config => {
  108. // 构建缓存
  109. config.plugins.push(new HardSourceWebpackPlugin());
  110. }
  111. };