vue.config.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. module.exports = {
  39. publicPath,
  40. assetsDir,
  41. outputDir,
  42. lintOnSave: false,
  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://你的后端接口地址`,
  62. // ws: true,
  63. // changeOrigin: true,
  64. // pathRewrite: {
  65. // ['^' + baseURL]: '',
  66. // },
  67. // },
  68. // },
  69. },
  70. configureWebpack() {
  71. return {
  72. resolve: {
  73. alias: {
  74. '~': resolve('.'),
  75. '@': resolve('src'),
  76. },
  77. },
  78. plugins: [
  79. new Webpack.ProvidePlugin(providePlugin),
  80. new WebpackBar({
  81. name: webpackBarName,
  82. }),
  83. ],
  84. }
  85. },
  86. chainWebpack(config) {
  87. config.resolve.symlinks(true)
  88. config.module.rule('svg').exclude.add(resolve('src/icon'))
  89. config.module
  90. .rule('vabIcon')
  91. .test(/\.svg$/)
  92. .include.add(resolve('src/icon'))
  93. .end()
  94. .use('svg-sprite-loader')
  95. .loader('svg-sprite-loader')
  96. .options({ symbolId: 'vab-icon-[name]' })
  97. config.when(process.env.NODE_ENV === 'development', (config) => {
  98. config.devtool('source-map')
  99. })
  100. config.when(process.env.NODE_ENV === 'production', (config) => {
  101. config.performance.set('hints', false)
  102. config.devtool('none')
  103. config.optimization.splitChunks({
  104. automaticNameDelimiter: '-',
  105. chunks: 'all',
  106. cacheGroups: {
  107. chunk: {
  108. name: 'vab-chunk',
  109. test: /[\\/]node_modules[\\/]/,
  110. minSize: 131072,
  111. maxSize: 524288,
  112. chunks: 'async',
  113. minChunks: 2,
  114. priority: 10,
  115. },
  116. vue: {
  117. name: 'vue',
  118. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  119. chunks: 'initial',
  120. priority: 20,
  121. },
  122. elementUI: {
  123. name: 'element-ui',
  124. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  125. priority: 30,
  126. },
  127. extra: {
  128. name: 'vab-extra',
  129. test: resolve('src/extra'),
  130. priority: 40,
  131. },
  132. // 根据使用模块抽取公共代码
  133. // echarts: {
  134. // name: 'echarts',
  135. // test: /[\\/]node_modules[\\/](echarts|zrender|tslib)[\\/]/,
  136. // priority: 50,
  137. // },
  138. },
  139. })
  140. config.plugin('banner').use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`])
  141. if (imageCompression)
  142. config.module
  143. .rule('images')
  144. .use('image-webpack-loader')
  145. .loader('image-webpack-loader')
  146. .options({
  147. bypassOnDebug: true,
  148. })
  149. .end()
  150. if (buildGzip)
  151. config.plugin('compression').use(CompressionWebpackPlugin, [
  152. {
  153. filename: '[path][base].gz[query]',
  154. algorithm: 'gzip',
  155. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  156. threshold: 8192,
  157. minRatio: 0.8,
  158. },
  159. ])
  160. if (build7z)
  161. config.plugin('fileManager').use(FileManagerPlugin, [
  162. {
  163. events: {
  164. onEnd: {
  165. archive: [
  166. {
  167. source: `./${outputDir}`,
  168. destination: `./${outputDir}/${abbreviation}_${dayjs().unix()}.zip`,
  169. },
  170. ],
  171. },
  172. },
  173. },
  174. ])
  175. })
  176. },
  177. runtimeCompiler: true,
  178. productionSourceMap: false,
  179. css: {
  180. requireModuleExtension: true,
  181. sourceMap: true,
  182. loaderOptions: {
  183. sass: {
  184. sassOptions: { outputStyle: 'expanded' },
  185. additionalData(content, loaderContext) {
  186. const { resourcePath, rootContext } = loaderContext
  187. const relativePath = path.relative(rootContext, resourcePath)
  188. if (relativePath.replace(/\\/g, '/') !== 'src/vab/styles/variables/variables.scss')
  189. return '@import "~@/vab/styles/variables/variables.scss";' + content
  190. return content
  191. },
  192. },
  193. },
  194. },
  195. }