-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tree shaking problem (React Application and Library) #3069
Comments
run on latest @nrwl/nx 9.3.0 |
Hi, thank you for the details to reproduce. It would help us help you if you went 1 step further and provided a repo we could pull down and see the issue! Could you please provide a repo if you have time? |
Thanks for your response @FrozenPandaz i create a repo you can pull it here : on this repo : 2 main apps :
2 libs :
on libs (ui) entry file index.js
information
this below image if we commnet nxModal on libs ui entry file index.js. this below image if we uncommnet nxModal on libs ui entry file index.js.
|
@conioX Did you found a solution? |
I'm also experience the same problem in my monorepo. |
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. |
Any updates on this? Before we begin to utilize a lot more libs in our monorepo which has many advantages, this is a good thing to have clarified so we know how best to architect it. We are using React applications and pure typescript libraries on the latest NX. |
Can agree but we are using angular, we just figured out that we have the same problem. Instead of using the index.ts in the tsconfig.base.json you can add all the index.ts files there e.g:
Change to:
you still can use the import as before. Try it out ;) for us, it was working. |
Any news about that? We should get that out of the box without any workaround |
I don’t think this is exclusively an Nx issue but rather a Webpack issue that they’ve tried addressing in Webpack 5: https://github.com/webpack/changelog-v5#nested-tree-shaking There’s also a Next.js issue tracking this: vercel/next.js#12557 We’re a little behind in our Nx upgrades so I can’t try webpack 5 just yet, but that would my first port of call. Otherwise it looks like removing re-exports is the naive way to go. |
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. |
@ZackDeRose can I ask what your webpack config looks like? I can't seem to get this to work. Everything that's exported from |
@MitkoTschimev I'm facing the exact same issue unfortunately :( I've tried your solution with great hopes, but it didn't work as it breaks my compilation saying it could find things from the modules. Example:
Do you have anything more specific in your solution that could help me out? 🙏 |
Hi @maxime1992, unfortunately it was working for the beginning but as more and more the project was growing with more libs it was not anymore working as in the beginning so we removed them back and waiting for the october release with webpack 5 support. It is still ok if you cut your libs all in small peaces. So the recommendation is to use domain driven development to keep it them as small as possible and getting a rocket after webpack 5 and tree shaking is doing their job :D |
I migrated my workspace to the latest, but still, I am facing the issue of bundling unused code. Can anyone help? |
Still present in v13.2.3 with Webpack 5 |
I ended up fixing it by modifying my imports. I am not adding entries in index.ts anymore instead directly import the desired component. The drawback is I can't use destructured imports but this solved the bundling issue. |
Wow, this is critical to discover late. I assumed importing one item would not involve the whole lib being imported into my next.js project. |
We've had luck in webpack 4 with the
This was with a custom app using runCommands tho, so not using any out-of-the-box NX webpack. |
any update on this? So far I've had no luck with this on v13.3.7 with webpack 5. I've tried the |
What I was doing is have multiple components in shared lib and exporting all of them from |
@asit-prakash I've tried that and it does work but its a hard sell to my colleagues for NX if this is the default workaround. What I'd like to understand is why even though webpack5 does indeed support nested tree shaking https://webpack.js.org/blog/2020-10-10-webpack-5-release/#nested-tree-shaking - it doesn't appear to work for this library. Is there something in the webpack scripts for NX that would cause this? |
How can I achieve this?
|
This happens to me too
This happens to me too. |
So the only solution I found was to not export the components from index.js rather import them like import componentName from components/componentName/componentName.tsx @asit-prakash After I added in tsConfig.base.json file, Getting an error that, component is not exported. |
I'm in the same situation here. |
Not to beat a dead horse, but I'm running into this as well on nx v14. #3426 was an interesting read on the pros/cons of barrel files. I'm now questioning our use of them, despite their convenience. We have a react component component library with ~25 components exported in the barrel file. |
Facing the same One way around i am doing is using '*' in paths under
This saves you from writing each and every module in path list |
@FrozenPandaz and team - this is a pretty big issue with NX, can we get some traction towards a resolution that doesn't rely on workarounds such as the above? |
Same problem for me, Im mimgrating from turbo and nx increases the bundle size 10x in my production builds. |
Same problem here, bundle sizes are sky rocketing... |
Hey NX team / @FrozenPandaz , kindly re-open the issue as there is no ideal solution found to this problem yet and many clients are reporting the same. Also do we have it on the roadmap? It directly affects performance which is critical to many organizations and needs to be addressed soon. |
This still seems to be an issue. Created a new workspace with a Next.js project, and created a shared UI library with some components. Exporting via |
Same issue here |
Same as above. What I've tried with limited success is using a babel plugin to transform named imports. Adding these lines to {
// ...
"plugins": [
// ...
[
// plugin name
'babel-plugin-import',
// plugin options
{
libraryName: '@finmid/ui',
libraryDirectory: '',
camel2DashComponentName: false,
},
// plugin instance name, should be unique for each library
'ui-components',
]
]
} In case you use TypeScript, you should add {
"compilerOptions": {
"paths": {
"@finmid/ui": ["libs/ui/src/index.ts"], // that's original line
"@finmid/ui/*": ["libs/ui/src/*"], // <--
}
},
} I don't like this solution:
|
Actually experiencing the same problem, does some of you guys found an alternative? |
Is this still a valid workaround? I tried to implement it today, but the imports are then partly nicjht more found. What I try to export are not only modules, but also e.g. models, which then have their own entrypoint (index.ts). @MitkoTschimev |
I've found a potential fix. See this answer which might resolve the issue of Webpack not properly tree shaking barrel files |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
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)
main.js (main apps)
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
problem with ui library
index.js (ui library )
Button.js
Modal.js
Main.js
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
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
The text was updated successfully, but these errors were encountered: