React学习22----React UI框架 Antd (Ant Design)配置react-app-rewired按需加载Antd的css

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaihaohao1/article/details/87979477

高级配置:文档(以文档为准)
https://ant.design/docs/react/use-with-create-react-app-cn

上一篇,我们现在已经把组件成功运行起来了,但是在实际开发中,上面的例子实际上加载了全部的 antd 组件的样式(对前端加载速度有影响)。

所以我们要进行高级配置
具体配置步骤:
1、安装antd
cnpm install antd --save

2、安装 customize-cra
cnpm install react-app-rewired customize-cra

3、修改 package.json,(第2步完成之后在修改)
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”
},

4、然后在项目根目录创建一个 config-overrides.js 用于修改默认配置。
module.exports = function override(config, env) {
// do stuff with the webpack config…
return config;
};

5、安装babel-plugin-import
cnpm install babel-plugin-import

6、修改config-overrides.js配置
const { override, fixBabelImports } = require(‘customize-cra’);
module.exports = override(
fixBabelImports(‘import’, {
libraryName: ‘antd’,
libraryDirectory: ‘es’,
style: ‘css’,
}),
);

7、移除前面在 src/App.css 里全量添加的 @import ‘~antd/dist/antd.css’; 样式代码,并且按下面的格式引入模块。
import { Button } from ‘antd’;

<Button type="primary">Primary</Button>

源码下载:
rdemo22
https://download.csdn.net/download/zhaihaohao1/10980541

猜你喜欢

转载自blog.csdn.net/zhaihaohao1/article/details/87979477