vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. vue: 'Vue',
  13. 'vue-router': 'VueRouter',
  14. vuex: 'Vuex',
  15. axios: 'axios',
  16. nprogress: 'NProgress',
  17. clipboard: 'ClipboardJS',
  18. '@antv/data-set': 'DataSet',
  19. 'js-cookie': 'Cookies'
  20. },
  21. css: [
  22. ],
  23. js: [
  24. '//cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
  25. '//cdn.jsdelivr.net/npm/vue-router@3.3.4/dist/vue-router.min.js',
  26. '//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
  27. '//cdn.jsdelivr.net/npm/axios@0.19.2/dist/axios.min.js',
  28. '//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js',
  29. '//cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js',
  30. '//cdn.jsdelivr.net/npm/@antv/data-set@0.11.4/build/data-set.min.js',
  31. '//cdn.jsdelivr.net/npm/js-cookie@2.2.1/src/js.cookie.min.js'
  32. ]
  33. }
  34. module.exports = {
  35. devServer: {
  36. //port:8080,
  37. proxy: {
  38. '/api': { //此处要与 /services/api.js 中的 API_PROXY_PREFIX 值保持一致
  39. target: process.env.VUE_APP_API_BASE_URL,
  40. //ws: true,//代理websocket
  41. changeOrigin: true,
  42. pathRewrite: {
  43. '^/api': ''
  44. }
  45. }
  46. }
  47. },
  48. pluginOptions: {
  49. 'style-resources-loader': {
  50. preProcessor: 'less',
  51. patterns: [path.resolve(__dirname, "./src/theme/theme.less")],
  52. }
  53. },
  54. configureWebpack: config => {
  55. config.entry.app = ["babel-polyfill", "whatwg-fetch", "./src/main.js"];
  56. config.performance = {
  57. hints: false
  58. }
  59. config.plugins.push(
  60. new ThemeColorReplacer({
  61. fileName: 'css/theme-colors-[contenthash:8].css',
  62. matchColors: getThemeColors(),
  63. injectCss: true,
  64. resolveCss
  65. })
  66. )
  67. // Ignore all locale files of moment.js
  68. config.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/))
  69. // 生产环境下将资源压缩成gzip格式
  70. if (isProd) {
  71. // add `CompressionWebpack` plugin to webpack plugins
  72. config.plugins.push(new CompressionWebpackPlugin({
  73. algorithm: 'gzip',
  74. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  75. threshold: 10240,
  76. minRatio: 0.8
  77. }))
  78. }
  79. // if prod, add externals
  80. if (isProd) {
  81. config.externals = assetsCDN.externals
  82. }
  83. },
  84. chainWebpack: config => {
  85. // 生产环境下关闭css压缩的 colormin 项,因为此项优化与主题色替换功能冲突
  86. if (isProd) {
  87. config.plugin('optimize-css')
  88. .tap(args => {
  89. args[0].cssnanoOptions.preset[1].colormin = false
  90. return args
  91. })
  92. }
  93. // 生产环境下使用CDN
  94. if (isProd) {
  95. config.plugin('html')
  96. .tap(args => {
  97. args[0].cdn = assetsCDN
  98. return args
  99. })
  100. }
  101. },
  102. css: {
  103. loaderOptions: {
  104. less: {
  105. lessOptions: {
  106. modifyVars: modifyVars(),
  107. javascriptEnabled: true
  108. }
  109. }
  110. }
  111. },
  112. publicPath: process.env.VUE_APP_PUBLIC_PATH,
  113. outputDir: 'dist',
  114. assetsDir: 'static',
  115. productionSourceMap: false
  116. }