vue.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. const pagesConfig = require('./pages.config')
  2. const path = require('path')
  3. const glob = require('glob')
  4. // gzip压缩
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  6. // 代码压缩
  7. const TerserPlugin = require('terser-webpack-plugin')
  8. const projectName = process.env.PROJECT_NAME // 获取package.json中scripts配置的变量
  9. function resolve(dir) {
  10. return path.join(__dirname, dir)
  11. }
  12. const pages = {}
  13. let entryPages = {}
  14. glob.sync('./src/pages/**/main.ts').forEach((entry) => {
  15. const chunk = entry.match(/\.\/src\/pages\/(.*)\/main\.ts/)[1]
  16. const curr = pagesConfig[chunk]
  17. if (curr) {
  18. const pageItem = {
  19. entry,
  20. ...curr,
  21. chunks: process.env.NODE_ENV === 'production'
  22. ? ['chunk-libs', 'chunk-element', 'chunk-commons', 'runtime', chunk]
  23. : ['chunk-vendors', 'chunk-common', chunk]
  24. }
  25. pages[chunk] = pageItem
  26. }
  27. })
  28. projectName ? entryPages[projectName] = pages[projectName] : entryPages = pages
  29. const vueConfig = {
  30. pages: entryPages,
  31. publicPath: './',
  32. // 如果你不需要使用eslint,把lintOnSave设为false即可
  33. lintOnSave: true,
  34. productionSourceMap: process.env.NODE_ENV !== 'production',
  35. outputDir: `dist-${process.env.VUE_APP_CURENV}`,
  36. chainWebpack: config => {
  37. const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
  38. types.forEach((type) => addStyleResource(config.module.rule('less').oneOf(type)))
  39. // 防止多页面打包卡顿
  40. config.plugins.delete('named-chunks')
  41. // 修复HMR
  42. config.resolve.symlinks(true)
  43. config.resolve.alias
  44. .set('@', resolve('src'))
  45. .set('_c', resolve('src/components'))
  46. .set('_p', resolve('src/pages'))
  47. // .set('_pd1', resolve('src/pages/demo1'))
  48. // .set('_pd1v', resolve('src/pages/demo1/views'))
  49. // 设置svg-loader
  50. config.module
  51. .rule('svg')
  52. .exclude.add(resolve('src/assets/icons'))
  53. .end()
  54. config.module
  55. .rule('icons')
  56. .test(/\.svg$/)
  57. .include.add(resolve('src/assets/icons'))
  58. .end()
  59. .use('svg-sprite-loader')
  60. .loader('svg-sprite-loader')
  61. .options({
  62. symbolId: 'icon-[name]'
  63. })
  64. .end()
  65. // 图片压缩
  66. // config.module
  67. // .rule('images')
  68. // .use('image-webpack-loader')
  69. // .loader('image-webpack-loader')
  70. // .options({
  71. // bypassOnDebug: true
  72. // })
  73. // .end()
  74. // 生产环境
  75. config.when(process.env.NODE_ENV === 'production', config => {
  76. // gzip压缩
  77. const productionGzipExtensions = ['html', 'js', 'css']
  78. config.plugin('CompressionWebpackPlugin')
  79. .use(new CompressionWebpackPlugin({
  80. filename: '[path].gz[query]',
  81. algorithm: 'gzip',
  82. test: new RegExp(
  83. '\\.(' + productionGzipExtensions.join('|') + ')$'
  84. ),
  85. threshold: 10240, // 只有大小大于该值的资源会被处理 10240
  86. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  87. deleteOriginalAssets: false // 删除原文件
  88. }))
  89. config.plugin('TerserPlugin')
  90. .use(new TerserPlugin({
  91. terserOptions: {
  92. // 生产环境自动删除console
  93. compress: {
  94. drop_debugger: true,
  95. drop_console: true,
  96. pure_funcs: ['console.log']
  97. }
  98. },
  99. sourceMap: false,
  100. parallel: true
  101. }))
  102. config
  103. .plugin('ScriptExtHtmlWebpackPlugin')
  104. .after(`html`)
  105. .use('script-ext-html-webpack-plugin', [{
  106. inline: /runtime\..*\.js$/
  107. }])
  108. .end()
  109. config
  110. .optimization.splitChunks({
  111. chunks: 'all',
  112. cacheGroups: {
  113. libs: {
  114. name: 'chunk-libs',
  115. test: /[\\/]node_modules[\\/]/,
  116. priority: 10,
  117. chunks: 'initial'
  118. },
  119. element: {
  120. name: 'chunk-element', // 拆分element
  121. priority: 20, // 权重
  122. test: /[\\/]node_modules[\\/]_?element-plus(.*)/
  123. },
  124. commons: {
  125. name: 'chunk-commons',
  126. test: resolve('src/components'),
  127. minChunks: 3, // 最小使用数
  128. priority: 5,
  129. reuseExistingChunk: true
  130. }
  131. }
  132. })
  133. config.optimization.runtimeChunk('single')
  134. Object.keys(entryPages).forEach((page) => {
  135. // 预加载
  136. config.plugin(`preload-${page}`).tap(() => [{
  137. rel: 'preload',
  138. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  139. include: 'initial'
  140. }])
  141. config.plugins.delete(`prefetch-${page}`)
  142. })
  143. })
  144. },
  145. configureWebpack: () => {
  146. process.env.NODE_ENV === 'development' ? 'source-map' : undefined
  147. },
  148. css: {
  149. loaderOptions: {
  150. less: {
  151. javascriptEnabled: true
  152. }
  153. }
  154. },
  155. // 跨域代理
  156. devServer: {
  157. proxy: {
  158. '/api': {
  159. target: 'http://220.160.52.164:8213',
  160. changeOrigin: true,
  161. ws: true,
  162. pathRewrite: {
  163. '^/api': ''
  164. }
  165. }
  166. }
  167. }
  168. }
  169. function addStyleResource(rule) {
  170. rule.use('style-resource')
  171. .loader('style-resources-loader')
  172. .options({
  173. patterns: [
  174. path.resolve(__dirname, './src/styles/variables.less') // 需要全局导入的less
  175. ]
  176. })
  177. }
  178. module.exports = vueConfig