vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // const WebpackShellPluginNext = require('webpack-shell-plugin-next')
  10. // const buildTime = new Date().toISOString().replace(/[:.-]/g, '').substring(0, 14) //构建时间
  11. function resolve(dir) {
  12. return path.join(__dirname, dir)
  13. }
  14. // check Git
  15. function getGitHash() {
  16. try {
  17. return GitRevision.version()
  18. } catch (e) {}
  19. return 'unknown'
  20. }
  21. // eslint-disable-next-line no-unused-vars
  22. const isProd = process.env.NODE_ENV === 'production'
  23. // eslint-disable-next-line no-unused-vars
  24. const assetsCDN = {
  25. // webpack build externals
  26. externals: {
  27. vue: 'Vue',
  28. 'vue-router': 'VueRouter',
  29. vuex: 'Vuex',
  30. axios: 'axios',
  31. },
  32. css: [],
  33. // https://unpkg.com/browse/vue@2.6.10/
  34. js: [
  35. '//cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js',
  36. '//cdn.jsdelivr.net/npm/vue-router@3.5.1/dist/vue-router.min.js',
  37. '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
  38. '//cdn.jsdelivr.net/npm/axios@0.21.1/dist/axios.min.js',
  39. ],
  40. }
  41. // vue.config.js
  42. const vueConfig = {
  43. // outputDir: `dist/${buildTime}`,
  44. configureWebpack: {
  45. // webpack plugins
  46. plugins: [
  47. // Ignore all locale files of moment.js
  48. new webpack.IgnorePlugin({
  49. contextRegExp: /^\.\/locale$/,
  50. resourceRegExp: /moment$/,
  51. }),
  52. new webpack.DefinePlugin({
  53. APP_VERSION: `"${packageJson.version}"`,
  54. GIT_HASH: JSON.stringify(getGitHash()),
  55. BUILD_DATE: buildDate,
  56. }),
  57. ],
  58. cache: {
  59. type: 'filesystem', // 使用文件缓存
  60. },
  61. // en_US: `if prod, add externals`
  62. // zh_CN: `这里是用来控制编译忽略外部依赖的,与 config.plugin('html') 配合可以编译时引入外部CDN文件依赖`
  63. // externals: isProd ? assetsCDN.externals : {}
  64. },
  65. chainWebpack: (config) => {
  66. config.resolve.alias.set('@$', resolve('src'))
  67. config.output.filename(`static/js/[name].[hash].${Timestamp}.js`).end()
  68. config.output.chunkFilename('static/js/[name].[hash].' + Timestamp + '.js').end()
  69. // fixed svg-loader by https://github.com/damianstasik/vue-svg-loader/issues/185#issuecomment-1126721069
  70. const svgRule = config.module.rule('svg')
  71. // Remove regular svg config from root rules list
  72. config.module.rules.delete('svg')
  73. // if (isProd) {
  74. // config.plugin('shell-plugin').use(WebpackShellPluginNext, [
  75. // {
  76. // onBuildEnd: {
  77. // scripts: [
  78. // `date --utc +%Y%m%dT%H%M%S | tr -d '\n' > dist/latest_build.txt`,
  79. // ],
  80. // blocking: false,
  81. // parallel: true,
  82. // },
  83. // },
  84. // ])
  85. // }
  86. config.module
  87. .rule('svg')
  88. // Use svg component rule
  89. .oneOf('svg_as_component')
  90. .resourceQuery(/inline/)
  91. .test(/\.(svg)(\?.*)?$/)
  92. .use('babel-loader')
  93. .loader('babel-loader?cacheDirectory')
  94. .end()
  95. .use('vue-svg-loader')
  96. .loader('vue-svg-loader?cacheDirectory')
  97. .options({
  98. svgo: {
  99. plugins: [
  100. { prefixIds: true },
  101. { cleanupIDs: true },
  102. { convertShapeToPath: false },
  103. { convertStyleToAttrs: true },
  104. ],
  105. },
  106. })
  107. .end()
  108. .end()
  109. // Otherwise use original svg rule
  110. .oneOf('svg_as_regular')
  111. .merge(svgRule.toConfig())
  112. .end()
  113. // en_US: If prod is on assets require on cdn
  114. // zh_CN: 如果是 prod 模式,则引入 CDN 依赖文件,有需要减少包大小请自行解除依赖
  115. //
  116. // if (isProd) {
  117. // config.plugin('html').tap(args => {
  118. // args[0].cdn = assetsCDN
  119. // return args
  120. // })
  121. // }
  122. },
  123. css: {
  124. loaderOptions: {
  125. less: {
  126. modifyVars: {
  127. // less vars,customize ant design theme
  128. // 'primary-color': '#F5222D',
  129. // 'link-color': '#F5222D',
  130. 'border-radius-base': '2px',
  131. },
  132. // DO NOT REMOVE THIS LINE
  133. javascriptEnabled: true,
  134. },
  135. },
  136. },
  137. devServer: {
  138. // development server port 8000
  139. port: 8000,
  140. // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  141. proxy: {
  142. '/api': {
  143. target: 'https://dev.autocde.com/',
  144. ws: false,
  145. changeOrigin: true,
  146. },
  147. '/c2': {
  148. target: 'https://dev-1322828622.cos.ap-guangzhou.myqcloud.com/',
  149. ws: false,
  150. changeOrigin: true,
  151. },
  152. },
  153. client: {
  154. overlay: false,
  155. },
  156. },
  157. // disable source map in production
  158. productionSourceMap: false,
  159. lintOnSave: false,
  160. // babel-loader no-ignore node_modules/*
  161. transpileDependencies: [],
  162. }
  163. // preview.pro.loacg.com only do not use in your production;
  164. if (process.env.VUE_APP_PREVIEW === 'true') {
  165. // add `ThemeColorReplacer` plugin to webpack plugins
  166. vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  167. }
  168. module.exports = vueConfig