vue.config.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // svg
  47. config.module.rule("svg").uses.clear();
  48. config.module
  49. .rule("svg-sprite-loader")
  50. .test(/.svg$/)
  51. .exclude.add(/node_modules/)
  52. .end()
  53. .use("svg-sprite-loader")
  54. .loader("svg-sprite-loader")
  55. .options({
  56. symbolId: "[name]"
  57. });
  58. // 去掉元素之间空格
  59. config.module
  60. .rule("vue")
  61. .use("vue-loader")
  62. .loader("vue-loader")
  63. .tap(options => {
  64. options.compilerOptions.preserveWhitespace = true;
  65. return options;
  66. })
  67. .end();
  68. // 移除 prefetch 插件
  69. config.plugins.delete("prefetch-index");
  70. // 移除 preload 插件,避免加载多余的资源
  71. config.plugins.delete("preload-index");
  72. // 设置环境变量
  73. config.plugin('define').tap(args => {
  74. args[0]['process.env'].PROXY_LIST = JSON.stringify(PROXY_LIST)
  75. return args
  76. })
  77. // 生产模式下
  78. if (isProduction) {
  79. config.performance.set("hints", false);
  80. config.optimization.splitChunks({
  81. chunks: "all",
  82. cacheGroups: {
  83. // 公用模块抽离
  84. common: {
  85. chunks: "initial",
  86. minSize: 0,
  87. minChunks: 2
  88. },
  89. // 第三方库抽离
  90. vendor: {
  91. priority: 1,
  92. test: /node_modules/,
  93. chunks: "initial",
  94. minSize: 0,
  95. minChunks: 2
  96. }
  97. }
  98. });
  99. }
  100. },
  101. configureWebpack: config => {
  102. // config.plugins.push(new HardSourceWebpackPlugin());
  103. }
  104. };