craco打包优化

本质还是webpack

1.查看包体积

使用webpack-bundle-analyzer进行分析

const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); 
 new BundleAnalyzerPlugin({
        analyzerMode: 'server',
        analyzerHost: '127.0.0.1',
        analyzerPort: 8888,
        openAnalyzer: true, // 构建完打开浏览器
        reportFilename: path.resolve(__dirname, `analyzer/index.html`), 
    }),

在这里插入图片描述

2 使用splitChunks拆分代码

plugins:[],
configure: (webpackConfig, { env: webpackEnv, paths }) => {
            webpackConfig.optimization.splitChunks = {
                ...webpackConfig.optimization.splitChunks,
                cacheGroups: {
                    base: {
                        // 基本框架
                        chunks: 'all',
                        test: /(react|react-dom|react-dom-router|axios)/,
                        name: 'base',
                        priority: 100,
                    },
                    moment: {
                        test: /[\\/]node_modules[\\/]moment[\\/]/,
                        name: 'moment',
                        priority: 90,
                    },
                    // echarts: {
                    //     test: /(echarts)/,
                    //     name: 'echarts',
                    //     priority: 100,
                    // },
                    commons: {
                        chunks: 'all',
                        // 将两个以上的chunk所共享的模块打包至commons组。
                        minChunks: 2,
                        name: 'commons',
                        priority: 80,
                    },
                },
            };
            return webpackConfig;
        }

3.使用babel-plugin-import按需引入

 babel: {
    plugins: [
      // 配置 babel-plugin-import ant按需加载
      [
        'import',
        {
          libraryName: 'antd',
          libraryDirectory: 'es',
          style: 'true',
        },
        'antd',
      ],
      [
        'import',
        {
          libraryName: 'moment',
          libraryDirectory: 'es',
          style: 'true',
        },
        'moment',
      ],
    ],

使用图片懒加载 LazyLoadImage

yarn add react-lazy-load-image-component

css异步加载

css文件的加载是会block网站渲染的 因此我们可以把css异步加载 具体方法

<link href="73131f5.css" rel="stylesheet" media="jscourse" onload="this.media='all'" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值