vue.config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @description vue.config.js全局配置
  3. */
  4. const path = require('path')
  5. const {
  6. baseURL,
  7. publicPath,
  8. assetsDir,
  9. outputDir,
  10. lintOnSave,
  11. transpileDependencies,
  12. title,
  13. abbreviation,
  14. devPort,
  15. providePlugin,
  16. build7z,
  17. buildGzip,
  18. imageCompression,
  19. } = require('./src/config')
  20. const rely = require('vue-plugin-rely')
  21. const { webpackBarName, webpackBanner } = require('./vab.config')
  22. const { version, author } = require('./package.json')
  23. const Webpack = require('webpack')
  24. const WebpackBar = require('webpackbar')
  25. const FileManagerPlugin = require('filemanager-webpack-plugin')
  26. const dayjs = require('dayjs')
  27. const dateTime = dayjs().format('YYYY-M-D HH:mm:ss')
  28. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  29. const productionGzipExtensions = ['html', 'js', 'css', 'svg']
  30. process.env.VUE_APP_TITLE = title
  31. process.env.VUE_APP_AUTHOR = author
  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. module.exports = {
  39. publicPath,
  40. assetsDir,
  41. outputDir,
  42. lintOnSave,
  43. transpileDependencies,
  44. devServer: {
  45. hot: true,
  46. port: devPort,
  47. open: true,
  48. noInfo: false,
  49. overlay: {
  50. warnings: true,
  51. errors: true,
  52. },
  53. // 注释掉的地方是前端配置代理访问后端的示例
  54. // baseURL必须为/xxx,而不是后端服务器,请先了解代理逻辑,再设置前端代理
  55. // !!!一定要注意!!!
  56. // 1.这里配置了跨域及代理只针对开发环境生效
  57. // 2.不建议你在前端配置跨域,建议你后端配置Allow-Origin,Method,Headers,放行token字段,一步到位
  58. // 3.后端配置了跨域,就不需要前端再配置,会发生Origin冲突
  59. proxy: {
  60. [baseURL]: {
  61. // target: `http://web.mentor.site.ximengnaikang.com/api`,
  62. // target: `http://web.mentor.site.ximengnaikang.com/api`,
  63. // target: `https://api.remote.site.ximengnaikang.com/api`,
  64. target: `https://api.remote.site.ximengnaikang.com/api`,
  65. ws: true,
  66. changeOrigin: true,
  67. pathRewrite: {
  68. ['^' + baseURL]: '',
  69. },
  70. },
  71. },
  72. after: require('./mock'),
  73. },
  74. configureWebpack() {
  75. return {
  76. resolve: {
  77. alias: {
  78. '~': resolve('.'),
  79. '@': resolve('src'),
  80. },
  81. },
  82. plugins: [
  83. new Webpack.ProvidePlugin(providePlugin),
  84. new WebpackBar({
  85. name: webpackBarName,
  86. }),
  87. ],
  88. }
  89. },
  90. chainWebpack(config) {
  91. config.resolve.symlinks(true)
  92. config.module.rule('svg').exclude.add(resolve('src/icon'))
  93. config.module
  94. .rule('vabIcon')
  95. .test(/\.svg$/)
  96. .include.add(resolve('src/icon'))
  97. .end()
  98. .use('svg-sprite-loader')
  99. .loader('svg-sprite-loader')
  100. .options({ symbolId: 'vab-icon-[name]' })
  101. config.when(process.env.NODE_ENV === 'development', (config) => {
  102. config.devtool('source-map')
  103. })
  104. config.when(process.env.NODE_ENV === 'production', (config) => {
  105. config.performance.set('hints', false)
  106. config.devtool('none')
  107. config.optimization.splitChunks({
  108. automaticNameDelimiter: '-',
  109. chunks: 'all',
  110. cacheGroups: {
  111. chunk: {
  112. name: 'chunk',
  113. test: /[\\/]node_modules[\\/]/,
  114. minSize: 131072,
  115. maxSize: 524288,
  116. chunks: 'async',
  117. minChunks: 2,
  118. priority: 10,
  119. },
  120. vue: {
  121. name: 'vue',
  122. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  123. chunks: 'initial',
  124. priority: 20,
  125. },
  126. elementUI: {
  127. name: 'element-ui',
  128. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  129. priority: 30,
  130. },
  131. extra: {
  132. name: 'extra',
  133. test: resolve('src/extra'),
  134. priority: 40,
  135. },
  136. // 根据使用模块抽取公共代码
  137. // echarts: {
  138. // name: 'echarts',
  139. // test: /[\\/]node_modules[\\/](echarts|zrender|tslib)[\\/]/,
  140. // priority: 50,
  141. // },
  142. },
  143. })
  144. config
  145. .plugin('banner')
  146. .use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`])
  147. if (imageCompression)
  148. config.module
  149. .rule('images')
  150. .use('image-webpack-loader')
  151. .loader('image-webpack-loader')
  152. .options({
  153. bypassOnDebug: true,
  154. })
  155. .end()
  156. if (buildGzip)
  157. config.plugin('compression').use(CompressionWebpackPlugin, [
  158. {
  159. filename: '[path][base].gz[query]',
  160. algorithm: 'gzip',
  161. test: new RegExp(
  162. '\\.(' + productionGzipExtensions.join('|') + ')$'
  163. ),
  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. scss: {
  192. additionalData(content, loaderContext) {
  193. const { resourcePath, rootContext } = loaderContext
  194. const relativePath = path.relative(rootContext, resourcePath)
  195. if (
  196. relativePath.replace(/\\/g, '/') !==
  197. 'src/vab/styles/variables/variables.scss'
  198. )
  199. return '@import "~@/vab/styles/variables/variables.scss";' + content
  200. return content
  201. },
  202. },
  203. },
  204. },
  205. }