evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.16k stars 1.15k forks source link

Question on tree shaking dependencies #3969

Open wernst opened 2 hours ago

wernst commented 2 hours ago

This may be more a question than an issue, because esbuild is probably working as intended. I am bundling a file that imports a dependent library that uses bigint and exports that code. The code being imported and used does not use bigint. The target es2015 doenst support bigint and I am hoping that the dependency can be tree shaken so the bigint uses arent a part of the bundle. However esbuild does appear at some point to be pulling in bigint references and failing to build due to the target environment. Should I expect esbuild's tree shaking to help with this?

Reproduction: https://github.com/wernst/esbuild-tree-shake-reproduction

More specifics: I am trying to bundle code to run in K6 (https://k6.io/), which uses a js engine written in Go goja (https://github.com/dop251/goja). From what i can tell, es2015 is a fairly safe target for this engine. However the code i am bundling has a dependency on remeda (https://github.com/remeda/remeda/), which uses bigint. According to their readme the library should play nicely with common bundler tree shaking implementations.

My current workaround is to patch remeda's package exports field to allow imports like import {doNothing} from 'remeda/doNothing with a wildcard entry point (https://nodejs.org/docs/latest-v18.x/api/packages.html#package-entry-points), but id rather not patch modules.

wernst commented 2 hours ago

Related remeda issue: https://github.com/remeda/remeda/issues/916