非按需加载
// App.js
import { Button } from 'antd';
// App.css
@import '~antd/dist/antd.css';
按需加载
- 去掉 App.css 中的 @import ‘~antd/dist/antd.css’;
- 安装依赖
yarn add babel-plugin-import react-app-rewired customize-cra
- 在项目根目录创建一个 config-overrides.js 用于修改默认配置。类似于vue的vue.config.js
const { override, fixBabelImports } = require('customize-cra');
module.exports = override(
fixBabelImports('import', {
libraryName: 'antd',
libraryDirectory: 'es',
style: 'css',
}),
);
- package.json文件中的
react-scripts全部替换为react-app-rewired
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
对比两种方式的chunk.js大小
按需加载

全量加载

本文探讨了React应用中按需加载与全量加载Ant Design组件的不同策略。通过对比chunk.js大小,介绍如何利用babel-plugin-import、react-app-rewired和customize-cra等工具实现按需加载,以优化项目构建大小。
903

被折叠的 条评论
为什么被折叠?



