React中使用antd 4.x后在网上没搜到CSS按需加载的教程,自己摸索了一番,踩了一些坑,简单记录一下。
2021-7-8更新:
craco社区是有相关插件来完成按需加载功能的,自己之前没注意…
craco传送门:https://github.com/gsoft-inc/craco
craco-antd传送门:https://github.com/DocSpring/craco-antd
在antd 4.x后,使用
craco
对 create-react-app 进行自定义配置。craco
的相关配置看官网使用
babel-plugin-import
,babel-plugin-import
是一个用于按需加载组件代码和样式的 babel 插件- 安装:
$ yarn add babel-plugin-import
- 配置:在
craco.config.js
中配置如下:module.exports = { babel: { plugins: [['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css', }]], }, };
- 安装:
踩坑:
不要使用.babelrc
文件,原因看这
曾尝试引入craco社区维护的相关插件来解决.babelrc
问题,最终以失败告终还是多看官方的配置文档有用。
babel-plugin-import
craco补充:在网上看见了一个其他教程,未测试是否可用
https://www.cnblogs.com/leyi/p/10985913.html
版权声明:本文为leejieleejie原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。