vue.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. let path = require("path");
  2. const webpack = require("webpack");
  3. const ThemeColorReplacer = require("webpack-theme-color-replacer");
  4. const { getThemeColors, modifyVars } = require("./src/utils/themeUtil");
  5. const { resolveCss } = require("./src/utils/theme-color-replacer-extend");
  6. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  7. const productionGzipExtensions = ["js", "css"];
  8. const isProd = process.env.NODE_ENV === "production";
  9. const assetsCDN = {
  10. // webpack build externals
  11. externals: {},
  12. css: [],
  13. js: [],
  14. };
  15. module.exports = {
  16. devServer: {
  17. open: true,
  18. proxy: {
  19. "/api": {
  20. target: process.env.VUE_APP_API_BASE_URL,
  21. changeOrigin: true,
  22. pathRewrite: {
  23. "^/api": "",
  24. },
  25. },
  26. },
  27. },
  28. pluginOptions: {
  29. "style-resources-loader": {
  30. preProcessor: "less",
  31. patterns: [path.resolve(__dirname, "./src/theme/theme.less")],
  32. },
  33. },
  34. configureWebpack: config => {
  35. config.entry.app = ["babel-polyfill", "whatwg-fetch", "./src/main.js"];
  36. config.performance = {
  37. hints: false,
  38. };
  39. config.plugins.push(
  40. new ThemeColorReplacer({
  41. fileName: "css/theme-colors-[contenthash:8].css",
  42. matchColors: getThemeColors(),
  43. injectCss: true,
  44. resolveCss,
  45. })
  46. );
  47. // Ignore all locale files of moment.js
  48. config.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
  49. // 生产环境下将资源压缩成gzip格式
  50. if (isProd) {
  51. // add `CompressionWebpack` plugin to webpack plugins
  52. config.plugins.push(
  53. new CompressionWebpackPlugin({
  54. algorithm: "gzip",
  55. test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
  56. threshold: 10240,
  57. minRatio: 0.8,
  58. })
  59. );
  60. }
  61. // if prod, add externals
  62. if (isProd) {
  63. config.externals = assetsCDN.externals;
  64. }
  65. },
  66. chainWebpack: config => {
  67. // 生产环境下关闭css压缩的 colormin 项,因为此项优化与主题色替换功能冲突
  68. if (isProd) {
  69. config.plugin("optimize-css").tap(args => {
  70. args[0].cssnanoOptions.preset[1].colormin = false;
  71. return args;
  72. });
  73. }
  74. // 生产环境下使用CDN
  75. if (isProd) {
  76. config.plugin("html").tap(args => {
  77. args[0].cdn = assetsCDN;
  78. return args;
  79. });
  80. }
  81. },
  82. css: {
  83. loaderOptions: {
  84. less: {
  85. lessOptions: {
  86. modifyVars: modifyVars(),
  87. javascriptEnabled: true,
  88. },
  89. },
  90. },
  91. },
  92. publicPath: process.env.VUE_APP_PUBLIC_PATH,
  93. outputDir: "dist",
  94. assetsDir: "static",
  95. productionSourceMap: false,
  96. };