Skip to content

Mangle class method names #9626

@leafypout

Description

@leafypout

Describe the feature

I was curious if a PR for mangling class method names would be accepted. This would be a (likely optional) feature that could be enabled in the minifier that would mangle method names in classes and interfaces (e.g. add() {} -> a() {}). The goal of this obviously would be to reduce output size of generated code after minification for code where private/non-exported classes and interfaces are used.

As far as I'm aware, the side effects of doing this are:

  • Extending from another class (class a extends b)
  • Inheriting an interface (class a implements b) (TypeScript)
  • Instance of class passed into a function like JSON.stringify
  • Instance of class parsed from a string using a function like JSON.parse
  • Class/interface being exported (shouldn't be an issue, since the mangler already doesn't rename identifiers that are potentially exported)

I'm not sure if we'd be interested in doing this, I'd be down to look into it if maintainers would be interested in having this as a feature, as it's a feature I'm interested in having available.

Also, side note: currently, the mangler doesn't check if an object or class is used in functions like JSON.stringify, so I think it would be good to at least look into adding detection for this so users can have this enabled without having to disable class property name mangling entirely. (I'm pretty sure you can filter out certain properties in the config, but I imagine this can become troublesome when dealing with large files)

I did try find if other minifiers do this, I don't believe terser does. I also checked Google Closure Compiler and it doesn't seem to either.

Babel plugin or link to the feature description

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions