前端性能优化三十六:花裤衩模板减少请求次数

1. 使用SplitChunks分离代码并实现相同模块共享,从而减少请求次数:

. vue-router使用懒加载.. 按需引用组件库、UI,避免全局注入.. 使用SplitChunks分离代码并实现相同模块共享,从而减少请求次数.

1. 网上案列分析:

(1). 打包工具分析:

. 通过可视化分析页面报告:
    a. 上图会了多个chunk包,导致有大概300个请求.

(2). 配置SplitChunks对注意模块进行分离:

chainWebpack: config => {
 if (!DEV) {
  config.optimization.splitChunks({
    cacheGroups: {
      common: {
        name: 'chunk-common', // 打包后的文件名
        chunks: 'all',
        minChunks: 2,
        maxInitialRequests: 5,
        minSize: 0,
        priority: 1,
        reuseExistingChunk: true
      },
      vendors: {
        name: 'chunk-vendors',
        test: /[\\/]node_modules[\\/]/,
        chunks: 'all',
        priority: 2,
        reuseExistingChunk: true,
        enforce: true
      },
      ElementUI: {
        name: 'chunk-element-ui',
        test: /[\\/]node_modules[\\/]element-ui[\\/]/,
        chunks: 'all',
        priority: 3,
        reuseExistingChunk: true,
        enforce: true
     },
     xlsx: {
      name:"chunk-xlsx",
      test: /[\\/]node_modules[\\/]xlsx[\\/]/,
      chunks: 'all',
        priority: 4,
        reuseExistingChunk: true,
        enforce: true
     }
    }
   })
  }
}

(3). 优化后打包工具分析:

. 通过可视化分析页面报告,明显生成的chunk包比上图要少,差不多130多个请求.

3. 花裤衩模板:

花裤衩vueconfig.js已经配置好了,如下为去掉配置前后对比,可能是项目小,打包效果不太明显.

(1). vueconfig.js:

module.exports = {
  chainWebpack(config) {
    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
            // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值