Create-react-app: Implement ant-design (antd) in create-react-app

Created on 3 Jun 2017  Â·  25Comments  Â·  Source: facebook/create-react-app

Hi guys.
As you may know, ant-design is being sort of popular in these last days, but the implementation require eject CRA if you want to implement it in the best performant way.

More info:

Is there a way to implement ant-design, with no ejecting CRA?

up for grabs! starter question documentation

Most helpful comment

I don’t understand why Ant docs recommend ejecting and using the Babel plugin.
If I understand it right, you don’t need to eject to use Ant—you can import components one by one:

import Button from 'antd/lib/button';
import 'antd/lib/button/style';

This should work with Create React App.

cc @afc163, would you be willing to amend the docs to recommend this approach for CRA integration rather than ejecting? It’s unfortunate when libraries prescribe people to change their build process.

All 25 comments

this would be really great. antd is really the most mature components framework so far, and even though it is obviously there fault that the library can't be used easily, it would be great if create-react-app could be used easily with antd.

Hi!

Unfortunately this is a design decision made by Ant Design; there's nothing we can do to support it out of the box without adding explicit integration with it. Since this is not used heavily by Facebook or any of our maintainers, adding integration is pretty much out of the question (sorry!).

My suggestion would be to ask ant design to publish a webpack consumable / module friendly build or use a maintained react-scripts fork.

The best solution would be a module-consumable build produced by Ant Design.

I don’t understand why Ant docs recommend ejecting and using the Babel plugin.
If I understand it right, you don’t need to eject to use Ant—you can import components one by one:

import Button from 'antd/lib/button';
import 'antd/lib/button/style';

This should work with Create React App.

cc @afc163, would you be willing to amend the docs to recommend this approach for CRA integration rather than ejecting? It’s unfortunate when libraries prescribe people to change their build process.

Yeap, I did it in that way.
I imported the component from lib folder, and added styles from lib/x/style.

Ant-design says that you have to eject in case you want to import using this way:

import Button from 'antd';

(and just that, with no style and lib folder needed)

That’s fair—I’m just saying that Ant’s “Usage with Create React App” page should probably mention that approach first, and only mention ejecting as last option.

Tagging this as needing help. The actionable item is to submit a PR to Ant docs (or raise an issue) that changes use in create-react-app page to suggest the workaround above rather than ejecting.

@gaearon On it

Issue created: https://github.com/ant-design/ant-design/issues/6371.
The answer really quick, always. (really nice team).
Let's see what happens!

In another hand.
What about adding information about ant (and UI Component-libraries) in the README?

@gaearon antd uses less so we hav eto import css instead:

import 'antd/lib/button/style/index.css';

On the other hand, although eject is not needed to get it running, it is almost necessary in order to customize themes, which is a huge part of web development.

@BrodaNoel do you have any advice for theming?

@khankuan yeap. Check the PR: https://github.com/ant-design/ant-design/pull/6375
They added info about implementing antd with CRA. But I didn't read anything about what you said about css files. I always included all css files instead of less files.

Do you have a need to customize themes like colors in your project?

The CSS is not that big. 43kb gzipped. So you can compile it statically into a themed file and import it in you index.html.

See https://github.com/zeit/next.js/issues/484

See example less theme here:
https://github.com/MrLoh/nextjs-cms/blob/master/static/antd-theme.less

Which is compile with
lessc --clean-css static/antd-theme.less static/antd.min.css

You'd have to adjust the directory structure of course in create React app.

@MrLoh Thanks for the tip! Do you have need for reusing variables such as colors in CRA for an in-house component that is outside of antd?

Yeah sure that comes up. But it's not that much overhead two copy a handful of established color values. Mainly I need colors in SVG components outside of antd

@MrLoh I've tried your example with lessc src/App.less src/App.css but got some errors

FileError: '../../style/themes/default' wasn't found. Tried - F:\proyekto\taskify-antd-ts\node_modules\antd\lib\style\themes\default,F:\proyekto\style\themes\default.les
s,F:\proyekto\style\themes\default,..\..\style\themes\default.less in F:\proyekto\taskify-antd-ts\node_modules\antd\lib\alert\style\index.less

I think it cannot resolve the relative paths of antd?

@afc163 Thank you! using [email protected] worked!

(create react app + typescript + antd) without eject and without using Babel.

https://github.com/qinyang1980/create-react-antd-app.git

(create react app + typescript + antd) eject code to add antd :
it's work for me.

$ yarn add ts-import-plugin --save

in webpack.config.js

const tsImportPluginFactory = require('ts-import-plugin');
const antdTransformer = tsImportPluginFactory({
    libraryName: 'antd',
    libraryDirectory: 'lib',
    style: 'css'
});
{
    test: /\.(ts|tsx)$/,
    include: paths.appSrc,
    use: [{
        loader: require.resolve('ts-loader'),
        options: {
            getCustomTransformers: () => ({ before: [antdTransformer] }),
            // disable type checker - we will use it in fork plugin
            transpileOnly: true
            }
        }]
},

then import antd component:

import { Button } from 'antd';

  <div>
    <Button type="primary">Primary</Button>
    <Button>Default</Button>
    <Button type="dashed">Dashed</Button>
    <Button type="danger">Danger</Button>
  </div>

Check this out. They added docs about how we can use Antd with CRA
https://ant.design/docs/react/use-with-create-react-app

@ani-naslyan that documentation was there for a few years ago.
The problem is that they eject CRA for using it.

Check the firsts lines of that documentation:

create-react-app is one of the best React application development tools. We are going to use antd within it and modify the webpack config for some customized needs.

"and modify the webpack config " <<< eject

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ericdfields picture ericdfields  Â·  78Comments

jantimon picture jantimon  Â·  154Comments

rovansteen picture rovansteen  Â·  192Comments

andreypopp picture andreypopp  Â·  96Comments

akctba picture akctba  Â·  80Comments