vue.config.js 3.9 KB

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