Ant Design component customization and bundle optimization
Tags: typescript, javascript, programming
Published: 2021-01-30 14:57:09

Easily replace original components with your custom wrappers and efficiently decrease bundle size. Step-by-step tutorial with React, TypeScript, Styled-Components.
____
I’m Ivan Kopenkov, a front-end developer. In this article, I will tell you about the approaches I have used for the UI library component customization. You will also learn how to significantly decrease bundle size, cutting off all the unnecessary modules Ant Design takes there.

In our case, we are making wrappers for original Ant Design components inside the project, changing their appearance, and developing their logic. At the same time, we import both customized and original components right from the ant-design module. That saves tree shaking functionality and makes complex library components use our wrappers instead of original nested elements.

If you are already or about to use Ant Design, this article will provide you with a better and more effective way to do so. Even if you have chosen another UI library, you might be able to implement these ideas.

#Problems with using UI libraries
UI libraries provide developers with a variety of ready-to-use components that are commonly required in any project. Usually, such components are covered with tests, and they support the most common use cases.
If you’re going to use one of these libraries, you should be ready to face the next two problems:

1. Surely, every project requires UI components to be modified. The components must match the project design. Moreover, it’s often needed to develop or change some components’ logic for particular use cases.

2.The majority of UI libraries include more components, icons, and utilities than will be used in one project, at least in its early stages. But all these files might be put into the bundle, which can dramatically increase the initial loading time for your app.

The first issue is solved by the customization of library components, and the second is tackled by bundle optimization. Some libraries, including Ant Design, are already adapted for tree shaking, which lets the bundler automatically exclude unused modules from the bundle.

However, even if you use Ant Design, built-in tree shaking support will be not enough to achieve effective bundle size. All the icons of this library will be included in the bundle, as well as the entire Moment.js library with every localization file since it is a dependency for some Ant components. Moreover, if some of the Ant components are re-exported in one file, each of them will be added to the bundle. Even if only one of them is used.

#Methods of customization
Let’s begin by defining available solutions for customization of UI library components.

###1. Redefinition of global classes (CSS only)
This is the simplest method. You just need to add styles for global CSS classes, which are used by UI library components.

The cons:
* The behavior and logic of components can’t be changed or added.
* CSS-in-JS may be used in this way, but only for global class definition, without the superpowers of this solution.
* Global class usage causes unwanted style mixing: the same classes might be used in other parts of a project, and the selected UI-library may be used by third-party modules on the same site.

Indeed, the only advantage of this method is its simplicity.

###2. Local wrappers for components
This method is more advanced, and it involves creating a separate file in your project for every component that you need to customize. Inside such a file, you make a new component, which renders inside itself the optional one from the UI-library.

The pros:

* It lets you customize the styles of the components and also modify component logic.
* You can use all the powers of CSS-in-JS at the same time.

The cons:
* If an original component is used widely across the project, you will need to change all its imports to your new wrapper’s source. It can be quite time-consuming depending on the component usage broadness.
* Suppose you use IDE autocomplete to automatically import selected components, using this approach. In that case, you will need to pay attention to the component you select from the list because you will have at least two of them: the customized one and the original one. It’s easy to forget about this and pick the original component or even accidentally leave imports of some original ones after creating a new wrapper.
And the most important thing: many of the components are complex, and they use inside themselves other components of the same library. Since the original components have absolutely no idea about our wrappers, they will continue to use the original ones inside themselves, ignoring the logic or appearance changes made in wrappers. For example, such an Ant Design component as AutoComplete renders inside itself the components Input and Select. At the same time, inside List are used Grid, Pagination, and Spin. The same thing with Password, Search, and Textarea, which are the dependencies for Input, and so on.

###3. Forking the UI library repository
Making a private copy of the original UI library repository seems to be the most powerful and the most complicated approach at once.

The pros:

* It gives you maximum freedom in appearance customization and logic modification.
* There is the opportunity to reuse the same forked UI library in other projects.

The cons:
* You could meet some complications when you try to pull the original repository updates to the forked one.
* It can be quite inconvenient for developers to continuously modify components in a separate repository to meet the main project’s requirements.

#How we have been customizing Ant components
After a long discussion, our team decided to use the Ant Design UI library for new projects. My responsibility was to create a boilerplate for a new project, which will be used later to launch other projects. It is crucial for us to change styles and also to modify and add logic for components.

We didn’t want to fork the Ant Design repository because we had a bad experience separating the components to a detached repo. Developing [MCS](http://mcs.mail.ru/), we’ve been using the Semantic UI library, storing its components in a separate repository. No convenient way of working with that was found. For the first time, we used to share this repository with another project ([b2c-cloud](https://cloud.mail.ru/)), developing different themes for each other. But that was inconvenient, and changes for one project could accidentally affect another, so at some point, we forked from this repository again. Eventually, we moved the wrappers from the detached repository to the project, and we’re pretty happy with that.

I’ve chosen the second approach to create wrappers directly in the project. At the same time, I wanted customized components to be imported right from the antd module. This allows us to avoid changing imports of already used components when we make wrappers for them. This also saves tree shaking and makes complex components automatically use custom wrappers instead of original components inside themselves.

After that, I will tell you how meeting these requirements was achieved step by step, and you will understand how to implement the same approach in other projects.

### Step 1. Files with wrappers
In the folder where project components are stored, I made a new catalog for future wrappers, called antd. Here, we gradually added new files for wrappers, depending on our demands in modification. Every file is a composition, a wrapper component rendering an original one imported from a UI library. Let’s look at the simplified example of such a file:

{% gist https://gist.github.com/ikopenkov/74b8c4e70e6c02e76170f583165227f9 %}

To demonstrate a method of style customization, I just changed the component background color using Styled Components. To show the method of logic customization, I added the tooltipTitleparameter to additionally render a tooltip when it is passed.

### Step 2. Change component imports with aliases to wrappers

Now let’s consider how to make a builder (here: Webpack) change the original path of modules imported from the root of antd to the path of our wrappers.

We should create an index.ts file in the root folder with wrappers src/components/antd and copy into this file the content of the file located at node_modules/antd/lib/index.d.ts. Then, using the massive replace tool of some IDE, we change every import path from ./componentName to antd/lib/componentName.

By this point, there should be the next content:

{% gist https://gist.github.com/ikopenkov/e8d9de905d50e9fda51f8890d931217d %}

Then, we change the import paths of the components for which we made the wrappers. In this case, we should import Button from src/components/antd/Button:

{% gist https://gist.github.com/ikopenkov/412597f7c932a436802b57f5fafce913 %}

Now we only need to configure Webpack to use these paths as the aliases to the Ant components. I’ve made a simple tool that makes the set of aliases:

{% gist https://gist.github.com/ikopenkov/64fe0800d39ab529f49de20e1beadf2c %}
> Worth noting, here is **the solution to the problem when complex components use original nested elements instead of the custom ones**. A piece of code in the file AntAliases.ts that finds relative imports of nested components inside the complex ones located in the Ant Design library folder files. It then creates aliases for these imports, making complex components use our custom wrappers for the nested components.

The resolve section of our Webpack config looks like this:

{% gist https://gist.github.com/ikopenkov/8f6bd2a6e42314577a22ec7d6162303a %}

### Step 3. TypeScript support (optional)

The first two steps are enough to work on their own. However, if you use TypeScript and change interfaces of original components in your wrappers (as I did in the example, having added the additional property tooltipTitle), then you will need to add aliases to the TypeScript config. In this case, it's much simpler than it was with Webpack; you simply add the path of the file with imports of the wrappers from the previous step to tsconfig.json:

{% gist https://gist.github.com/ikopenkov/b549d69eb20370f102f1d64f737ba922 %}

### Step 4. Variables (optional)

As we use Styled Components for our projects, it’s pretty convenient for us to declare style variables in a single ts file and import some of them where we need them. Ant Design styles were written using Less.js, which allows us to build styles in our project, injecting our variables using less-loader. Thus, it’s a great opportunity to use the same variables inside our components and wrappers, as well as to build styles of the original components with them.

Because our style guide implies naming variables and functions in camelCase, initially we defined variables in this case. Ant Designless-files use kebab-case for variable naming, thus we automatically transform and export these variables in kebab-case as well.

Our file with style variable declarations in short form looks like this:

{% gist https://gist.github.com/ikopenkov/a3230c90cdd5d353bfe705465526531e %}

You can see the complete list of Ant Design variables in [this file](https://github.com/ant-design/ant-design/blob/master/components/style/themes/default.less).

We do injection of variables and building of less-files by adding less-loader into the Webpack configuration:

{% gist https://gist.github.com/ikopenkov/8b473d8f0dca056aefe31b1aa1270bf6 %}

### The component example

Once you have completed the first two steps, everything should work fine. Let’s have a look at the code in which we use the modified component:

{% gist https://gist.github.com/ikopenkov/2483f2cbd76ffd247dea79c21b268802 %}

### The problem with Grid and Radio

You can omit this part if you don’t plan to make Grid and Radio render wrapped components inside themselves instead of original ones. This problem is caused by the fact that Grid is virtually not a separate component. In fact, its source located at node_modules/antd/es/grid/index.jscontains only re-exports of the components Col and Row.

All the other complex components already use our wrappers, thanks to aliases we made. But when we use Grid it will still import original Col and Row because of its file content. To fix this we should consider the next steps.

To illustrate this case, I created a wrapper for Col and made its background red by default.

{% gist https://gist.github.com/ikopenkov/a21da5e466f52b3fa62c1246083f0e73 %}

Then I rendered the original List component for the test and want it to render the modified Col for its columns.

{% gist https://gist.github.com/ikopenkov/b34892363150695a46eb5169bac82ee3 %}

To make List use exactly our wrapper instead of the default Col, we created a new file to replace original re-exports located in node_modules/antd/es/grid/index.js with paths to our wrappers. We applied this new file to antd/Grid.ts, and here is its content:

{% gist https://gist.github.com/ikopenkov/8e758db3332bbaaf13a0b1c391202183 %}

Now we only need to set the path to this file in the constant SPECIAL_ALIASES defined in AntAliases.tsx:

{% gist https://gist.github.com/ikopenkov/1a6103bbab25216eb5d23236449f6540 %}

Finally, the customization part is over. Now List will render our Col wrapper as its columns. To customize Row as well just make a wrapper and change the Row path at src/components/antd/Grid.tsx. It's not very convenient to do, but you only need it for two components: Grid and Radio. Although, during the last year, we haven't received demand for that in our projects.

## Bundle optimization

### Tree shaking

As I mentioned, the latest version of Ant Design is adapted for tree shaking right out of the box. Its previous versions weren’t, so we used to use babel-plugin-import to drop the unused code. I assume that the other libraries without built-in tree shaking support can achieve this, at least partially, using this plugin.

### Styles import

Despite native tree shaking support, we didn’t drop babel-plugin-import and continue to use it to automatically get styles of a component when we import its js-code. Using it, no excess styles are added to the bundle, and developers don’t need to think about style dependencies. Now, it’s impossible to forget to import the styles of some components.

The plugin is enabled in the babel.config.js file in the root of the project:

{% gist https://gist.github.com/ikopenkov/4fbdb48e4533e33781490ff95f3b78fb %}

### Moment.js

At this time, the bundle consists of the following modules:

![](https://dev-to-uploads.s3.amazonaws.com/i/g36tki2ha7r7wpmsdr4m.png)

Ant Design uses Moment.js, which pulls all the localization files it has to the bundle. You can see in the image how dramatically it increases the size of the bundle. If you don’t need such components depending on Moment.js, such as DatePicker, you can simply cut this library, for example, by adding an alias for Moment.js to some empty file.

As we’re still using Moment.js for our projects (ignoring the fact that its creators have recently deprecated it 😅), we didn’t need to fully eliminate it. We just excluded useless localization files from adding to the bundle, leaving only supported languages (en and ru).

It became possible thanks to ContextReplacementPlugin, delivered with Webpack:

{% gist https://gist.github.com/ikopenkov/b622da07115e05327bdb41abe0c536de %}

Now we can make sure that redundant files are eliminated, as in the next screenshot:

![](https://dev-to-uploads.s3.amazonaws.com/i/92eu59ig05vkx2uwhc76.png)
> If you use Lodash and/or Ramda and want to exclude their unused files from the bundle, but at the same time you don’t want to import every function from their separate files, you can just add to your Babel config [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash) and [babel-plugin-ramda](https://github.com/megawac/babel-plugin-ramda).

### Icons

Webpack Bundle Analyzer screenshots above show that the heaviest part of the bundle is the Ant Design built-in icon set. This happens because Ant Design exports icons from a single file.

We use unique custom icons in our projects, so we don’t need this file at all. You can cut it off, as well as Moment.js, just by making an alias to some empty file. However, I want to illustrate the ability to save only the required default icons if you want to use them.

For that reason, I added the file src/antd/components/Icons.tsx. I left there only the Spinner icon to render a button in state "loading":

{% gist https://gist.github.com/ikopenkov/f9de2e93e2245ea0afb899cd49ce7801 %}

I also added an alias to this file into the Webpack config.

{% gist https://gist.github.com/ikopenkov/3387634610ce7382528fb1015269acf8 %}

And now we just need to render the button itself:

{% gist https://gist.github.com/ikopenkov/3cb2df1469e88a28ec654b8531af6c15 %}

As a result, we get the bundle with only the one icon we used instead of getting the full pack of icons as before:

![](https://dev-to-uploads.s3.amazonaws.com/i/l0i0z97nwyrjc4hnvhxt.png)

Optionally, you can easily replace default icons with standard ones using the same file we’ve just created.

## Conclusion

Finally, every unused component of Ant Design has been cut off by Webpack. At the same time, we continue to import any component, whether it is a wrapper or an original one, from the root of the library.

Moreover, during development, TypeScript will show proper types for customized components as it was with Button from the example above, for which we added the additional property tooltipTitle.

If we decide to customize another component in the project, even a widely used one, we will just need to add a file with the wrapper and change the path of that component in the file with re-exports located at src/components/antd/index.ts.

We’ve been using this approach for more than a year in two different projects, and we still haven’t found any flaws.
___
You can see the ready-to-use boilerplate with a prototype of this approach and the examples described in this article [in my repository](https://github.com/ikopenkov/ant-customization). Along with this solution, we test our components using Jest and React Testing Library. This will be addressed in a different post, as it includes a few tricky elements.