Skip to content

[Suggestion] Option to mangle only private properties #5960

@AshleyScirra

Description

@AshleyScirra

This is not a bug, just a suggestion for a (hopefully) straightforward but potentially useful feature. This is a kind of a follow-on from #5937.

Uglify version (uglifyjs -V) 3.19.3

Proposal

Property mangling is not safe to run on all JavaScript code: things like dynamic string property accesses can cause code to be broken after property mangling. Sometimes code has to be specially written to ensure it is not broken by property mangling. Other optimizations like renaming local variables are always safe since they cannot be dynamically referenced by string.

As noted in #5937 the restrictions of private properties - notably that they cannot be accessed dynamically by string, and cannot have name collisions even with base classes - means that just like with local variables, it is actually always safe to mangle them. It is not possible for mangling private property names to break code, at least with the current features of JavaScript.

If you have a large amount of third-party JavaScript code to minify and you don't know if it supports property mangling, it should still always be safe to mangle only private properties. Therefore it would be useful to have a setting that allows UglifyJS to mangle only private properties without mangling any other kinds of properties.

JavaScript demonstration code

Properties not mangled at all (always safe)

class Test {
    foo;
    #bar;
}

Property mangling enabled (may break some JavaScript code)

class Test {
    a;
    #a;
}

Only private property mangling enabled - new proposed mode which is also always safe

class Test {
    foo;
    #a;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions