Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant design - huge imports

Tags:

reactjs

antd

I'm using ant design library for my react application.

And I've faced with huge imports, that hurts my bundle (currently 1.1 mb in minified version because of ant-design lib).

How can I differently import antd components through all my app?

enter image description here

UPDATE:

Seems antd has some huge or non optimized modules. Here the thing - only difference is import Datepicker module, and.. boom! + almost 2MB (in dev bundle ofc.)

enter image description here

like image 564
WebArtisan Avatar asked Feb 10 '18 13:02

WebArtisan


People also ask

Is ant design heavy?

Ant Design with basic setup makes your bundle size nearly 1.5 MB (without gzip). And they say that Ant Design by default supports Tree Shaking.

What companies use ant design?

Created by Chinese conglomerate Alibaba, Ant Design is used by several big names: Alibaba (of course), Tencent, Baidu, and more. While Material-UI remains the most popular React UI library with over 40k stars on Github, Ant Design is currently at a close second, and is quickly closing-in the gap.

Is ant design the best?

Ant Design is by far my preferred React UI library. It comes with dozens of components, each of which has many different properties. The official documentation is well-written, complete, and full of working examples. Based on my experience, I consider antd the best choice for beginners.

Is Ant Design Pro paid?

Ant Design Pro is described as an out-of-box UI solution for enterprise applications but it is open-source and totally free (its name has the word Pro which can confuse many of us).


Video Answer


2 Answers

UPD: the underlying issue seems to be resolved for the new (4.0) version of antd.
Therefore, if you try to resolve this issue for the earlier versions, the recommended way is to migrate onto antd 4

Previous answer:

At the moment, a huge part of antd dist is SVG icons.
There is no official way to deal with it yet (check the issue on github).

But a workaround exists.

  1. Adapt webpack to resolve icons differently. In your webpack config:
module.exports = {   //...   resolve: {     alias: {       "@ant-design/icons/lib/dist$": path.resolve(__dirname, "./src/icons.js")     }   } }; 
  1. Create icons.js in the folder src/ or wherever you want it. Be sure it matches the alias path!
    In this file, you define which icons antd should include.
export {   default as DownOutline } from "@ant-design/icons/lib/outline/DownOutline"; 

It's also possible to do this with react-app-rewired (create-react-app modifications) within config-overrides.js

like image 155
4 revs, 2 users 64% Avatar answered Oct 11 '22 17:10

4 revs, 2 users 64%


1) Prevent antd to load the all moment localization. Add webpack plugin and configure it in webpack.config.js like the follow:

plugins: [     new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /ru/), ], resolve: {     alias: {moment: `moment/moment.js`} }, target: `web` } 

2) Use the same moment version as in antd library.

3) Use modularized antd Use babel-plugin-import

// .babelrc or babel-loader option {   "plugins": [     ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }]     // `style: true` for less   ] } 

I use BundleAnalyzerPlugin to analyze the bundle.

plugins: [new BundleAnalyzerPlugin()]

like image 27
sultan Avatar answered Oct 11 '22 19:10

sultan