vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const packageJson = require('./package.json')
  4. const GitRevisionPlugin = require('git-revision-webpack-plugin')
  5. const GitRevision = new GitRevisionPlugin()
  6. const buildDate = JSON.stringify(new Date().toLocaleString())
  7. const createThemeColorReplacerPlugin = require('./config/plugin.config')
  8. const Timestamp = new Date().getTime();
  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. // eslint-disable-next-line no-unused-vars
  20. const isProd = process.env.NODE_ENV === 'production'
  21. // eslint-disable-next-line no-unused-vars
  22. const assetsCDN = {
  23. // webpack build externals
  24. externals: {
  25. vue: 'Vue',
  26. 'vue-router': 'VueRouter',
  27. vuex: 'Vuex',
  28. axios: 'axios',
  29. },
  30. css: [],
  31. // https://unpkg.com/browse/vue@2.6.10/
  32. js: [
  33. '//cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js',
  34. '//cdn.jsdelivr.net/npm/vue-router@3.5.1/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.21.1/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({
  46. contextRegExp: /^\.\/locale$/,
  47. resourceRegExp: /moment$/,
  48. }),
  49. new webpack.DefinePlugin({
  50. APP_VERSION: `"${packageJson.version}"`,
  51. GIT_HASH: JSON.stringify(getGitHash()),
  52. BUILD_DATE: buildDate,
  53. }),
  54. ],
  55. cache: {
  56. type: 'filesystem', // 使用文件缓存
  57. },
  58. // en_US: `if prod, add externals`
  59. // zh_CN: `这里是用来控制编译忽略外部依赖的,与 config.plugin('html') 配合可以编译时引入外部CDN文件依赖`
  60. // externals: isProd ? assetsCDN.externals : {}
  61. },
  62. chainWebpack: (config) => {
  63. config.resolve.alias.set('@$', resolve('src'))
  64. config.output.filename(`static/js/[name].[hash].${Timestamp}.js`).end();
  65. config.output.chunkFilename('static/js/[name].[hash].' + Timestamp + '.js').end()
  66. // fixed svg-loader by https://github.com/damianstasik/vue-svg-loader/issues/185#issuecomment-1126721069
  67. const svgRule = config.module.rule('svg')
  68. // Remove regular svg config from root rules list
  69. config.module.rules.delete('svg')
  70. config.module
  71. .rule('svg')
  72. // Use svg component rule
  73. .oneOf('svg_as_component')
  74. .resourceQuery(/inline/)
  75. .test(/\.(svg)(\?.*)?$/)
  76. .use('babel-loader')
  77. .loader('babel-loader?cacheDirectory')
  78. .end()
  79. .use('vue-svg-loader')
  80. .loader('vue-svg-loader?cacheDirectory')
  81. .options({
  82. svgo: {
  83. plugins: [
  84. { prefixIds: true },
  85. { cleanupIDs: true },
  86. { convertShapeToPath: false },
  87. { convertStyleToAttrs: true },
  88. ],
  89. },
  90. })
  91. .end()
  92. .end()
  93. // Otherwise use original svg rule
  94. .oneOf('svg_as_regular')
  95. .merge(svgRule.toConfig())
  96. .end()
  97. // en_US: If prod is on assets require on cdn
  98. // zh_CN: 如果是 prod 模式,则引入 CDN 依赖文件,有需要减少包大小请自行解除依赖
  99. //
  100. // if (isProd) {
  101. // config.plugin('html').tap(args => {
  102. // args[0].cdn = assetsCDN
  103. // return args
  104. // })
  105. // }
  106. },
  107. css: {
  108. loaderOptions: {
  109. less: {
  110. modifyVars: {
  111. // less vars,customize ant design theme
  112. // 'primary-color': '#F5222D',
  113. // 'link-color': '#F5222D',
  114. 'border-radius-base': '2px',
  115. },
  116. // DO NOT REMOVE THIS LINE
  117. javascriptEnabled: true,
  118. },
  119. },
  120. },
  121. devServer: {
  122. // development server port 8000
  123. port: 8000,
  124. // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  125. proxy: {
  126. '/api': {
  127. target: 'https://dev.autocde.com/',
  128. ws: false,
  129. changeOrigin: true,
  130. },
  131. '/c2': {
  132. target: 'https://dev-1322828622.cos.ap-guangzhou.myqcloud.com/',
  133. ws: false,
  134. changeOrigin: true,
  135. },
  136. },
  137. },
  138. // disable source map in production
  139. productionSourceMap: false,
  140. lintOnSave: false,
  141. // babel-loader no-ignore node_modules/*
  142. transpileDependencies: [],
  143. }
  144. // preview.pro.loacg.com only do not use in your production;
  145. if (process.env.VUE_APP_PREVIEW === 'true') {
  146. // add `ThemeColorReplacer` plugin to webpack plugins
  147. vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  148. }
  149. module.exports = vueConfig