egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.95k stars 217 forks source link

Is tree shaking possible when building a lib with tsup? #578

Open zchenwei opened 2 years ago

zchenwei commented 2 years ago

I know tsup is powered by esbuild which supports tree shaking by default. However, I don't see much details about this part on the doc sites. So I was wondering if tsup inherits this good nature and what I can reference to make a lib bundled with tsup tree shakable. Thanks!

Upvote & Fund

Fund with Polar

eric-burel commented 2 years ago

@zchenwei by any chance did you progress on this? I use Tsup to build a script and end up with a lot of code, it's even marked as not used when I open the file in VS Code. However, Esbuild indeed enables tree-shaking by default in bundle mode, so it's maybe more an issue with the code. There are not many documentation on how to debug tree-shaking to detect where the code is no correctly seen as "pure" and tree-shaking gets disabled.

egoist commented 2 years ago

@eric-burel check out the new treeshake option if you don't mind being a little bit slower XD https://tsup.egoist.sh/#tree-shaking

eric-burel commented 2 years ago

Thanks for this very fast answer!

It seems to work better, any idea why? Also tsup-nodeseems removed from v6 but is still documented, is there a replacement for it?

No problem with it being slower I only build TypeScript scripts this way, not full apps.

Edit: so treeshake is fixed! but I really need tsup-node otherwise the script built doesn't work

egoist commented 2 years ago

@eric-burel sorry about that, tsup-node is back in 6.0.1, I accidentally deleted it 🥲

eric-burel commented 2 years ago

It seems that overall the tree shaking is better, even without --treeshake actually, just by updating Tsup. My initial issue was next/router leaking in the script and this already disappear just after updating tsup to v6.

Adding --treeshake will also remove a few unused imports.

Did you change something between v5/v6? Maybe updating esbuild version?

Cannot thank you enough, this helps me having a pattern to run startup scripts in serverless frameworks such as Next. In a serverless logic, you need to run some logic on "post build", instead of running on "server startup" as you do with a long-running server, so it becomes critical to be able to build scripts that work outside of the Next.js app.

egoist commented 2 years ago

Did you change something between v5/v6? Maybe updating esbuild version?

no, it's been 0.14.25 for quite a while.

egoist commented 2 years ago

it's probably because --shims is not enabled by default now 🤔

zchenwei commented 2 years ago

https://tsup.egoist.sh/#tree-shaking

@eric-burel l switch to use Rollup for bundling amplify ui library because our goal is not only to tree shake the library itself, but more importantly, to make sure the library is tree-shakeable for our users when bundling an App. Rollup has a very good option preserveModule to help us achieve the goal versus bundling a whole bunch of things into a single file, which is not good for bundler to do code analysis and dead code detection. @egoist I like the simplicity in tsup and hope we can have an option similar to that where the module structure can be well preserved :)

eric-burel commented 2 years ago

@zchenwei Thanks for the insights, I might need that as I was disappointed that our bundle went bigger with ESM instead of smaller...

I guess we would simply need more access to rollup config for advanced customization? Currently it's only quoted in the Tree shaking doc.

@egoist I couldn't find any documentation for this option though, only in webpack. I guess I still have some global variables and that's my initial issue (typically accessing process.env to get some value globally instead of wrapping that into a function) , it's hard to spot them in the codebase. Anyway the build sounds better now so great.

chrislambe commented 1 year ago

@eric-burel check out the new treeshake option if you don't mind being a little bit slower XD [redacted]

@egoist the redacted URL in this comment seems to redirect through to a malware site (survey-smiles dot com). Might be best to scrub that link so other people don't get caught by it.

jly36963 commented 11 months ago

Updated link to tsup tree shake docs