Skip to content

Commit 96e5f90

Browse files
committed
Add prefix to property name after mangling
1 parent 7f8d72d commit 96e5f90

File tree

7 files changed

+142
-2
lines changed

7 files changed

+142
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ The available options are:
150150
notation. You can override these by setting
151151
them explicitly on the command line.
152152
--mangle-regex Only mangle property names matching the regex
153+
--mangle-props-prefix Prefix added to the property name after mangling
153154
--name-cache File to hold mangled names mappings
154155
--pure-funcs List of functions that can be safely removed if
155156
their return value is not used [array]

bin/uglifyjs

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ You need to pass an argument to this option to specify the name that your module
7474
.describe("reserve-domprops", "Make (most?) DOM properties reserved for --mangle-props")
7575
.describe("mangle-props", "Mangle property names (0 - disabled, 1 - mangle all properties, 2 - mangle unquoted properies)")
7676
.describe("mangle-regex", "Only mangle property names matching the regex")
77+
.describe("mangle-props-prefix", "Prefix added to the property name after mangling")
7778
.describe("name-cache", "File to hold mangled names mappings")
7879
.describe("pure-funcs", "List of functions that can be safely removed if their return value is not used")
7980
.describe("dump-spidermonkey-ast", "Dump SpiderMonkey AST to stdout.")
@@ -110,6 +111,7 @@ You need to pass an argument to this option to specify the name that your module
110111
.string("p")
111112
.string("prefix")
112113
.string("name-cache")
114+
.string("mangle-props-prefix")
113115

114116
.array("reserved-file")
115117
.array("pure-funcs")
@@ -421,6 +423,7 @@ async.eachLimit(files, 1, function (file, cb) {
421423
only_cache : !ARGS.mangle_props,
422424
regex : regex,
423425
ignore_quoted : ARGS.mangle_props == 2,
426+
prefix : ARGS.mangle_props_prefix || "",
424427
debug : typeof ARGS.mangle_props_debug === "undefined" ? false : ARGS.mangle_props_debug
425428
});
426429
writeNameCache("props", cache);

lib/propmangle.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ function mangle_properties(ast, options) {
6767
only_cache : false,
6868
regex : null,
6969
ignore_quoted : false,
70-
debug : false
70+
debug : false,
71+
prefix: ""
7172
});
7273

7374
var reserved = options.reserved;
@@ -202,7 +203,7 @@ function mangle_properties(ast, options) {
202203
// (filled with quoted properties when ignore_quoted set). Make sure we add this
203204
// check so we don't collide with a quoted name.
204205
do {
205-
mangled = base54(++cache.cname);
206+
mangled = options.prefix + base54(++cache.cname);
206207
} while (!can_mangle(mangled) || (ignore_quoted && mangled in ignored));
207208
}
208209

test/compress/mangle-prefix.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
prefix_no_debug: {
2+
mangle_props = {
3+
ignore_quoted: true,
4+
prefix: "__"
5+
}
6+
input: {
7+
var x = {};
8+
x.foo = 1;
9+
x["a"] = 2 * x.foo;
10+
console.log(x.foo, x["a"]);
11+
}
12+
expect: {
13+
var x = {};
14+
x.__a = 1;
15+
x["a"] = 2 * x.__a;
16+
console.log(x.__a, x["a"]);
17+
}
18+
}
19+
20+
prefix_debug: {
21+
mangle_props = {
22+
ignore_quoted: true,
23+
debug: "",
24+
prefix: "__"
25+
}
26+
input: {
27+
var x = {};
28+
x.foo = 1;
29+
x["_$foo$_"] = 2 * x.foo;
30+
console.log(x.foo, x["_$foo$_"]);
31+
}
32+
expect: {
33+
var x = {};
34+
x.__a = 1;
35+
x["_$foo$_"] = 2 * x.__a;
36+
console.log(x.__a, x["_$foo$_"]);
37+
}
38+
}
39+
40+
prefix_with_quoted: {
41+
mangle_props = {
42+
ignore_quoted: false,
43+
prefix: "__"
44+
}
45+
input: {
46+
var x = {};
47+
x.foo = 1;
48+
x["a"] = 2 * x.foo;
49+
console.log(x.foo, x["a"]);
50+
}
51+
expect: {
52+
var x = {};
53+
x.__a = 1;
54+
x["__b"] = 2 * x.__a;
55+
console.log(x.__a, x["__b"]);
56+
}
57+
}

test/input/mangle-prefix/sample.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var Test = function() {
2+
this.someVar = 123;
3+
};
4+
5+
Test.prototype.someMethod = function() {
6+
console.log(this.someVar);
7+
}
8+
9+
Test.prototype.someOther = function() {
10+
console.log("other");
11+
this.someMethod();
12+
this.__special();
13+
}
14+
15+
Test.prototype.__special = function() {
16+
console.log("special");
17+
}
18+
19+
var oTest = new Test();
20+
oTest.someOther();

test/mocha/cli.js

+20
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,24 @@ describe("bin/uglifyjs", function () {
100100
done();
101101
});
102102
});
103+
it("Should work with --mangle-props-prefix", function (done) {
104+
var command = uglifyjscmd + ' test/input/mangle-prefix/sample.js --mangle-props --mangle-props-prefix="__"';
105+
106+
exec(command, function (err, stdout) {
107+
if (err) throw err;
108+
109+
assert.strictEqual(stdout, "var Test=function(){this.__a=123};Test.prototype.__b=function(){console.log(this.__a)};Test.prototype.__c=function(){console.log(\"other\");this.__b();this.__d()};Test.prototype.__d=function(){console.log(\"special\")};var oTest=new Test;oTest.__c();\n");
110+
done();
111+
});
112+
});
113+
it("Should work with --mangle-props-prefix and --mangle-props-regex", function (done) {
114+
var command = uglifyjscmd + ' test/input/mangle-prefix/sample.js --mangle-props --mangle-regex="/^__/" --mangle-props-prefix="__"';
115+
116+
exec(command, function (err, stdout) {
117+
if (err) throw err;
118+
119+
assert.strictEqual(stdout, "var Test=function(){this.someVar=123};Test.prototype.someMethod=function(){console.log(this.someVar)};Test.prototype.someOther=function(){console.log(\"other\");this.someMethod();this.__a()};Test.prototype.__a=function(){console.log(\"special\")};var oTest=new Test;oTest.someOther();\n");
120+
done();
121+
});
122+
});
103123
});

test/mocha/minify.js

+38
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ describe("minify", function() {
5858
assert.strictEqual(result.code,
5959
'a["foo"]="bar",a.a="red",x={"bar":10};');
6060
});
61+
62+
it("Should add prefix for mangle properties (not quoted)", function() {
63+
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
64+
var result = Uglify.minify(js, {
65+
fromString: true,
66+
compress: {
67+
properties: false
68+
},
69+
mangleProperties: {
70+
ignore_quoted: true,
71+
prefix: "__"
72+
},
73+
output: {
74+
keep_quoted_props: true,
75+
quote_style: 3
76+
}
77+
});
78+
assert.strictEqual(result.code, 'a["foo"]="bar",a.__a="red",x={"bar":10};');
79+
});
80+
81+
it("Should add prefix for mangle properties (with quoted)", function() {
82+
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
83+
var result = Uglify.minify(js, {
84+
fromString: true,
85+
compress: {
86+
properties: false
87+
},
88+
mangleProperties: {
89+
ignore_quoted: false,
90+
prefix: "__"
91+
},
92+
output: {
93+
keep_quoted_props: true,
94+
quote_style: 3
95+
}
96+
});
97+
assert.strictEqual(result.code, 'a["__a"]="bar",a.__b="red",x={"__c":10};');
98+
});
6199
});
62100

63101
describe("inSourceMap", function() {

0 commit comments

Comments
 (0)