vue.config.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. const timestamp = new Date().getTime()
  42. module.exports = {
  43. publicPath,
  44. assetsDir,
  45. outputDir,
  46. lintOnSave,
  47. transpileDependencies,
  48. devServer: {
  49. hot: true,
  50. port: devPort,
  51. open: true,
  52. noInfo: false,
  53. overlay: {
  54. warnings: true,
  55. errors: true,
  56. },
  57. // 注释掉的地方是前端配置代理访问后端的示例
  58. // baseURL必须为/xxx,而不是后端服务器,请先了解代理逻辑,再设置前端代理
  59. // !!!一定要注意!!!
  60. // 1.这里配置了跨域及代理只针对开发环境生效
  61. // 2.不建议你在前端配置跨域,建议你后端配置Allow-Origin,Method,Headers,放行token字段,一步到位
  62. // 3.后端配置了跨域,就不需要前端再配置,会发生Origin冲突
  63. proxy: {
  64. [baseURL]: {
  65. target: `http://api.lab.site.xmnk.cn/admin`,
  66. // target: `https://syjxkf.hactcm.edu.cn/api`, //仅用于数据查看
  67. ws: true,
  68. changeOrigin: true,
  69. pathRewrite: {
  70. ['^' + baseURL]: '',
  71. },
  72. },
  73. },
  74. after: require('./mock'),
  75. },
  76. configureWebpack() {
  77. return {
  78. resolve: {
  79. alias: {
  80. '~': resolve('.'),
  81. '@': resolve('src'),
  82. },
  83. },
  84. output: {
  85. // 修改输出js目录名及文件名
  86. filename: `js/[name]-${timestamp}.js`,
  87. chunkFilename: `js/[name]-${timestamp}.js`,
  88. },
  89. plugins: [
  90. new Webpack.ProvidePlugin(providePlugin),
  91. new WebpackBar({
  92. name: webpackBarName,
  93. }),
  94. ],
  95. }
  96. },
  97. chainWebpack(config) {
  98. config.resolve.symlinks(true)
  99. config.module.rule('svg').exclude.add(resolve('src/icon'))
  100. config.module
  101. .rule('vabIcon')
  102. .test(/\.svg$/)
  103. .include.add(resolve('src/icon'))
  104. .end()
  105. .use('svg-sprite-loader')
  106. .loader('svg-sprite-loader')
  107. .options({ symbolId: 'vab-icon-[name]' })
  108. config.when(process.env.NODE_ENV === 'development', (config) => {
  109. config.devtool('source-map')
  110. })
  111. config.when(process.env.NODE_ENV === 'production', (config) => {
  112. config.performance.set('hints', false)
  113. config.devtool('none')
  114. config.optimization.splitChunks({
  115. automaticNameDelimiter: '-',
  116. chunks: 'all',
  117. cacheGroups: {
  118. chunk: {
  119. name: 'chunk',
  120. test: /[\\/]node_modules[\\/]/,
  121. minSize: 131072,
  122. maxSize: 524288,
  123. chunks: 'async',
  124. minChunks: 2,
  125. priority: 10,
  126. },
  127. vue: {
  128. name: 'vue',
  129. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  130. chunks: 'initial',
  131. priority: 20,
  132. },
  133. elementUI: {
  134. name: 'element-ui',
  135. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  136. priority: 30,
  137. },
  138. extra: {
  139. name: 'extra',
  140. test: resolve('src/extra'),
  141. priority: 40,
  142. },
  143. // 根据使用模块抽取公共代码
  144. // echarts: {
  145. // name: 'echarts',
  146. // test: /[\\/]node_modules[\\/](echarts|zrender|tslib)[\\/]/,
  147. // priority: 50,
  148. // },
  149. },
  150. })
  151. config
  152. .plugin('banner')
  153. .use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`])
  154. if (imageCompression)
  155. config.module
  156. .rule('images')
  157. .use('image-webpack-loader')
  158. .loader('image-webpack-loader')
  159. .options({
  160. bypassOnDebug: true,
  161. })
  162. .end()
  163. if (buildGzip)
  164. config.plugin('compression').use(CompressionWebpackPlugin, [
  165. {
  166. filename: '[path][base].gz[query]',
  167. algorithm: 'gzip',
  168. test: new RegExp(
  169. '\\.(' + productionGzipExtensions.join('|') + ')$'
  170. ),
  171. threshold: 8192,
  172. minRatio: 0.8,
  173. },
  174. ])
  175. if (build7z)
  176. config.plugin('fileManager').use(FileManagerPlugin, [
  177. {
  178. events: {
  179. onEnd: {
  180. archive: [
  181. {
  182. source: `./${outputDir}`,
  183. destination: `./${outputDir}/${abbreviation}_${dayjs().unix()}.zip`,
  184. },
  185. ],
  186. },
  187. },
  188. },
  189. ])
  190. })
  191. },
  192. runtimeCompiler: true,
  193. productionSourceMap: false,
  194. css: {
  195. requireModuleExtension: true,
  196. sourceMap: true,
  197. loaderOptions: {
  198. scss: {
  199. additionalData(content, loaderContext) {
  200. const { resourcePath, rootContext } = loaderContext
  201. const relativePath = path.relative(rootContext, resourcePath)
  202. if (
  203. relativePath.replace(/\\/g, '/') !==
  204. 'src/vab/styles/variables/variables.scss'
  205. )
  206. return '@import "~@/vab/styles/variables/variables.scss";' + content
  207. return content
  208. },
  209. },
  210. },
  211. },
  212. }