vue.config.js 5.1 KB

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