Craco Ant Design Plugin
Use react-app-rewired for
- Less (provided by craco-less)
babel-plugin-importto only import the required CSS, instead of everything
- An easy way to customize the theme. Set your custom variables in
The latest version of
craco-antd is tested with:
First, follow the beginning of the Ant Design
create-react-app Documentation to set up your app with Ant Design.
(Stop before the "Advanced Guides" section, because this plugin handles all of that for you.)
Then, follow the
craco Installation Instructions to install the
craco package, create a
craco.config.js file, and modify the scripts in your
$ yarn add craco-antd antd# OR$ npm i -S craco-antd antd
craco-antdonly has a "peer dependency" for
antd >= 3.0.0. You should add
antdto your own
package.jsonand use a fixed version (e.g.
3.11.2). Be careful when upgrading
antd, because unexpected changes could break your application.
Here is a complete
craco.config.js configuration file that sets up Less compilation and
const CracoAntDesignPlugin = ;moduleexports =plugins: plugin: CracoAntDesignPlugin;
Here is a production-ready
craco.config.js file that sets up
It also sets up Preact with the
craco-preact plugin. (Preact is faster and smaller than React, and it works fine with Ant Design.)
I put my custom theme variables in
src/style/AntDesign/customTheme.less. I also use that folder for some custom components and other CSS.
const BundleAnalyzerPlugin = ;const WebpackBar = ;const CracoAntDesignPlugin = ;const path = ;// Don't open the browser during developmentprocessenvBROWSER = "none";moduleexports =webpack:plugins:profile: true...processenvNODE_ENV === "development"? openAnalyzer: false:plugins:plugin:plugin: CracoAntDesignPluginoptions:customizeThemeLessPath: path;
Customize Ant Design Theme
You can modify the default Ant Design theme by changing some Less variables.
craco-antd will look for variables in a Less file at
./antd.customize.less. (You can customize this file path with the
// ./antd.customize.less@primary-color: #1da57a;@link-color: #1da57a;
You can also customize these variables directly in your
craco.config.js with the
const CracoAntDesignPlugin = ;moduleexports =plugins:plugin: CracoAntDesignPluginoptions:customizeTheme:"@primary-color": "#1DA57A""@link-color": "#1DA57A";
customizeThemeis just an alias for the
If you use multiple options to customize the theme variables, they are merged together in the following order:
- The file at
For more information, see Ant Design's "Customize Theme" documentation.
Reload Custom Variables During Development
The webpack dev server needs to be restarted whenever you make a change to your custom theme variables. (It's not possible to reload this file automatically, because the variables are set in the webpack config. I tried to fix this issue but hit a dead-end.)
However, you can automatically restart webpack by wrapping
craco start with
yarn add -D nodemon# Or globally (not recommended):npm install -g nodemon
Update the "start" script in
./antd.customize.lessif you are using a different file.)
The webpack dev server will now be restarted whenever you make a change to
Restart Webpack When
While you're here, you can also add
-w craco.config.js to restart webpack whenever your
craco configuration changes (
craco doesn't do this automatically):
Disable "Open In Browser"
create-react-app will open a new browser tab every time it starts. This can be really annoying, especially if you set up the
nodemon watcher. You can disable this behavior with an environment variable:
You can set this in an
I prefer to set it at the top of
// Don't open the browser during developmentprocessenvBROWSER = "none";
You can pass an
options object to configure the loaders and plugins. You can also pass a
modifyLessRule callback to have full control over the Less webpack rule.
craco-less documentation for more information about these options:
moduleexports =plugins:plugin: CracoAntDesignPluginoptions:lessLoaderOptions:modifyVars: "@primary-color": "#1DA57A"strictMath: truenoIeCompat: truecssLoaderOptions:modules: truelocalIdentName: "[local]_[hash:base64:5]";
Large Bundle Size from Ant Design Icons
You can use the webpack-bundle-analyzer plugin to see a breakdown of all the JS and CSS in your webpack build. Here's how to add this plugin to your
craco.config.js configuration file:
const BundleAnalyzerPlugin = ;moduleexports =webpack:plugins:plugins: plugin:;
If you have imported any icons from Ant Design, you will see a very large (> 500KB) entry for
In the meantime, you can set up an import alias and only include the required icons.
If you need to configure anything else for the webpack build, take a look at the
Configuration Overview section in the
craco README. You can use
CracoAntDesignPlugin while making other changes to
$ yarn install# OR$ npm install
$ yarn test
Before submitting a pull request, please check the following:
- All tests are passing
- 100% test coverage
- Coverage will be printed after running tests.
- Check the coverage results in your browser:
- No ESLint errors
- All code is formatted with Prettier
- If you use VS Code, you should enable the
We've included a test React application in this repo, under
cd test-app yarn install
Test the app in development:
Test the production build:
yarn build # Install the "serve" package yarn global add serve # Serve the production build serve -s build
Releasing a new version
- Make sure the "Supported Versions" section is updated at the top of the README.
- Check which files will be included in the NPM package:
.npmignoreto exclude any files.
- Release new version to NPM: