vue.config.js 4.0 KB

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