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

Feature Request: Annotate keys in __publicField() for mangling compatibility #4034

Open
mckravchyk opened this issue Jan 11, 2025 · 2 comments

Comments

@mckravchyk
Copy link

mckravchyk commented Jan 11, 2025

When targeting ES2017, ESBuild will output public class fields like:

__publicField(this, "propName")

I am using Terser for post-processing with property mangling by Regex enabled. Terser is not able to recognise the string in the function argument as a property to mangle, which makes it incompatible.

The same could be achieved with:

__publicField(this, { propName: '' })

at a small cost of being more verbose and some runtime footprint to get Object.keys(arg)[0] It could be an option / not enabled by default.

Alternatively, is there a reason why Object.defineProperty is not used? This is something that Terser makes an exception for, TypeScript also outputs JS this way.

@evanw
Copy link
Owner

evanw commented Jan 13, 2025

I think the missing thing for Terser (and for subsequent esbuild runs with --mangle-props) is the addition of a /* @__KEY__ */ comment to annotate the string literals as property keys. Both Terser and esbuild already have a feature where you can annotate strings with /* @__KEY__ */. That change would look like this:

 var __defProp = Object.defineProperty;
 var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
 var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
 class Foo {
   constructor(it) {
-    __publicField(this, "foo", 123);
-    __publicField(this, "bar");
+    __publicField(this, /* @__KEY__ */ "foo", 123);
+    __publicField(this, /* @__KEY__ */ "bar");
     this.bar = it;
   }
 }
 const { foo, bar } = new Foo(null);
 console.log(foo, bar);

Alternatively, is there a reason why Object.defineProperty is not used? This is something that Terser makes an exception for, TypeScript also outputs JS this way.

Calling Object.defineProperty instead of using a helper function has non-trivial run-time overhead. It makes the generated code both download and run more slowly.

Edit: Compare without the comment to with the comment (the original code is here).

@mckravchyk
Copy link
Author

Thanks! Yes, I confirm Terser handles the annotation.

@mckravchyk mckravchyk changed the title Feature Request: Output __publicField without string literal for property mangling compatibility Feature Request: Annotate keys in __publicField() for mangling compatibility Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants