随笔 - 105  文章 - 0  评论 - 9  阅读 - 24万 

react编写项目引入npm包打包时,总会将一些npm包重复打包的不同的js文件中,可以使用splitChunks 进行拆分,降低体积,加快速度

1、安装

1
npm install customize-cra webpack-bundle-analyzer

2、修改启动命令,在package.json中修改

1
2
3
4
5
6
"scripts": {
   "start": "react-app-rewired start",
   "build": "react-app-rewired build",
   "test": "react-app-rewired test",
   "eject": "react-app-rewired eject"
 },

3、在项目根目录下新建config-overrides.js 文件,在里面如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const { override } = require("customize-cra");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
 
// View packaging size
const analyzer = () => (config) => {
  if (config.mode === "production") {
    config.plugins.push(new BundleAnalyzerPlugin());
  }
  return config;
};
module.exports = override(
  analyzer(),   //打包后可以查看项目中重复引用的npm包,方便底下拆分
  (config) => {
  config.optimization.splitChunks = {
    cacheGroups: {
      reactDom: {
        chunks: "all",
        test: /(react-dom)/,  //只是示例,可以自己根据需要调整
        name: "reactDom",
        priority: 100,
        enforce: true,
      },
    },
  };
  return config;
});

 配置完成,快去试试效果

 

posted on   随心的博客  阅读(166)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示