在create-react-app创建的项目中对antd的样式按需引入
下载 customize-cra, react-app-rewired, babel-plugin-import
yarn add customize-cra react-app-rewired babel-plugin-import --dev
customize-cra文档:https://github.com/arackaf/customize-cra
react-app-rewired文档:https://github.com/timarney/react-app-rewired
babel-plugin-import文档:https://github.com/ant-design/babel-plugin-import
在package.json的同级创建config-overrides.js文件
config-overrides.js文件配置
const { override, fixBabelImports } = require('customize-cra'); module.exports = override( fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'lib', style: "css", //如果是less文件则改为true }) );
fixBabelImports配置:https://github.com/arackaf/customize-cra/blob/master/api.md#fixbabelimportslibraryname-options
import部分配置找babel-plugin-import:https://github.com/ant-design/babel-plugin-import#usage
修改package.json的
script
选项 安装react-app-rewired后的:https://github.com/timarney/react-app-rewired#3-flip-the-existing-calls-to-react-scripts-in-npm-scripts-for-start-build-and-test
/* package.json */ "scripts": { - "start": "react-scripts start", + "start": "react-app-rewired start", - "build": "react-scripts build", + "build": "react-app-rewired build", - "test": "react-scripts test", + "test": "react-app-rewired test", "eject": "react-scripts eject" }