vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @author https://gitee.com/chu1204505056/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. build7z,
  17. donation,
  18. } = require('./src/config')
  19. const { webpackBarName, webpackBanner, donationConsole } = require('zx-layouts')
  20. if (donation) donationConsole()
  21. const { version, author } = require('./package.json')
  22. const Webpack = require('webpack')
  23. const WebpackBar = require('webpackbar')
  24. const FileManagerPlugin = require('filemanager-webpack-plugin')
  25. const dayjs = require('dayjs')
  26. const date = dayjs().format('YYYY_M_D')
  27. const time = dayjs().format('YYYY-M-D HH:mm:ss')
  28. process.env.VUE_APP_TITLE = title || 'vue-admin-beautiful'
  29. process.env.VUE_APP_AUTHOR =
  30. author || 'https://gitee.com/chu1204505056/vue-admin-better'
  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. config.module
  138. .rule('images')
  139. .use('image-webpack-loader')
  140. .loader('image-webpack-loader')
  141. .options({
  142. bypassOnDebug: true,
  143. })
  144. .end()
  145. })
  146. if (build7z) {
  147. config.when(process.env.NODE_ENV === 'production', (config) => {
  148. config
  149. .plugin('fileManager')
  150. .use(FileManagerPlugin, [
  151. {
  152. onEnd: {
  153. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  154. archive: [
  155. {
  156. source: `./${outputDir}`,
  157. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  158. },
  159. ],
  160. },
  161. },
  162. ])
  163. .end()
  164. })
  165. }
  166. },
  167. runtimeCompiler: true,
  168. productionSourceMap: false,
  169. css: {
  170. requireModuleExtension: true,
  171. sourceMap: true,
  172. loaderOptions: {
  173. scss: {
  174. /*sass-loader 8.0语法 */
  175. //prependData: '@import "~@/styles/variables.scss";',
  176. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  177. additionalData(content, loaderContext) {
  178. const { resourcePath, rootContext } = loaderContext
  179. const relativePath = path.relative(rootContext, resourcePath)
  180. if (
  181. relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss'
  182. ) {
  183. return '@import "~@/styles/variables.scss";' + content
  184. }
  185. return content
  186. },
  187. },
  188. },
  189. },
  190. }