vue.config.js 5.8 KB

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