vue.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @author https://gitee.com/chu1204505056/vue-admin-better (不想保留author可删除)
  3. * @description cli配置
  4. */
  5. const url = 'http://localhost:9100'
  6. const path = require('path')
  7. const {
  8. publicPath,
  9. assetsDir,
  10. outputDir,
  11. lintOnSave,
  12. transpileDependencies,
  13. title,
  14. abbreviation,
  15. devPort,
  16. providePlugin,
  17. build7z,
  18. donation,
  19. } = require('./src/config')
  20. const { webpackBarName, webpackBanner, donationConsole } = require('zx-layouts')
  21. if (donation) donationConsole()
  22. const { version, author } = require('./package.json')
  23. const Webpack = require('webpack')
  24. const WebpackBar = require('webpackbar')
  25. const FileManagerPlugin = require('filemanager-webpack-plugin')
  26. const dayjs = require('dayjs')
  27. const date = dayjs().format('YYYY_M_D')
  28. const time = dayjs().format('YYYY-M-D HH:mm:ss')
  29. process.env.VUE_APP_TITLE = title || 'vue-admin-beautiful'
  30. process.env.VUE_APP_AUTHOR =
  31. author || 'https://gitee.com/chu1204505056/vue-admin-better'
  32. process.env.VUE_APP_UPDATE_TIME = time
  33. process.env.VUE_APP_VERSION = version
  34. const resolve = (dir) => path.join(__dirname, dir)
  35. const mockServer = () => {
  36. if (process.env.NODE_ENV === 'development') return require('./mock')
  37. else return ''
  38. }
  39. module.exports = {
  40. publicPath,
  41. assetsDir,
  42. outputDir,
  43. lintOnSave,
  44. transpileDependencies,
  45. devServer: {
  46. hot: true,
  47. port: devPort,
  48. open: true,
  49. noInfo: false,
  50. overlay: {
  51. warnings: false,
  52. errors: false,
  53. },
  54. //after: mockServer(),
  55. proxy: {
  56. '/': {
  57. target: url,
  58. ws: true,
  59. pathRewrite: {
  60. '^/': '/'
  61. }
  62. }
  63. }
  64. },
  65. configureWebpack() {
  66. return {
  67. resolve: {
  68. alias: {
  69. '@': resolve('src'),
  70. },
  71. },
  72. plugins: [
  73. new Webpack.ProvidePlugin(providePlugin),
  74. new WebpackBar({
  75. name: webpackBarName,
  76. }),
  77. ],
  78. }
  79. },
  80. chainWebpack(config) {
  81. config.plugins.delete('preload')
  82. config.plugins.delete('prefetch')
  83. config.module
  84. .rule('svg')
  85. .exclude.add(resolve('src/remixIcon'))
  86. .add(resolve('src/colorfulIcon'))
  87. .end()
  88. config.module
  89. .rule('remixIcon')
  90. .test(/\.svg$/)
  91. .include.add(resolve('src/remixIcon'))
  92. .end()
  93. .use('svg-sprite-loader')
  94. .loader('svg-sprite-loader')
  95. .options({ symbolId: 'remix-icon-[name]' })
  96. .end()
  97. config.module
  98. .rule('colorfulIcon')
  99. .test(/\.svg$/)
  100. .include.add(resolve('src/colorfulIcon'))
  101. .end()
  102. .use('svg-sprite-loader')
  103. .loader('svg-sprite-loader')
  104. .options({ symbolId: 'colorful-icon-[name]' })
  105. .end()
  106. /* config.when(process.env.NODE_ENV === "development", (config) => {
  107. config.devtool("source-map");
  108. }); */
  109. config.when(process.env.NODE_ENV !== 'development', (config) => {
  110. config.performance.set('hints', false)
  111. config.devtool('none')
  112. config.optimization.splitChunks({
  113. automaticNameDelimiter: '-',
  114. chunks: 'all',
  115. cacheGroups: {
  116. chunk: {
  117. name: 'vab-chunk',
  118. test: /[\\/]node_modules[\\/]/,
  119. minSize: 131072,
  120. maxSize: 524288,
  121. chunks: 'async',
  122. minChunks: 2,
  123. priority: 10,
  124. },
  125. vue: {
  126. name: 'vue',
  127. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  128. chunks: 'initial',
  129. priority: 20,
  130. },
  131. elementUI: {
  132. name: 'element-ui',
  133. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  134. priority: 30,
  135. },
  136. extra: {
  137. name: 'vab-layouts',
  138. test: resolve('src/layouts'),
  139. priority: 40,
  140. },
  141. },
  142. })
  143. config
  144. .plugin('banner')
  145. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  146. .end()
  147. config.module
  148. .rule('images')
  149. .use('image-webpack-loader')
  150. .loader('image-webpack-loader')
  151. .options({
  152. bypassOnDebug: true,
  153. })
  154. .end()
  155. })
  156. if (build7z) {
  157. config.when(process.env.NODE_ENV === 'production', (config) => {
  158. config
  159. .plugin('fileManager')
  160. .use(FileManagerPlugin, [
  161. {
  162. onEnd: {
  163. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  164. archive: [
  165. {
  166. source: `./${outputDir}`,
  167. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  168. },
  169. ],
  170. },
  171. },
  172. ])
  173. .end()
  174. })
  175. }
  176. },
  177. runtimeCompiler: true,
  178. productionSourceMap: false,
  179. css: {
  180. requireModuleExtension: true,
  181. sourceMap: true,
  182. loaderOptions: {
  183. scss: {
  184. /*sass-loader 8.0语法 */
  185. //prependData: '@import "~@/styles/variables.scss";',
  186. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  187. additionalData(content, loaderContext) {
  188. const { resourcePath, rootContext } = loaderContext
  189. const relativePath = path.relative(rootContext, resourcePath)
  190. if (
  191. relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss'
  192. ) {
  193. return '@import "~@/styles/variables.scss";' + content
  194. }
  195. return content
  196. },
  197. },
  198. },
  199. },
  200. }