Add --mangle-regex option

This commit is contained in:
Joao Carlos
2015-06-09 13:21:22 +03:00
parent c6fa291571
commit 0ac6918a41
3 changed files with 14 additions and 2 deletions

View File

@@ -132,6 +132,7 @@ The available options are:
--reserve-domprops Make (most?) DOM properties reserved for
--mangle-props
--mangle-props Mangle property names
--mangle-regex Only mangle property names matching the regex
--name-cache File to hold mangled names mappings
```
@@ -251,6 +252,10 @@ A default exclusion file is provided in `tools/domprops.json` which should
cover most standard JS and DOM properties defined in various browsers. Pass
`--reserve-domprops` to read that in.
You can also use a regular expression to define which property names should be
mangled. For example, `--mangle-regex="^_"` will only mangle property names
that start with an underscore.
When you compress multiple files using this option, in order for them to
work together in the end we need to ensure somehow that one property gets
mangled to the same name in all of them. For this, pass `--name-cache

View File

@@ -70,6 +70,7 @@ You need to pass an argument to this option to specify the name that your module
.describe("reserved-file", "File containing reserved names")
.describe("reserve-domprops", "Make (most?) DOM properties reserved for --mangle-props")
.describe("mangle-props", "Mangle property names")
.describe("mangle-regex", "Only mangle property names matching the regex")
.describe("name-cache", "File to hold mangled names mappings")
.alias("p", "prefix")
@@ -375,10 +376,12 @@ async.eachLimit(files, 1, function (file, cb) {
if (ARGS.mangle_props || ARGS.name_cache) (function(){
var reserved = RESERVED ? RESERVED.props : null;
var cache = readNameCache("props");
var regex = ARGS.mangle_regex ? new RegExp(ARGS.mangle_regex) : null;
TOPLEVEL = UglifyJS.mangle_properties(TOPLEVEL, {
reserved : reserved,
cache : cache,
only_cache : !ARGS.mangle_props
only_cache : !ARGS.mangle_props,
regex : regex
});
writeNameCache("props", cache);
})();

View File

@@ -64,7 +64,8 @@ function mangle_properties(ast, options) {
options = defaults(options, {
reserved : null,
cache : null,
only_cache : false
only_cache : false,
regex : null
});
var reserved = options.reserved;
@@ -79,6 +80,8 @@ function mangle_properties(ast, options) {
};
}
var regex = options.regex;
var names_to_mangle = [];
// step 1: find candidates to mangle
@@ -149,6 +152,7 @@ function mangle_properties(ast, options) {
}
function should_mangle(name) {
if (regex && !regex.test(name)) return false;
if (reserved.indexOf(name) >= 0) return false;
return cache.props.has(name)
|| names_to_mangle.indexOf(name) >= 0;