1、安裝antd npm install antd --save
2、安裝(react-app-rewired)一個(gè)對 create-react-app 進(jìn)行自定義配置的社區(qū)解決方案
? yarn add react-app-rewired? ? /? cnpm install? react-app-rewired --save
3、修改 package.json
react-scripts 需改為react-app-rewired
? "scripts": {
? ? "start": "react-app-rewired start",
? ? "build": "react-app-rewired build",
? ? "test": "react-app-rewired test --env=jsdom",
? ? "eject": "react-app-rewired eject"
}
4、在項(xiàng)目根目錄創(chuàng)建一個(gè) config-overrides.js 用于修改默認(rèn)配置
module.exports = function override(config, env) {
// do stuff with the webpack config...
return config;
};
5、安裝babel-plugin-import? babel-plugin-import是一個(gè)用于按需加載組件代碼和樣式的 babel 插件
yarn add babel-plugin-import? /? cnpm install babel-plugin-import --save
6、修改 config-overrides.js
const { injectBabelPlugin } = require('react-app-rewired');
? module.exports = function override(config, env) {
? config = injectBabelPlugin(
? ? ? ['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }],
? ? ? ? ? config,
? ? );
? return config;
};
7、然后移除前面在 src/App.css 里全量添加的 @import '~antd/dist/antd.css'; 直接引入組件使用就會(huì)有對應(yīng)的css
import { Button } from 'antd';
<Button type="primary">Primary</Button>