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

Ensuring the mangler doesn't try to mangle constructors #1976

Closed
fabiospampinato opened this issue Feb 1, 2022 · 2 comments
Closed

Ensuring the mangler doesn't try to mangle constructors #1976

fabiospampinato opened this issue Feb 1, 2022 · 2 comments

Comments

@fabiospampinato
Copy link

Mangling the following code:

class Foo {}

class Bar extends Foo {
  constructor () {
    super ();
  }
}

With this command:

esbuild '--mangle-props=.' ./foo.js

Generates this error:

✘ [ERROR] Unexpected "super"

    foo.js:5:4:
      5 │     super ();
        ╵     ~~~~~

IMO super calls in constructors should be automatically detected and ignored by the mangler.

@evanw
Copy link
Owner

evanw commented Feb 1, 2022

Thanks for the report. It looks like this happens because --mangle-props=. causes constructor to be renamed to something else and then it's no longer a constructor. I will add a workaround for this.

For what it's worth I recommend not doing --mangle-props=. as you will have to manually add every single API you use as a reserved property or your code will be broken. I recommend instead using a common pattern for all properties you intend to be mangled that is unlikely to appear in the APIs you use (i.e. opt-in instead of opt-out). The reserved property setting is mainly intended as a last-resort override that may be necessary if code in a dependency you don't control happens to use a handful of names similar to the ones you want to use.

@fabiospampinato
Copy link
Author

Thanks for the report. It looks like this happens because --mangle-props=. causes constructor to be renamed to something else and then it's no longer a constructor. I will add a workaround for this.

I see, now I understand the error message.

For what it's worth I recommend not doing --mangle-props=. as you will have to manually add every single API you use as a reserved property or your code will be broken. I recommend instead using a common pattern for all properties you intend to be mangled that is unlikely to appear in the APIs you use (i.e. opt-in instead of opt-out). The reserved property setting is mainly intended as a last-resort override that may be necessary if code in a dependency you don't control happens to use a handful of names similar to the ones you want to use.

I have a fairly narrow use case for this, a tiny library for which it's obvious what the public methods are and all of them are unit-tested, mangling everything except those methods seems like the best option for my use case, prefixing all the non-public stuff with _ or something would only make the code a bit uglier I think.

@fabiospampinato fabiospampinato changed the title Ensuring the mangler doesn't try to mangle super () calls in constructors Ensuring the mangler doesn't try to mangle constructors Feb 1, 2022
@evanw evanw closed this as completed in 3dd6c57 Feb 1, 2022
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