fix and expand --mangle-props documentation (#2008)

fixes #2007
This commit is contained in:
kzc
2017-05-26 13:25:51 -04:00
committed by Alex Lam S.L
parent dc33facfcb
commit 695e182d59

View File

@@ -224,18 +224,47 @@ separate step, different from variable name mangling. Pass
object literal, or that are assigned to. For example: object literal, or that are assigned to. For example:
```javascript ```javascript
// example.js
var x = { var x = {
foo: 1 baz_: 0,
foo_: 1,
calc: function() {
return this.foo_ + this.baz_;
}
}; };
x.bar_ = 2;
x.bar = 2; x["baz_"] = 3;
x["baz"] = 3; console.log(x.calc());
x[condition ? "moo" : "boo"] = 4; ```
console.log(x.something()); Mangle all properties (except for javascript `builtins`):
```bash
$ uglifyjs example.js -c -m --mangle-props
```
```javascript
var x={o:0,_:1,l:function(){return this._+this.o}};x.t=2,x.o=3,console.log(x.l());
```
Mangle all properties except for `reserved` properties:
```bash
$ uglifyjs example.js -c -m --mangle-props reserved=[foo_,bar_]
```
```javascript
var x={o:0,foo_:1,_:function(){return this.foo_+this.o}};x.bar_=2,x.o=3,console.log(x._());
```
Mangle all properties matching a `regex`:
```bash
$ uglifyjs example.js -c -m --mangle-props regex=/_$/
```
```javascript
var x={o:0,_:1,calc:function(){return this._+this.o}};x.l=2,x.o=3,console.log(x.calc());
``` ```
In the above code, `foo`, `bar`, `baz`, `moo` and `boo` will be replaced Combining mangle properties options:
with single characters, while `something()` will be left as is. ```bash
$ uglifyjs example.js -c -m --mangle-props regex=/_$/,reserved=[bar_]
```
```javascript
var x={o:0,_:1,calc:function(){return this._+this.o}};x.bar_=2,x.o=3,console.log(x.calc());
```
In order for this to be of any use, we avoid mangling standard JS names by In order for this to be of any use, we avoid mangling standard JS names by
default (`--mangle-props builtins` to override). default (`--mangle-props builtins` to override).
@@ -244,7 +273,7 @@ A default exclusion file is provided in `tools/domprops.json` which should
cover most standard JS and DOM properties defined in various browsers. Pass cover most standard JS and DOM properties defined in various browsers. Pass
`--mangle-props domprops` to disable this feature. `--mangle-props domprops` to disable this feature.
You can also use a regular expression to define which property names should be A regular expression can be used to define which property names should be
mangled. For example, `--mangle-props regex=/^_/` will only mangle property mangled. For example, `--mangle-props regex=/^_/` will only mangle property
names that start with an underscore. names that start with an underscore.
@@ -272,9 +301,20 @@ Using quoted property name (`o["foo"]`) reserves the property name (`foo`)
so that it is not mangled throughout the entire script even when used in an so that it is not mangled throughout the entire script even when used in an
unquoted style (`o.foo`). Example: unquoted style (`o.foo`). Example:
```javascript
// stuff.js
var o = {
"foo": 1,
bar: 3
};
o.foo += o.bar;
console.log(o.foo);
```
```bash ```bash
$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props keep_quoted -mc $ uglifyjs stuff.js --mangle-props keep_quoted -c -m
var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo); ```
```javascript
var o={foo:1,o:3};o.foo+=o.o,console.log(o.foo);
``` ```
### Debugging property name mangling ### Debugging property name mangling
@@ -285,6 +325,13 @@ would mangle to `o._$foo$_` with this option. This allows property mangling
of a large codebase while still being able to debug the code and identify of a large codebase while still being able to debug the code and identify
where mangling is breaking things. where mangling is breaking things.
```bash
$ uglifyjs stuff.js --mangle-props debug -c -m
```
```javascript
var o={_$foo$_:1,_$bar$_:3};o._$foo$_+=o._$bar$_,console.log(o._$foo$_);
```
You can also pass a custom suffix using `--mangle-props debug=XYZ`. This would then You can also pass a custom suffix using `--mangle-props debug=XYZ`. This would then
mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a
script to identify how a property got mangled. One technique is to pass a script to identify how a property got mangled. One technique is to pass a