vue.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @description vue.config.js全局配置
  3. */
  4. const path = require('path')
  5. const {
  6. /* baseURL, */
  7. publicPath,
  8. assetsDir,
  9. outputDir,
  10. transpileDependencies,
  11. title,
  12. abbreviation,
  13. devPort,
  14. providePlugin,
  15. build7z,
  16. buildGzip,
  17. imageCompression,
  18. } = require('./src/config')
  19. // const rely = require('vue-plugin-rely')
  20. const { webpackBarName, webpackBanner } = require('./vab.config')
  21. const { version, author } = require('./package.json')
  22. const Webpack = require('webpack')
  23. const WebpackBar = require('webpackbar')
  24. const FileManagerPlugin = require('filemanager-webpack-plugin')
  25. const dayjs = require('dayjs')
  26. const dateTime = dayjs().format('YYYY-M-D HH:mm:ss')
  27. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  28. const productionGzipExtensions = ['html', 'js', 'css', 'svg']
  29. process.env.VUE_APP_TITLE = title
  30. process.env.VUE_APP_AUTHOR = author
  31. process.env.VUE_APP_RANDOM = `${dayjs()}-${process.env.VUE_GITHUB_USER_NAME}`
  32. process.env.VUE_APP_UPDATE_TIME = dateTime
  33. process.env.VUE_APP_VERSION = version
  34. // process.env.VUE_APP_RELY = rely
  35. const resolve = (dir) => {
  36. return path.join(__dirname, dir)
  37. }
  38. const microServiceProxyPrefix = '/micro-srv-proxy'
  39. const restApiProxyPrefix = '/api'
  40. const isHttpUrl = (url) => /^https?:\/\//i.test(url || '')
  41. module.exports = {
  42. publicPath,
  43. assetsDir,
  44. outputDir,
  45. lintOnSave: false,
  46. transpileDependencies,
  47. devServer: {
  48. hot: true,
  49. port: devPort,
  50. open: true,
  51. noInfo: false,
  52. overlay: {
  53. warnings: true,
  54. errors: true,
  55. },
  56. // 开发环境代理:当前端和后端端口不一致时,统一经由 devServer 转发,避免浏览器 CORS 限制
  57. proxy: isHttpUrl(process.env.VUE_APP_MicroSrvProxy_API)
  58. ? {
  59. [restApiProxyPrefix]: {
  60. target: process.env.VUE_APP_MicroSrvProxy_API,
  61. ws: false,
  62. changeOrigin: true,
  63. pathRewrite: {
  64. [`^${restApiProxyPrefix}`]: '',
  65. },
  66. },
  67. [microServiceProxyPrefix]: {
  68. target: process.env.VUE_APP_MicroSrvProxy_API,
  69. ws: true,
  70. changeOrigin: true,
  71. pathRewrite: {
  72. [`^${microServiceProxyPrefix}`]: '',
  73. },
  74. },
  75. }
  76. : {},
  77. },
  78. configureWebpack() {
  79. return {
  80. resolve: {
  81. alias: {
  82. '~': resolve('.'),
  83. '@': resolve('src'),
  84. },
  85. },
  86. plugins: [
  87. new Webpack.ProvidePlugin(providePlugin),
  88. new WebpackBar({
  89. name: webpackBarName,
  90. }),
  91. ],
  92. }
  93. },
  94. chainWebpack(config) {
  95. config.resolve.symlinks(true)
  96. config.module.rule('svg').exclude.add(resolve('src/icon'))
  97. config.module
  98. .rule('vabIcon')
  99. .test(/\.svg$/)
  100. .include.add(resolve('src/icon'))
  101. .end()
  102. .use('svg-sprite-loader')
  103. .loader('svg-sprite-loader')
  104. .options({ symbolId: 'vab-icon-[name]' })
  105. config.when(process.env.NODE_ENV === 'development', (config) => {
  106. config.devtool('source-map')
  107. })
  108. config.when(process.env.NODE_ENV === 'production', (config) => {
  109. config.performance.set('hints', false)
  110. config.devtool('none')
  111. config.optimization.splitChunks({
  112. automaticNameDelimiter: '-',
  113. chunks: 'all',
  114. cacheGroups: {
  115. chunk: {
  116. name: 'vab-chunk',
  117. test: /[\\/]node_modules[\\/]/,
  118. minSize: 131072,
  119. maxSize: 524288,
  120. chunks: 'async',
  121. minChunks: 2,
  122. priority: 10,
  123. },
  124. vue: {
  125. name: 'vue',
  126. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  127. chunks: 'initial',
  128. priority: 20,
  129. },
  130. elementUI: {
  131. name: 'element-ui',
  132. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  133. priority: 30,
  134. },
  135. extra: {
  136. name: 'vab-extra',
  137. test: resolve('src/extra'),
  138. priority: 40,
  139. },
  140. // 根据使用模块抽取公共代码
  141. // echarts: {
  142. // name: 'echarts',
  143. // test: /[\\/]node_modules[\\/](echarts|zrender|tslib)[\\/]/,
  144. // priority: 50,
  145. // },
  146. },
  147. })
  148. config.plugin('banner').use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`])
  149. if (imageCompression)
  150. config.module
  151. .rule('images')
  152. .use('image-webpack-loader')
  153. .loader('image-webpack-loader')
  154. .options({
  155. bypassOnDebug: true,
  156. })
  157. .end()
  158. if (buildGzip)
  159. config.plugin('compression').use(CompressionWebpackPlugin, [
  160. {
  161. filename: '[path][base].gz[query]',
  162. algorithm: 'gzip',
  163. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  164. threshold: 8192,
  165. minRatio: 0.8,
  166. },
  167. ])
  168. if (build7z)
  169. config.plugin('fileManager').use(FileManagerPlugin, [
  170. {
  171. events: {
  172. onEnd: {
  173. archive: [
  174. {
  175. source: `./${outputDir}`,
  176. destination: `./${outputDir}/${abbreviation}_${dayjs().unix()}.zip`,
  177. },
  178. ],
  179. },
  180. },
  181. },
  182. ])
  183. })
  184. },
  185. runtimeCompiler: true,
  186. productionSourceMap: false,
  187. css: {
  188. requireModuleExtension: true,
  189. sourceMap: true,
  190. loaderOptions: {
  191. sass: {
  192. sassOptions: { outputStyle: 'expanded' },
  193. additionalData(content, loaderContext) {
  194. const { resourcePath, rootContext } = loaderContext
  195. const relativePath = path.relative(rootContext, resourcePath)
  196. if (relativePath.replace(/\\/g, '/') !== 'src/vab/styles/variables/variables.scss')
  197. return '@import "~@/vab/styles/variables/variables.scss";' + content
  198. return content
  199. },
  200. },
  201. },
  202. },
  203. }