vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
  3. * @description cli配置
  4. */
  5. const path = require('path')
  6. const {
  7. publicPath,
  8. assetsDir,
  9. outputDir,
  10. lintOnSave,
  11. transpileDependencies,
  12. title,
  13. abbreviation,
  14. devPort,
  15. providePlugin,
  16. donation,
  17. imageCompression,
  18. } = require('./src/config')
  19. const { webpackBarName, webpackBanner, donationConsole } = require('layouts')
  20. if (donation) donationConsole()
  21. const { version, author } = require('./package.json')
  22. const Webpack = require('webpack')
  23. const WebpackBar = require('webpackbar')
  24. const dayjs = require('dayjs')
  25. const date = dayjs().format('YYYY_M_D')
  26. const time = dayjs().format('YYYY-M-D HH:mm:ss')
  27. process.env.VUE_APP_TITLE = '轻马智能体'
  28. process.env.VUE_APP_AUTHOR = author || 'https://vuejs-core.cn'
  29. process.env.VUE_APP_UPDATE_TIME = time
  30. process.env.VUE_APP_VERSION = version
  31. const resolve = (dir) => path.join(__dirname, dir)
  32. const mockServer = () => {
  33. if (process.env.NODE_ENV === 'development') return require('./mock')
  34. else return ''
  35. }
  36. module.exports = {
  37. publicPath,
  38. assetsDir,
  39. outputDir,
  40. lintOnSave,
  41. transpileDependencies,
  42. devServer: {
  43. hot: true,
  44. port: devPort,
  45. open: true,
  46. noInfo: false,
  47. overlay: {
  48. warnings: true,
  49. errors: true,
  50. },
  51. after: mockServer(),
  52. proxy: {
  53. '/wechat-api': {
  54. target: 'http://localhost:19101',
  55. changeOrigin: true,
  56. ws: true,
  57. rewrite: (path) => path.replace(new RegExp(`^/wechat-api`), ''),
  58. },
  59. }
  60. },
  61. configureWebpack() {
  62. return {
  63. resolve: {
  64. alias: {
  65. '@': resolve('src'),
  66. },
  67. },
  68. plugins: [
  69. new Webpack.ProvidePlugin(providePlugin),
  70. new WebpackBar({
  71. name: webpackBarName,
  72. }),
  73. ],
  74. }
  75. },
  76. chainWebpack(config) {
  77. config.plugins.delete('preload')
  78. config.plugins.delete('prefetch')
  79. config.module.rule('svg').exclude.add(resolve('src/remixIcon')).add(resolve('src/colorfulIcon')).end()
  80. config.module
  81. .rule('remixIcon')
  82. .test(/\.svg$/)
  83. .include.add(resolve('src/remixIcon'))
  84. .end()
  85. .use('svg-sprite-loader')
  86. .loader('svg-sprite-loader')
  87. .options({ symbolId: 'remix-icon-[name]' })
  88. .end()
  89. config.module
  90. .rule('colorfulIcon')
  91. .test(/\.svg$/)
  92. .include.add(resolve('src/colorfulIcon'))
  93. .end()
  94. .use('svg-sprite-loader')
  95. .loader('svg-sprite-loader')
  96. .options({ symbolId: 'colorful-icon-[name]' })
  97. .end()
  98. /* config.when(process.env.NODE_ENV === "development", (config) => {
  99. config.devtool("source-map");
  100. }); */
  101. config.when(process.env.NODE_ENV !== 'development', (config) => {
  102. config.performance.set('hints', false)
  103. config.devtool('none')
  104. config.optimization.splitChunks({
  105. automaticNameDelimiter: '-',
  106. chunks: 'all',
  107. cacheGroups: {
  108. chunk: {
  109. name: 'vab-chunk',
  110. test: /[\\/]node_modules[\\/]/,
  111. minSize: 131072,
  112. maxSize: 524288,
  113. chunks: 'async',
  114. minChunks: 2,
  115. priority: 10,
  116. },
  117. vue: {
  118. name: 'vue',
  119. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  120. chunks: 'initial',
  121. priority: 20,
  122. },
  123. elementUI: {
  124. name: 'element-ui',
  125. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  126. priority: 30,
  127. },
  128. extra: {
  129. name: 'vab-layouts',
  130. test: resolve('src/layouts'),
  131. priority: 40,
  132. },
  133. },
  134. })
  135. config
  136. .plugin('banner')
  137. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  138. .end()
  139. if (imageCompression)
  140. config.module
  141. .rule('images')
  142. .use('image-webpack-loader')
  143. .loader('image-webpack-loader')
  144. .options({
  145. bypassOnDebug: true,
  146. })
  147. .end()
  148. })
  149. },
  150. runtimeCompiler: true,
  151. productionSourceMap: false,
  152. css: {
  153. requireModuleExtension: true,
  154. sourceMap: false,
  155. loaderOptions: {
  156. scss: {
  157. additionalData(content, loaderContext) {
  158. const { resourcePath, rootContext } = loaderContext
  159. const relativePath = path.relative(rootContext, resourcePath)
  160. if (relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss') {
  161. return `@import "~@/styles/variables.scss";${content}`
  162. }
  163. return content
  164. },
  165. },
  166. },
  167. },
  168. }