question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tree shaking problem (React Application and Library)

See original GitHub issue

hi guys i have problem with tree shaking in nx,

problem with assets library

so i create an application let`s says its (main app) and i create a library called (assets)

index.js (assets library)

export { default as Logo} from './lib/Logo/';
export { default as Loading } from './lib/Loading/';

main.js (main apps)

import { Logo} from '@nx-test/assets';

export const Main= () => {
    return (
        <>
      <Logo/>
        </>
    );
};

export default Main;


nx build main --prod --buildLibsFromSource

when i bundle my main apps in dist/apps/main i have loading.(hash number).svg so why this loading get bundle?

and when i delete this code export { default as Loading } from './lib/Loading/';
on my assets index.js
loading.(hash number).svg not get bundle on dist folder

this problem also happen on my ui library and increase a lot of my bundle size

problem with ui library

index.js (ui library )

export { default as MyButton} from './lib/Button/Button';
export { default as MyModal } from './lib/Modal/Modal';

Button.js

import {Button} from 'antd'

export const MyButton =() =>{
    return (
        <Button/>
    }
};

Modal.js

import {Modal} from 'antd'

export const MyModal =() =>{
    return (
        <Modal/>
    }
};

Main.js

import {MyButton} from '@nx-test/ui';

export const Main= () => {
    return (
        <>
      <MyButton/>
        </>
    );
};

export default Main;

as you can see its only import button on main app, but when i remove export Modal on my index.js (entry file ui library) and now its only import button

export { default as MyButton} from './lib/Button/Button';

bundle size decrease 30kb

so why its happen? its like nx keep bundle all the third party (ant design modal) not what im using which is only button

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:36 (5 by maintainers)

github_iconTop GitHub Comments

21reactions
4kochicommented, Sep 11, 2020

We have the same problem in our nx monorepo. So for example we have an UI lib with many components. Each component has it’s own module file. And all modules are exported through the index.ts file of the lib.

When I now have an app and only import one module from the UI lib, all modules from the lib are included in the bundle after the build, even for the production build.

So one solution is to add path mapping in the root tsconfig.base.json for each module from the lib. Then only the need module is included in the build.

But maybe there is a nicer way? My colleague told me for publishable libs ng-packagr would do the magic. But since we have a monorepo we do not have any publishable lib. It would be nice if there would be some mechanism like for instance in the Angular Material library, where you can also import each module on it’s own to keep the bundle size low.

12reactions
ZackDeRosecommented, Mar 25, 2021

Closing this issue as we believe this to be resolved when we upgrade to Webpack 5.

Please reopen if you you believe this is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree-Shaking Problems with Component Libraries - Medium
Tree -shaking isn't magic, and much like the garbage collector, you need to understand a little of what it's doing to get the...
Read more >
Enable Tree Shaking to Optimize Your React Component ...
Tree shaking, or “dead code removal”, is a technique that allows you to remove any unused code from a library. In order to...
Read more >
Why is my React component library not tree-shakable?
I found one possible solution to my problem. It has nothing to do with tree-shaking though. I'm simply splitting the library into several ......
Read more >
Tree-Shaking a React Component Library in Rollup
In this article, I will cover some of the pointers that I learnt about "Tree Shaking" the hard way, after countless trials and...
Read more >
How to write a tree-shakable component library
Tree Shaking in React How to write a tree-shakable component library · root ∟ packages ∟ app ∟ ui-library · import X from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found