選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

vue.config.js 4.4 KiB

1年前
1年前
1年前
1年前
1年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. function resolve(dir) {
  10. return path.join(__dirname, dir);
  11. }
  12. const assetsCDN = {
  13. // webpack build externals
  14. externals: {
  15. vue: 'Vue',
  16. 'vue-router': 'VueRouter',
  17. vuex: 'Vuex',
  18. axios: 'axios',
  19. nprogress: 'NProgress',
  20. clipboard: 'ClipboardJS',
  21. '@antv/data-set': 'DataSet',
  22. 'js-cookie': 'Cookies',
  23. },
  24. css: [],
  25. js: [
  26. '//cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
  27. '//cdn.jsdelivr.net/npm/vue-router@3.3.4/dist/vue-router.min.js',
  28. '//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
  29. '//cdn.jsdelivr.net/npm/axios@0.19.2/dist/axios.min.js',
  30. '//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js',
  31. '//cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js',
  32. '//cdn.jsdelivr.net/npm/@antv/data-set@0.11.4/build/data-set.min.js',
  33. '//cdn.jsdelivr.net/npm/js-cookie@2.2.1/src/js.cookie.min.js',
  34. ],
  35. };
  36. // 为匹配符合命名规则的API_*_BASE_URL环境变量动态生成proxy规则
  37. const proxyConfig = Object.entries(process.env)
  38. .filter(([key]) => key.match(/^VUE_APP_API_(.+_)?BASE_URL$/))
  39. .reduce((obj, [_, target]) => ({
  40. ...obj,
  41. [`${process.env.VUE_APP_API_DEV_SERVER_PROXY_BASE}/${target}`]: {
  42. target: target,
  43. changeOrigin: true,
  44. pathRewrite: {
  45. '^/proxy/': ''
  46. },
  47. logLevel: 'debug',
  48. }
  49. }), {});
  50. module.exports = {
  51. devServer: {
  52. port: 8082, // 端口号
  53. proxy: {
  54. ...proxyConfig,
  55. '/ids': {
  56. target: 'http://localhost:44339',
  57. changeOrigin: true,
  58. pathRewrite: {
  59. '^/ids': ''
  60. },
  61. logLevel: 'debug',
  62. },
  63. '/api': {
  64. target: 'http://localhost:44354',
  65. changeOrigin: true,
  66. pathRewrite: {},
  67. logLevel: 'debug',
  68. }
  69. }
  70. },
  71. pluginOptions: {
  72. 'style-resources-loader': {
  73. preProcessor: 'less',
  74. patterns: [path.resolve(__dirname, './src/theme/theme.less')],
  75. },
  76. },
  77. configureWebpack: (config) => {
  78. config.entry.app = ['babel-polyfill', 'whatwg-fetch', './src/main.js'];
  79. config.performance = {
  80. hints: false,
  81. };
  82. config.plugins.push(
  83. new ThemeColorReplacer({
  84. fileName: 'css/theme-colors-[contenthash:8].css',
  85. matchColors: getThemeColors(),
  86. injectCss: true,
  87. resolveCss,
  88. })
  89. );
  90. // Ignore all locale files of moment.js
  91. config.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
  92. // 生产环境下将资源压缩成gzip格式
  93. if (isProd) {
  94. // add `CompressionWebpack` plugin to webpack plugins
  95. config.plugins.push(
  96. new CompressionWebpackPlugin({
  97. algorithm: 'gzip',
  98. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  99. threshold: 10240,
  100. minRatio: 0.8,
  101. })
  102. );
  103. }
  104. // if prod, add externals
  105. // if (isProd) {
  106. // config.externals = assetsCDN.externals
  107. // }
  108. },
  109. chainWebpack: (config) => {
  110. config.resolve.alias
  111. .set('@', resolve('src'))
  112. .set('static', resolve('public/static'))
  113. .set('assets', resolve('src/assets'))
  114. .set('components', resolve('src/components'))
  115. .set('router', resolve('src/router'))
  116. .set('store', resolve('src/store'))
  117. .set('views', resolve('src/views'));
  118. // 生产环境下关闭css压缩的 colormin 项,因为此项优化与主题色替换功能冲突
  119. if (isProd) {
  120. config.plugin('optimize-css').tap((args) => {
  121. args[0].cssnanoOptions.preset[1].colormin = false;
  122. return args;
  123. });
  124. }
  125. // 生产环境下使用CDN
  126. // if (isProd) {
  127. // config.plugin('html')
  128. // .tap(args => {
  129. // args[0].cdn = assetsCDN
  130. // return args
  131. // })
  132. // }
  133. },
  134. css: {
  135. loaderOptions: {
  136. less: {
  137. lessOptions: {
  138. modifyVars: modifyVars(),
  139. javascriptEnabled: true,
  140. },
  141. },
  142. },
  143. },
  144. publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  145. outputDir: 'dist',
  146. assetsDir: 'static',
  147. productionSourceMap: false,
  148. };