Skip to content

Commit 5ab2a21

Browse files
committed
add support to specify mangled name (mishoo#5815)
1 parent f0ca9cf commit 5ab2a21

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,15 @@ discarded by the compressor as not referenced.
10291029
The safest comments where to place copyright information (or other info that
10301030
needs to be kept in the output) are comments attached to toplevel nodes.
10311031

1032+
### Specify preferred mangled name in comment annotations
1033+
1034+
`/*@mangleTo:X*/` or `/*@mangleTo:X*/` comments allow you to choose the name.
1035+
1036+
```javascript
1037+
(function(one /*#mangleTo:H*/, two /*#mangleTo:i*/) { /*..*/ })(1, 2);
1038+
// results (function(H,i){ /*..*/ )(1,2);
1039+
```
1040+
10321041
### The `unsafe` `compress` option
10331042

10341043
It enables some transformations that *might* break code logic in certain

lib/scope.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,13 @@ function next_mangled_name(def, options) {
565565
scopes.push(scope);
566566
} while (scope = scope.parent_scope);
567567
});
568-
var name;
568+
var comment = def.orig[0].start.comments_after[0]?.value;
569+
var preferred_name = comment && /[@#]mangleTo:(.*)/.exec(comment);
570+
var name = preferred_name && preferred_name[1];
571+
if (name && !names.has(name)) {
572+
in_use.set(name, true);
573+
return name;
574+
}
569575
for (var i = 0; i < holes.length; i++) {
570576
name = base54(holes[i]);
571577
if (names.has(name)) continue;

test/compress/keep_fargs.js

+14
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,20 @@ function_name_mangle: {
965965
expect_stdout: "function"
966966
}
967967

968+
function_name_mangle_from_preferred_comment: {
969+
options = {
970+
keep_fargs: false,
971+
}
972+
mangle = {}
973+
input: {
974+
(function(one /*#mangleTo:W*/, two) {
975+
console.log(one, two);
976+
})(1, 2);
977+
}
978+
expect_exact: "(function(W,o){console.log(W,o)})(1,2);"
979+
expect_stdout: "1 2"
980+
}
981+
968982
function_name_mangle_ie8: {
969983
options = {
970984
keep_fargs: false,

0 commit comments

Comments
 (0)