vue.config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @author https://vue-admin-beautiful.com (不想保留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. build7z,
  17. donation,
  18. imageCompression,
  19. } = require('./src/config')
  20. const { webpackBarName, webpackBanner, donationConsole } = require('layouts')
  21. if (donation) donationConsole()
  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 date = dayjs().format('YYYY_M_D')
  28. const time = dayjs().format('YYYY-M-D HH:mm:ss')
  29. process.env.VUE_APP_TITLE = title || 'vue-admin-beautiful'
  30. process.env.VUE_APP_AUTHOR = author || 'https://vue-admin-beautiful.com'
  31. process.env.VUE_APP_UPDATE_TIME = time
  32. process.env.VUE_APP_VERSION = version
  33. const resolve = (dir) => path.join(__dirname, dir)
  34. const mockServer = () => {
  35. if (process.env.NODE_ENV === 'development') return require('./mock')
  36. else return ''
  37. }
  38. module.exports = {
  39. publicPath,
  40. assetsDir,
  41. outputDir,
  42. lintOnSave,
  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. after: mockServer(),
  54. },
  55. configureWebpack() {
  56. return {
  57. resolve: {
  58. alias: {
  59. '@': resolve('src'),
  60. },
  61. },
  62. plugins: [
  63. new Webpack.ProvidePlugin(providePlugin),
  64. new WebpackBar({
  65. name: webpackBarName,
  66. }),
  67. ],
  68. }
  69. },
  70. chainWebpack(config) {
  71. config.plugins.delete('preload')
  72. config.plugins.delete('prefetch')
  73. config.module
  74. .rule('svg')
  75. .exclude.add(resolve('src/remixIcon'))
  76. .add(resolve('src/colorfulIcon'))
  77. .end()
  78. config.module
  79. .rule('remixIcon')
  80. .test(/\.svg$/)
  81. .include.add(resolve('src/remixIcon'))
  82. .end()
  83. .use('svg-sprite-loader')
  84. .loader('svg-sprite-loader')
  85. .options({ symbolId: 'remix-icon-[name]' })
  86. .end()
  87. config.module
  88. .rule('colorfulIcon')
  89. .test(/\.svg$/)
  90. .include.add(resolve('src/colorfulIcon'))
  91. .end()
  92. .use('svg-sprite-loader')
  93. .loader('svg-sprite-loader')
  94. .options({ symbolId: 'colorful-icon-[name]' })
  95. .end()
  96. /* config.when(process.env.NODE_ENV === "development", (config) => {
  97. config.devtool("source-map");
  98. }); */
  99. config.when(process.env.NODE_ENV !== 'development', (config) => {
  100. config.performance.set('hints', false)
  101. config.devtool('none')
  102. config.optimization.splitChunks({
  103. automaticNameDelimiter: '-',
  104. chunks: 'all',
  105. cacheGroups: {
  106. chunk: {
  107. name: 'vab-chunk',
  108. test: /[\\/]node_modules[\\/]/,
  109. minSize: 131072,
  110. maxSize: 524288,
  111. chunks: 'async',
  112. minChunks: 2,
  113. priority: 10,
  114. },
  115. vue: {
  116. name: 'vue',
  117. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  118. chunks: 'initial',
  119. priority: 20,
  120. },
  121. elementUI: {
  122. name: 'element-ui',
  123. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  124. priority: 30,
  125. },
  126. extra: {
  127. name: 'vab-layouts',
  128. test: resolve('src/layouts'),
  129. priority: 40,
  130. },
  131. },
  132. })
  133. config
  134. .plugin('banner')
  135. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  136. .end()
  137. if (imageCompression)
  138. config.module
  139. .rule('images')
  140. .use('image-webpack-loader')
  141. .loader('image-webpack-loader')
  142. .options({
  143. bypassOnDebug: true,
  144. })
  145. .end()
  146. })
  147. if (build7z) {
  148. config.when(process.env.NODE_ENV === 'production', (config) => {
  149. config
  150. .plugin('fileManager')
  151. .use(FileManagerPlugin, [
  152. {
  153. onEnd: {
  154. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  155. archive: [
  156. {
  157. source: `./${outputDir}`,
  158. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  159. },
  160. ],
  161. },
  162. },
  163. ])
  164. .end()
  165. })
  166. }
  167. },
  168. runtimeCompiler: true,
  169. productionSourceMap: false,
  170. css: {
  171. requireModuleExtension: true,
  172. sourceMap: false,
  173. loaderOptions: {
  174. scss: {
  175. additionalData(content, loaderContext) {
  176. const { resourcePath, rootContext } = loaderContext
  177. const relativePath = path.relative(rootContext, resourcePath)
  178. if (
  179. relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss'
  180. ) {
  181. return '@import "~@/styles/variables.scss";' + content
  182. }
  183. return content
  184. },
  185. },
  186. },
  187. },
  188. }