You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unwrapping static switch cases with --minify-syntax #4176
When a switch statement can be statically analyzed to have only one branch (because of a defined value, a resolved typeof, or any other static switch value), it should be unwrapped instead of remaining as a useless wrapper.
for example here is a polymorphic function that resolves an object differently based on the type of the first parameter : in esbuild playground
source for the minimal reproduction
functionresolveSource(target){switch('string'/* infered by TS from "typeof target" */){case'string':
case'symbol': {console.log('service id syntax');return{};}case'function': {console.log('target getter syntax');return{};}case'object': {console.log('direct target syntax');return{};}default: {if(import.meta.env.DEV){warn(newTypeError(`incorrect target provided to onEvent, typeof target === ${typeoftarget}, expected string, symbol, function or object`));}return{};}}}resolveSource("service");
When built with --minify-syntax, only the "string" branch remains :
functionresolveSource(e){switch("string"){case"string":
case"symbol":
returnconsole.log("service id syntax"),{};}}
But it could be minified further, as only a single branch remains, and it has to be executed if the switch is evaluated, the body of the case should then be unwrapped to something like :
functionresolveSource(e){returnconsole.log("service id syntax"),{};}
terser however returns only console.log("service id syntax"); from the whole snippet (can't find a way to share from the REPL ). I don't think expecting esbuild to be that thorough would be any help as it's not esbuild's main concern tho.
Activity
hyrious commentedon May 7, 2025
Interestingly Rollup doesn't delete much code for this case.
Ragnar-Oock commentedon May 7, 2025
terser however returns only
console.log("service id syntax");from the whole snippet (can't find a way to share from the REPL ). I don't think expecting esbuild to be that thorough would be any help as it's not esbuild's main concern tho.fix evanw#4176: improve switch statement partial-eval
Update dependency esbuild to v0.28.2 (#2)