vue.config.js 5.0 KB

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