[Pkg-javascript-commits] [uglifyjs] 100/491: fix and expand --mangle-props documentation (#2008)
Jonas Smedegaard
dr at jones.dk
Wed Feb 14 19:51:25 UTC 2018
This is an automated email from the git hooks/post-receive script.
js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.
commit 695e182d596c00bfdf9bacff36fd8e65c6f35bf3
Author: kzc <kzc at users.noreply.github.com>
Date: Fri May 26 13:25:51 2017 -0400
fix and expand --mangle-props documentation (#2008)
fixes #2007
---
README.md | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 58 insertions(+), 11 deletions(-)
diff --git a/README.md b/README.md
index 47012c1..d147e61 100644
--- a/README.md
+++ b/README.md
@@ -224,18 +224,47 @@ separate step, different from variable name mangling. Pass
object literal, or that are assigned to. For example:
```javascript
+// example.js
var x = {
- foo: 1
+ baz_: 0,
+ foo_: 1,
+ calc: function() {
+ return this.foo_ + this.baz_;
+ }
};
-
-x.bar = 2;
-x["baz"] = 3;
-x[condition ? "moo" : "boo"] = 4;
-console.log(x.something());
+x.bar_ = 2;
+x["baz_"] = 3;
+console.log(x.calc());
+```
+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
-with single characters, while `something()` will be left as is.
+Combining mangle properties options:
+```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
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
`--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
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
unquoted style (`o.foo`). Example:
+```javascript
+// stuff.js
+var o = {
+ "foo": 1,
+ bar: 3
+};
+o.foo += o.bar;
+console.log(o.foo);
+```
```bash
-$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props keep_quoted -mc
-var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo);
+$ uglifyjs stuff.js --mangle-props keep_quoted -c -m
+```
+```javascript
+var o={foo:1,o:3};o.foo+=o.o,console.log(o.foo);
```
### 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
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
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
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git
More information about the Pkg-javascript-commits
mailing list