vue.config.js 4.8 KB

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