Skip to content
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

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

Open
TheHolyWaffle opened this issue Jan 7, 2025 · 1 comment
Open

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).

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

No branches or pull requests

2 participants