ant design 配置按需加載
使用
create-react-app
建立一個 react 項目,并安裝
antd
。根據文檔需要下載下傳下面幾個插件
react-app-rewired customize-cra babel-plugin-import
。
- 安裝插件
yarn add react-app-rewired customize-cra babel-plugin-import
- 更改
檔案package.json
也就是說,把"scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" },
下的"script"
全部改成react-scripts
react-app-rewired
- 在項目根目錄建立一個
用于修改預設配置config-overrides.js
把上面代碼拷貝到該檔案即可。const { override, fixBabelImports } = require('customize-cra'); module.exports = override( fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css', }), );
- 重新啟動項目
npm run start
- 使用
import { Button } from 'antd' <div> <Button>Button</Button> </div>