Closed DavidN-Work closed 1 month ago
Thank you for bringing this issue to our attention! I want to assure you that I will take a look as soon as I can. Your patience and understanding in this matter is greatly appreciated.
Hey :)
Your setup is just ever so slightly incorrect. You only need to add
"module": "esnext",
"target": "es6",
to the tsconfig.app.json#compilerOptions
as seen in this repo: https://github.com/Coly010/tree-shaking-test
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.
Current Behavior
I'm encountering an issue with tree shaking in an NX workspace where unused exports are still being included in the final bundle. Despite following best practices and ensuring the correct configurations, unused functions are not being properly excluded.
Expected Behavior
Only the functions actually used from JS libraries should be included in the final bundle, while unused functions should be excluded.
GitHub Repo
No response
Steps to Reproduce
npx create-nx-workspace@latest tree-shaking-test --app --pm pnpm --interactive=false --preset=ts --workspaceType=integrated --nx-cloud=skip
nx add @nx/express
nx g @nx/express:app my-express-api --directory=apps/my-express-api
nx g @nx/js:lib my-lib --directory=libs/my-lib
lib/my-lib/src/lib
like so:libs/my-lib/src
export all three functions like so:webpack.config.js
and addusedExport: true
andsideEffects: true
:Add
"sideEffects": false
to the package.json of the my-lib projectUpdate the
tsconfig.base.json
Run
nx run-many -t build
Look at the
main.js
file in the dist and you'll see that it includes unused functions.Nx Report
Failure Logs
No response
Package Manager Version
pnpm 8.15.8
Operating System
Additional Information
This is my first time using NX so I'm not sure what the normal expected behaviour is but it looks like this issue appeared in #9717 and #3069. But was fixed in #17599.
The only way to fix the issue is with:
Using the following:
Yielded mixed results, with a success rate of about 50%. This might be due to my TS Server.