vue.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const GitRevisionPlugin = require('git-revision-webpack-plugin')
  4. const GitRevision = new GitRevisionPlugin()
  5. const buildDate = JSON.stringify(new Date().toLocaleString())
  6. const createThemeColorReplacerPlugin = require('./config/plugin.config')
  7. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  8. const productionGzipExtensions = ['js', 'css']
  9. function resolve(dir) {
  10. return path.join(__dirname, dir)
  11. }
  12. // check Git
  13. function getGitHash() {
  14. try {
  15. return GitRevision.version()
  16. } catch (e) { }
  17. return 'unknown'
  18. }
  19. const isProd = process.env.NODE_ENV === 'production'
  20. const assetsCDN = {
  21. // webpack build externals
  22. externals: {
  23. vue: 'Vue',
  24. 'vue-router': 'VueRouter',
  25. vuex: 'Vuex',
  26. axios: 'axios',
  27. qq: 'qq',
  28. TMap: 'TMap'
  29. },
  30. css: [],
  31. // https://unpkg.com/browse/vue@2.6.10/
  32. js: [
  33. '//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
  34. '//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
  35. '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
  36. '//cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js'
  37. ]
  38. }
  39. // vue.config.js
  40. const vueConfig = {
  41. configureWebpack: {
  42. // webpack plugins
  43. plugins: [
  44. // Ignore all locale files of moment.js
  45. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  46. new webpack.DefinePlugin({
  47. APP_VERSION: `"${require('./package.json').version}"`,
  48. GIT_HASH: JSON.stringify(getGitHash()),
  49. BUILD_DATE: buildDate
  50. }),
  51. // 配置compression-webpack-plugin压缩
  52. new CompressionWebpackPlugin({
  53. algorithm: 'gzip',
  54. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  55. threshold: 10240,
  56. minRatio: 0.8
  57. })
  58. ],
  59. // if prod, add externals
  60. externals: isProd ? assetsCDN.externals
  61. : {
  62. qq: 'qq',
  63. TMap: 'TMap'
  64. }
  65. },
  66. chainWebpack: (config) => {
  67. config.resolve.alias
  68. .set('@$', resolve('src'))
  69. .set('@assets', resolve('src/assets'))
  70. .set('@views', resolve('src/views'))
  71. const svgRule = config.module.rule('svg')
  72. svgRule.uses.clear()
  73. svgRule
  74. .oneOf('inline')
  75. .resourceQuery(/inline/)
  76. .use('vue-svg-icon-loader')
  77. .loader('vue-svg-icon-loader')
  78. .end()
  79. .end()
  80. .oneOf('external')
  81. .use('file-loader')
  82. .loader('file-loader')
  83. .options({
  84. name: 'assets/[name].[hash:8].[ext]'
  85. })
  86. // if prod is on
  87. // assets require on cdn
  88. if (isProd) {
  89. config.plugin('html').tap(args => {
  90. args[0].cdn = assetsCDN
  91. return args
  92. })
  93. }
  94. },
  95. css: {
  96. loaderOptions: {
  97. less: {
  98. modifyVars: {
  99. // less vars,customize ant design theme
  100. 'primary-color': '#2f54eb'
  101. // 'link-color': '#F5222D',
  102. // 'border-radius-base': '2px'
  103. },
  104. // DO NOT REMOVE THIS LINE
  105. javascriptEnabled: true
  106. }
  107. }
  108. },
  109. devServer: {
  110. // development server port 8000
  111. port: 8078,
  112. // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  113. proxy: {
  114. // detail: https://cli.vuejs.org/config/#devserver-proxy
  115. [process.env.VUE_APP_BASE_API]: {
  116. // target: `https://aidex.setworld.net`,
  117. target: `http://127.0.0.1:18080`,
  118. changeOrigin: true,
  119. pathRewrite: {
  120. ['^' + process.env.VUE_APP_BASE_API]: ''
  121. }
  122. }
  123. },
  124. disableHostCheck: true // 增加该设置是为了解决使用外网映射工具映射时报错--可以删除
  125. },
  126. // disable source map in production
  127. productionSourceMap: false,
  128. lintOnSave: undefined,
  129. // babel-loader no-ignore node_modules/*
  130. transpileDependencies: []
  131. }
  132. // preview.pro.loacg.com only do not use in your production;
  133. if (process.env.VUE_APP_PREVIEW === 'true') {
  134. console.log('VUE_APP_PREVIEW', true)
  135. // add `ThemeColorReplacer` plugin to webpack plugins
  136. vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  137. }
  138. module.exports = vueConfig