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

[Suggestion] Option to mangle only private properties #5960

Open
AshleyScirra opened this issue Nov 5, 2024 · 0 comments
Open

[Suggestion] Option to mangle only private properties #5960

AshleyScirra opened this issue Nov 5, 2024 · 0 comments

Comments

@AshleyScirra
Copy link
Contributor

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants