Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

154 linhas
4.5 KiB

  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://115.236.37.105:1136',
  57. target: 'https://dfmserver.jiepei.com:44336/ids',
  58. changeOrigin: true,
  59. pathRewrite: {
  60. '^/ids': ''
  61. },
  62. logLevel: 'debug',
  63. },
  64. '/api': {
  65. target: 'https://dfmserver.jiepei.com:44336',
  66. changeOrigin: true,
  67. pathRewrite: {},
  68. logLevel: 'debug',
  69. }
  70. }
  71. },
  72. pluginOptions: {
  73. 'style-resources-loader': {
  74. preProcessor: 'less',
  75. patterns: [path.resolve(__dirname, './src/theme/theme.less')],
  76. },
  77. },
  78. configureWebpack: (config) => {
  79. config.entry.app = ['babel-polyfill', 'whatwg-fetch', './src/main.js'];
  80. config.performance = {
  81. hints: false,
  82. };
  83. config.plugins.push(
  84. new ThemeColorReplacer({
  85. fileName: 'css/theme-colors-[contenthash:8].css',
  86. matchColors: getThemeColors(),
  87. injectCss: true,
  88. resolveCss,
  89. })
  90. );
  91. // Ignore all locale files of moment.js
  92. config.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
  93. // 生产环境下将资源压缩成gzip格式
  94. if (isProd) {
  95. // add `CompressionWebpack` plugin to webpack plugins
  96. config.plugins.push(
  97. new CompressionWebpackPlugin({
  98. algorithm: 'gzip',
  99. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  100. threshold: 10240,
  101. minRatio: 0.8,
  102. })
  103. );
  104. }
  105. // if prod, add externals
  106. // if (isProd) {
  107. // config.externals = assetsCDN.externals
  108. // }
  109. },
  110. chainWebpack: (config) => {
  111. config.resolve.alias
  112. .set('@', resolve('src'))
  113. .set('static', resolve('public/static'))
  114. .set('assets', resolve('src/assets'))
  115. .set('components', resolve('src/components'))
  116. .set('router', resolve('src/router'))
  117. .set('store', resolve('src/store'))
  118. .set('views', resolve('src/views'));
  119. // 生产环境下关闭css压缩的 colormin 项,因为此项优化与主题色替换功能冲突
  120. if (isProd) {
  121. config.plugin('optimize-css').tap((args) => {
  122. args[0].cssnanoOptions.preset[1].colormin = false;
  123. return args;
  124. });
  125. }
  126. // 生产环境下使用CDN
  127. // if (isProd) {
  128. // config.plugin('html')
  129. // .tap(args => {
  130. // args[0].cdn = assetsCDN
  131. // return args
  132. // })
  133. // }
  134. },
  135. css: {
  136. loaderOptions: {
  137. less: {
  138. lessOptions: {
  139. modifyVars: modifyVars(),
  140. javascriptEnabled: true,
  141. },
  142. },
  143. },
  144. },
  145. publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  146. outputDir: 'dist',
  147. assetsDir: 'static',
  148. productionSourceMap: false,
  149. };