vue.config.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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('./vab.config')
  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_RANDOM = `${dayjs()}-${process.env.VUE_GITHUB_USER_NAME}`
  33. process.env.VUE_APP_UPDATE_TIME = dateTime
  34. process.env.VUE_APP_VERSION = version
  35. process.env.VUE_APP_RELY = rely
  36. const resolve = (dir) => {
  37. return path.join(__dirname, dir)
  38. }
  39. module.exports = {
  40. publicPath,
  41. assetsDir,
  42. outputDir,
  43. lintOnSave,
  44. transpileDependencies,
  45. devServer: {
  46. hot: true,
  47. port: devPort,
  48. open: true,
  49. noInfo: false,
  50. overlay: {
  51. warnings: true,
  52. errors: true,
  53. },
  54. // 注释掉的地方是前端配置代理访问后端的示例
  55. // baseURL必须为/xxx,而不是后端服务器,请先了解代理逻辑,再设置前端代理
  56. // !!!一定要注意!!!
  57. // 1.这里配置了跨域及代理只针对开发环境生效
  58. // 2.不建议你在前端配置跨域,建议你后端配置Allow-Origin,Method,Headers,放行token字段,一步到位
  59. // 3.后端配置了跨域,就不需要前端再配置,会发生Origin冲突
  60. // proxy: {
  61. // [baseURL]: {
  62. // target: `http://你的后端接口地址`,
  63. // ws: true,
  64. // changeOrigin: true,
  65. // pathRewrite: {
  66. // ['^' + baseURL]: '',
  67. // },
  68. // },
  69. // },
  70. // after: require('./mock'),
  71. },
  72. pwa: {
  73. workboxOptions: {
  74. skipWaiting: true,
  75. clientsClaim: true,
  76. },
  77. themeColor: '#ffffff',
  78. msTileColor: '#ffffff',
  79. appleMobileWebAppCapable: 'yes',
  80. appleMobileWebAppStatusBarStyle: 'black',
  81. manifestOptions: {
  82. name: 'Vue Admin Beautiful - Admin Pro',
  83. short_name: 'Admin Pro',
  84. background_color: '#ffffff',
  85. },
  86. },
  87. configureWebpack() {
  88. return {
  89. resolve: {
  90. alias: {
  91. '~': resolve('.'),
  92. '@': resolve('src'),
  93. },
  94. },
  95. plugins: [
  96. new Webpack.ProvidePlugin(providePlugin),
  97. new WebpackBar({
  98. name: webpackBarName,
  99. }),
  100. ],
  101. }
  102. },
  103. chainWebpack(config) {
  104. config.resolve.symlinks(true)
  105. config.module.rule('svg').exclude.add(resolve('src/icon'))
  106. config.module
  107. .rule('vabIcon')
  108. .test(/\.svg$/)
  109. .include.add(resolve('src/icon'))
  110. .end()
  111. .use('svg-sprite-loader')
  112. .loader('svg-sprite-loader')
  113. .options({ symbolId: 'vab-icon-[name]' })
  114. config.when(process.env.NODE_ENV === 'development', (config) => {
  115. config.devtool('source-map')
  116. })
  117. config.when(process.env.NODE_ENV === 'production', (config) => {
  118. //为了防止忘记配置而造成项目无法打包,请保留以下提示
  119. if (process.env.NODE_ENV === 'production') {
  120. if (
  121. process['env'].VUE_GITHUB_USER_NAME == 'test' &&
  122. process['env'].VUE_APP_SECRET_KEY == 'preview'
  123. )
  124. console.log(
  125. '检测到您的用户名和key未配置,key在购买时通过邮件邀请函发放,请仔细阅读文档并进行配置'
  126. )
  127. }
  128. config.performance.set('hints', false)
  129. config.devtool('none')
  130. config.optimization.splitChunks({
  131. automaticNameDelimiter: '-',
  132. chunks: 'all',
  133. cacheGroups: {
  134. chunk: {
  135. name: 'vab-chunk',
  136. test: /[\\/]node_modules[\\/]/,
  137. minSize: 131072,
  138. maxSize: 524288,
  139. chunks: 'async',
  140. minChunks: 2,
  141. priority: 10,
  142. },
  143. vue: {
  144. name: 'vue',
  145. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  146. chunks: 'initial',
  147. priority: 20,
  148. },
  149. elementUI: {
  150. name: 'element-ui',
  151. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  152. priority: 30,
  153. },
  154. extra: {
  155. name: 'vab-extra',
  156. test: resolve('src/extra'),
  157. priority: 40,
  158. },
  159. // 根据使用模块抽取公共代码
  160. // echarts: {
  161. // name: 'echarts',
  162. // test: /[\\/]node_modules[\\/](echarts|zrender|tslib)[\\/]/,
  163. // priority: 50,
  164. // },
  165. },
  166. })
  167. config
  168. .plugin('banner')
  169. .use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`])
  170. if (imageCompression)
  171. config.module
  172. .rule('images')
  173. .use('image-webpack-loader')
  174. .loader('image-webpack-loader')
  175. .options({
  176. bypassOnDebug: true,
  177. })
  178. .end()
  179. if (buildGzip)
  180. config.plugin('compression').use(CompressionWebpackPlugin, [
  181. {
  182. filename: '[path][base].gz[query]',
  183. algorithm: 'gzip',
  184. test: new RegExp(
  185. '\\.(' + productionGzipExtensions.join('|') + ')$'
  186. ),
  187. threshold: 8192,
  188. minRatio: 0.8,
  189. },
  190. ])
  191. if (build7z)
  192. config.plugin('fileManager').use(FileManagerPlugin, [
  193. {
  194. events: {
  195. onEnd: {
  196. archive: [
  197. {
  198. source: `./${outputDir}`,
  199. destination: `./${outputDir}/${abbreviation}_${dayjs().unix()}.zip`,
  200. },
  201. ],
  202. },
  203. },
  204. },
  205. ])
  206. })
  207. },
  208. runtimeCompiler: true,
  209. productionSourceMap: false,
  210. css: {
  211. requireModuleExtension: true,
  212. sourceMap: true,
  213. loaderOptions: {
  214. sass: {
  215. sassOptions: { outputStyle: 'expanded' },
  216. additionalData(content, loaderContext) {
  217. const { resourcePath, rootContext } = loaderContext
  218. const relativePath = path.relative(rootContext, resourcePath)
  219. if (
  220. relativePath.replace(/\\/g, '/') !==
  221. 'src/vab/styles/variables/variables.scss'
  222. )
  223. return (
  224. '@use "sass:math";@import "~@/vab/styles/variables/variables.scss";' +
  225. content
  226. )
  227. return content
  228. },
  229. },
  230. },
  231. },
  232. }