haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[js] Top-level mutation of every class prevents tree-shaking

Open singpolyma opened this issue 1 month ago • 2 comments

I am working on a library written in Haxe and compiled to JS. I have everything working pretty well but have run into this. The generated code for a class (even in ES6 mode) always looks like:

class Thing {
}
Thing.__name__ = "Thing";
Object.assign(Thing.prototype, {
	__class__: Thing
	,thing: null
});

This top-level mutation prevents tree shaking of the class no matter how I set up my imports. removing these lines after the class fixes it for me but of course I'm not sure that safe to do. It seems like this should be equivalent to:

class Thing {
    static __name__ = "Thing";
    __class__ = Thing;
    thing = null;
}

perhaps ES6 mode could generate this modern syntax instead?

singpolyma avatar Nov 20 '25 05:11 singpolyma

Static fields supported in js since ES2022. Is setting Class.__name__ = ...; after class declaration will support tree shaking? Not sure how it will affect initialization order and __init__ stuff / static variable access tho.

I think i can add js_es=2022 and other year values support for future generator features.

RblSb avatar Nov 24 '25 08:11 RblSb

Setting anything after class declaration is a "mutation" which breaks tree shaking. Having it as a js_es=2022 or similar sounds like a great solution.

singpolyma avatar Nov 24 '25 16:11 singpolyma