developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8k stars 361 forks source link

Tree unshaken export #985

Closed dawsbot closed 1 year ago

dawsbot commented 1 year ago

I'm moving from using tsc as my entire build system to microbundle, but found that my bundle is nearly 2x the size now.

It appears the exports now are not conforming to module imports properly because each exported function now has the full file size:

before

Screen Shot 2022-08-05 at 11 00 51 PM

after

Screen Shot 2022-08-05 at 11 00 26 PM

This is an open PR currently, so seeing how I did this is quite easy: https://github.com/Earnifi/essential-eth/pull/154/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R48

One thing I noticed is that the previous tsc build would output all the files "unbundled", so heavy imports were able to be tree-shaken from functions which do not use them. I believe the since-file export from microbundle is why every exported function now has the max size. Do you know why this might be @rschristian ?

rschristian commented 1 year ago

Are you using bundlephobia to support the "2x the size" claim, or are you looking at the output files themeselves?

Bundlephobia has a lot off issues and I wouldn't trust its numbers much these days. I'd recommend https://bundle.js.org/ in its place.

so heavy imports were able to be tree-shaken from functions which do not use them.

Any decent bundler will continue to do dead code elimination, even if the module is just a single file.

dawsbot commented 1 year ago

@rschristian superb, you're right on this after testing my build.

Do you know bunle size test tool that DOES support granular exports like bundlephobia? Seems like bundlephobia is still used often online for something that is not up-to-date 🙏

rschristian commented 1 year ago

As I wrote, https://bundle.js.org/ is best and you can use it to test individual exports.