vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/settings')
  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. const productionGzipExtensions = ['html', 'js', 'css', 'svg']
  25. process.env.VUE_APP_TITLE = title || 'vue-admin-beautiful'
  26. process.env.VUE_APP_AUTHOR = author || 'chuzhixin 1204505056@qq.com'
  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')
  32. return require('./mock/mockServer.js')
  33. else return ''
  34. }
  35. module.exports = {
  36. publicPath,
  37. assetsDir,
  38. outputDir,
  39. lintOnSave,
  40. transpileDependencies,
  41. devServer: {
  42. hot: true,
  43. port: devPort,
  44. open: true,
  45. noInfo: false,
  46. overlay: {
  47. warnings: true,
  48. errors: true,
  49. },
  50. after: mockServer(),
  51. },
  52. configureWebpack() {
  53. return {
  54. resolve: {
  55. alias: {
  56. '@': resolve('src'),
  57. },
  58. },
  59. plugins: [
  60. new Webpack.ProvidePlugin(providePlugin),
  61. new WebpackBar({
  62. name: webpackBarName,
  63. }),
  64. ],
  65. }
  66. },
  67. chainWebpack(config) {
  68. config.plugins.delete('preload')
  69. config.plugins.delete('prefetch')
  70. config.module
  71. .rule('svg')
  72. .exclude.add(resolve('src/remixIcon'))
  73. .add(resolve('src/colorfulIcon'))
  74. .end()
  75. config.module
  76. .rule('remixIcon')
  77. .test(/\.svg$/)
  78. .include.add(resolve('src/remixIcon'))
  79. .end()
  80. .use('svg-sprite-loader')
  81. .loader('svg-sprite-loader')
  82. .options({ symbolId: 'remix-icon-[name]' })
  83. .end()
  84. config.module
  85. .rule('colorfulIcon')
  86. .test(/\.svg$/)
  87. .include.add(resolve('src/colorfulIcon'))
  88. .end()
  89. .use('svg-sprite-loader')
  90. .loader('svg-sprite-loader')
  91. .options({ symbolId: 'colorful-icon-[name]' })
  92. .end()
  93. /* config.when(process.env.NODE_ENV === "development", (config) => {
  94. config.devtool("source-map");
  95. }); */
  96. config.when(process.env.NODE_ENV !== 'development', (config) => {
  97. config.performance.set('hints', false)
  98. config.devtool('none')
  99. config.optimization.splitChunks({
  100. chunks: 'all',
  101. cacheGroups: {
  102. libs: {
  103. name: 'chunk-libs',
  104. test: /[\\/]node_modules[\\/]/,
  105. priority: 10,
  106. chunks: 'initial',
  107. },
  108. elementUI: {
  109. name: 'chunk-elementUI',
  110. priority: 20,
  111. test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
  112. },
  113. fortawesome: {
  114. name: 'chunk-fortawesome',
  115. priority: 20,
  116. test: /[\\/]node_modules[\\/]_?@fortawesome(.*)/,
  117. },
  118. },
  119. })
  120. config
  121. .plugin('banner')
  122. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  123. .end()
  124. config.module
  125. .rule('images')
  126. .use('image-webpack-loader')
  127. .loader('image-webpack-loader')
  128. .options({
  129. bypassOnDebug: true,
  130. })
  131. .end()
  132. })
  133. if (build7z) {
  134. config.when(process.env.NODE_ENV === 'production', (config) => {
  135. config
  136. .plugin('fileManager')
  137. .use(FileManagerPlugin, [
  138. {
  139. onEnd: {
  140. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  141. archive: [
  142. {
  143. source: `./${outputDir}`,
  144. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  145. },
  146. ],
  147. },
  148. },
  149. ])
  150. .end()
  151. })
  152. }
  153. },
  154. runtimeCompiler: true,
  155. productionSourceMap: false,
  156. css: {
  157. requireModuleExtension: true,
  158. sourceMap: true,
  159. loaderOptions: {
  160. scss: {
  161. /*sass-loader 8.0语法 */
  162. //prependData: '@import "~@/styles/variables.scss";',
  163. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  164. additionalData(content, loaderContext) {
  165. const { resourcePath, rootContext } = loaderContext
  166. const relativePath = path.relative(rootContext, resourcePath)
  167. if (
  168. relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss'
  169. ) {
  170. return '@import "~@/styles/variables.scss";' + content
  171. }
  172. return content
  173. },
  174. },
  175. },
  176. },
  177. }