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

add support to specify mangled name (#5815) #5816

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,15 @@ discarded by the compressor as not referenced.
The safest comments where to place copyright information (or other info that
needs to be kept in the output) are comments attached to toplevel nodes.

### Specify preferred mangled name in comment annotations

`/*@mangleTo:X*/` or `/*@mangleTo:X*/` comments allow you to choose the name.

```javascript
(function(one /*#mangleTo:H*/, two /*#mangleTo:i*/) { /*..*/ })(1, 2);
// results (function(H,i){ /*..*/ )(1,2);
```

### The `unsafe` `compress` option

It enables some transformations that *might* break code logic in certain
Expand Down
8 changes: 7 additions & 1 deletion lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,13 @@ function next_mangled_name(def, options) {
scopes.push(scope);
} while (scope = scope.parent_scope);
});
var name;
var comment = def.orig[0].start.comments_after[0]?.value;
var preferred_name = comment && /[@#]mangleTo:(.*)/.exec(comment);
var name = preferred_name && preferred_name[1];
if (name && !names.has(name)) {
in_use.set(name, true);
return name;
}
for (var i = 0; i < holes.length; i++) {
name = base54(holes[i]);
if (names.has(name)) continue;
Expand Down
14 changes: 14 additions & 0 deletions test/compress/keep_fargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,20 @@ function_name_mangle: {
expect_stdout: "function"
}

function_name_mangle_from_preferred_comment: {
options = {
keep_fargs: false,
}
mangle = {}
input: {
(function(one /*#mangleTo:W*/, two) {
console.log(one, two);
})(1, 2);
}
expect_exact: "(function(W,o){console.log(W,o)})(1,2);"
expect_stdout: "1 2"
}

function_name_mangle_ie8: {
options = {
keep_fargs: false,
Expand Down