vue.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @author chuzhixin 1204505056@qq.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. } = 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. const productionGzipExtensions = ['html', 'js', 'css', 'svg']
  29. process.env.VUE_APP_TITLE = title || 'vue-admin-beautiful'
  30. process.env.VUE_APP_AUTHOR = author || 'chuzhixin 1204505056@qq.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. chunks: 'all',
  104. cacheGroups: {
  105. libs: {
  106. name: 'chunk-libs',
  107. test: /[\\/]node_modules[\\/]/,
  108. priority: 10,
  109. chunks: 'initial',
  110. },
  111. elementUI: {
  112. name: 'chunk-elementUI',
  113. priority: 20,
  114. test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
  115. },
  116. fortawesome: {
  117. name: 'chunk-fortawesome',
  118. priority: 20,
  119. test: /[\\/]node_modules[\\/]_?@fortawesome(.*)/,
  120. },
  121. },
  122. })
  123. config
  124. .plugin('banner')
  125. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  126. .end()
  127. config.module
  128. .rule('images')
  129. .use('image-webpack-loader')
  130. .loader('image-webpack-loader')
  131. .options({
  132. bypassOnDebug: true,
  133. })
  134. .end()
  135. })
  136. if (build7z) {
  137. config.when(process.env.NODE_ENV === 'production', (config) => {
  138. config
  139. .plugin('fileManager')
  140. .use(FileManagerPlugin, [
  141. {
  142. onEnd: {
  143. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  144. archive: [
  145. {
  146. source: `./${outputDir}`,
  147. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  148. },
  149. ],
  150. },
  151. },
  152. ])
  153. .end()
  154. })
  155. }
  156. },
  157. runtimeCompiler: true,
  158. productionSourceMap: false,
  159. css: {
  160. requireModuleExtension: true,
  161. sourceMap: true,
  162. loaderOptions: {
  163. scss: {
  164. /*sass-loader 8.0语法 */
  165. //prependData: '@import "~@/styles/variables.scss";',
  166. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  167. additionalData(content, loaderContext) {
  168. const { resourcePath, rootContext } = loaderContext
  169. const relativePath = path.relative(rootContext, resourcePath)
  170. if (
  171. relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss'
  172. ) {
  173. return '@import "~@/styles/variables.scss";' + content
  174. }
  175. return content
  176. },
  177. },
  178. },
  179. },
  180. }