-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dead code removal #2006
Comments
The bundling pipeline is designed for speed, not for optimal output. It currently looks like this:
As you can see, the cross-module inlining happens during printing which is very late in the pipeline. It's after tree shaking so it won't be able to affect tree shaking. It's basically a specialized hack and isn't a general-purpose approach to optimization. So for example while What you're asking for is not a good fit for esbuild's pipeline as it currently exists. It's possible to build a tool that does what you're asking for while still being fast, but it would require a different architecture which would involve overhauling most of esbuild, which is likely just not going to happen. If you want esbuild to do what you're asking, then you could either:
This actually happens during the first stage when the file is being parsed and minified. At that point esbuild doesn't know anything about |
Thanks a lot for the explanation. Using |
hmm, kind of hope that it would eliminate code such as: false ? console.log(1) : console.log(2) or const isDev = false
isDev ? console.log(1) : console.log(2) but it didn't |
Hmm, you where right... i was more looking at why some part of my code was still there. function deadCode () {
console.log('dead code')
}
function isUsed () {
console.log('hello world')
}
false ? deadCode() : isUsed()
export {} i expected it's more or less transformed to: // unused
function deadCode () {
console.log('dead code')
}
function isUsed () {
console.log('hello world')
}
isUsed()
export {} |
Here's an example of that working fine too: (link) If you don't enable bundling then esbuild builds in a mode where by default it assumes that you might append additional code to its output (i.e. that the input is a partial module instead of a complete module) and that additional code might need to reference top-level variables in the input. This assumption was added because people actually do this, and so esbuild stripping dead code by default might be unsafe. The |
I'm sorry I could not come up with a better title. I'm aware of #1317 and I think what I'm trying to achieve is very similar but it does not appear to properly remove the dead code but it gets very close.
I want to exclude certain functionality from a "free" version of my bundle in contrast to the paid "pro" version. This is controlled by
process.env.FLAVOR
which I've put inflavor.js
. The simplified demo looks like this:flavor.js
index.js
Using 0.14.21
Produces
So esbuild is smart enough to inline the const expression (
'pro' === 'pro'
) and even remove the entire if-block. But it was not smart enough to just remove the entireelse
branch instead of combining both branches. So I'm kind of impressed that it turned this into a singleconsole.log
, but the expected output would beThis test case produces the desired output:
If I understand your comment (#1981 (comment)) correctly, my
flavor.js
should work?It has nothing but a single const.
The text was updated successfully, but these errors were encountered: