okikio / bundlejs

An online tool to quickly bundle & minify your projects, while viewing the compressed gzip/brotli bundle size, all running locally on your browser.
https://bundlejs.com
MIT License
751 stars 13 forks source link

BUG: Treeshake doesn't work for badges #51

Closed thoughtspile closed 1 year ago

thoughtspile commented 1 year ago

Looks like badges ignore treeshake option. Example with date-fns, trying to treeshake isAfter:

  1. Bundlejs website: 7.27KB (gzip)
  2. Badge: (link)
  3. Full date-fns on website: 22.83KB (gzip)

Moreover, local echo 'export { isAfter } from "date-fns";' | npx esbuild --bundle --minify | gzip -c | wc -c gives 541 bytes, maybe related to #43

okikio commented 1 year ago

@thoughtspile Sorry, for the delay, but after a through analysis of the bundl, a deconstruction and re-write of the virtual filesystem of bundlejs, it seems it keeps trying to bundle in the parser, and I can't quite figure out why.

It doesn't really make any sense as to why it keeps trying to bundle the parser, this might be an esbuild-wasm bug of some sort, it's super inconsistent, and due to the lack of an AST it's really difficult to debug where it's messing up

okikio commented 1 year ago

After a couple more experiments it seems for treeshaking to work in an ideal way it needs to have all the files it needs to traverse up front, the problem is that is double the build time versus just bundling it once.

I've setup a seperate branch with the treeshaking exeriments https://bundle-git-treeshaking-fix-okikio-dev.vercel.app/?q=date-fns&treeshake=%5B%7B+isAfter+%7D%5D&config=%7B%22esbuild%22%3A%7B%22minify%22%3Afalse%7D%7D

okikio commented 1 year ago

Good news, I seem to have a permanent solution to the problem of poor treeshaking. It seems this issue is caused by esbuild being unable to determine if it should include sideEffects or not.

On a local file system esbuild can easily just look up the package.json files but it can't quite do that when using a virtual fs, so the solution is to just let esbuild know to use sideEffects if it's stated in the package.json file

thoughtspile commented 1 year ago

Hi again my friend @okikio! Got a chance to roll out the fix? Still seeing odd sizes in badges:

Снимок экрана 2023-07-02 в 20 38 25
okikio commented 1 year ago

I was able to fix it, I just hadn't found a chance to publish it. This weekend is a little hectic, I should be able to get the fix out by Tuesday or Wednesday this week

okikio commented 1 year ago

The API hasn't been fixed yet, but here is the newly improved treeshaking

CleanShot 2023-07-02 at 21 44 08@2x
okikio commented 1 year ago

The API is now fixed as well. Treeshaking should now work properly across the website and API. I'll keep this issue open, please let me know if you notice any issues https://bundlejs.com/?q=date-fns&treeshake=%5B%7B+isAfter+%7D%5D&bundle

CleanShot 2023-07-03 at 00 15 37@2x
okikio commented 1 year ago

@thoughtspile Does the fix work for you?

okikio commented 1 year ago

Since the fix seems to work ill close this issue

thoughtspile commented 1 year ago

Thanks @okikio, works much better now! I'm seeing some other suspicious behavior, let me open another issue

okikio commented 1 year ago

Ok