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
When given a define, esbuild is able to treeshake some known dead code. This does not work for class methods unless the define is tested on the same line.
The difference you noticed is because esbuild doesn't attempt to tree-shake class methods, but it does attempt to tree-shake function declarations. Tree-shaking class methods is technically possible in a very limited number of situations, but the analysis would quickly fall apart as JavaScript is a very dynamic language, methods are properties on objects, and guaranteed analysis of dead properties is not generally possible (e.g. if you do console.log(new C) that could access init).
That said, you're right that this should work. Right now the dead code recognizer eliminates code inside of if/else branches that are known to not be taken, but does not currently consider the code past an if+return to be dead code. A full solution here would require control flow analysis that esbuild doesn't currently do, but special-casing if+return in the same block is easy enough.
When given a define, esbuild is able to treeshake some known dead code. This does not work for class methods unless the define is tested on the same line.
Produces
function i(){console.log("b")}var o=class{init(){}};new o;
when no logs were expectedvar o=class{init(){}};new o;
.It seems to be method specific. This code shakes fine:
Playground
The text was updated successfully, but these errors were encountered: