Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unreachable switch cases are not removed when switch value is known #4028

Closed
TheHolyWaffle opened this issue Jan 7, 2025 · 3 comments
Closed

Comments

@TheHolyWaffle
Copy link

TheHolyWaffle commented Jan 7, 2025

When the value of a switch case is hardcoded, either literally in code or through define option, esbuild does not treeshake the unreachable switch cases.

Reproduction 0.24.2: https://esbuild.github.io/try/#dAAwLjI0LjIALS1taW5pZnkgAHN3aXRjaCAoImEiKSB7CiAgICBjYXNlICJhIjogewogICAgICBjb25zb2xlLmxvZygiYSIpOwogICAgfQogICAgY2FzZSAiYiI6IHsKICAgICAgY29uc29sZS5sb2coImIiKTsKICAgIH0KICB9

Expected output after minification:

console.log("a")
@evanw
Copy link
Owner

evanw commented Jan 9, 2025

Thanks for the suggestion. I agree that this is something esbuild should be doing, as it's an easy win that could potentially lead to large size savings. This should also be added to esbuild's dead code analysis, which happens even without minification enabled (and which prunes edges in the module graph so they aren't even traversed in the first place during bundling).

@niedzielski
Copy link

as near as I can tell, this issue is still present in v0.25.0 both in the original report and in build mode.

@evanw
Copy link
Owner

evanw commented Feb 17, 2025

Your code example is missing a break. The new minification logic doesn’t handle complex fall-through cases.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants