vue.config.js 4.3 KB

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