From 0ac6918a411c85304dc716cd6de68b293a0d133a Mon Sep 17 00:00:00 2001 From: Joao Carlos Date: Tue, 9 Jun 2015 13:21:22 +0300 Subject: [PATCH 1/3] Add --mangle-regex option --- README.md | 5 +++++ bin/uglifyjs | 5 ++++- lib/propmangle.js | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f3eb5f71..073a8715 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/uglifyjs b/bin/uglifyjs index 8851660f..71c82644 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -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); })(); diff --git a/lib/propmangle.js b/lib/propmangle.js index 0890eaa7..086709e6 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -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; From 9aef34a8168f5de3bc00330a740c22de70314a70 Mon Sep 17 00:00:00 2001 From: Joao Carlos Date: Tue, 9 Jun 2015 14:31:49 +0300 Subject: [PATCH 2/3] Show descriptive error when --mangle-regex is invalid --- bin/uglifyjs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 71c82644..9a1323fd 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -376,7 +376,15 @@ 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; + var regex; + + try { + regex = ARGS.mangle_regex ? new RegExp(ARGS.mangle_regex) : null; + } catch (e) { + print_error("ERROR: Invalid --mangle-regex: " + e.message); + process.exit(1); + } + TOPLEVEL = UglifyJS.mangle_properties(TOPLEVEL, { reserved : reserved, cache : cache, From 0b82e1cd5b29e348700472cc711020e477d5e81a Mon Sep 17 00:00:00 2001 From: Joao Carlos Date: Tue, 9 Jun 2015 15:14:41 +0300 Subject: [PATCH 3/3] Change --mangle-regex to accept a full regex --- README.md | 2 +- bin/uglifyjs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 073a8715..3b99441b 100644 --- a/README.md +++ b/README.md @@ -253,7 +253,7 @@ 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 +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 diff --git a/bin/uglifyjs b/bin/uglifyjs index 9a1323fd..4768f766 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -192,6 +192,15 @@ function writeNameCache(key, cache) { return UglifyJS.writeNameCache(ARGS.name_cache, key, cache); } +function extractRegex(str) { + if (/^\/.*\/[a-zA-Z]*$/.test(str)) { + var regex_pos = str.lastIndexOf("/"); + return new RegExp(str.substr(1, regex_pos - 1), str.substr(regex_pos + 1)); + } else { + throw new Error("Invalid regular expression: " + str); + } +} + if (ARGS.quotes === true) { ARGS.quotes = 3; } @@ -218,9 +227,8 @@ if (BEAUTIFY) if (ARGS.comments != null) { if (/^\/.*\/[a-zA-Z]*$/.test(ARGS.comments)) { - var regex_pos = ARGS.comments.lastIndexOf("/"); try { - OUTPUT_OPTIONS.comments = new RegExp(ARGS.comments.substr(1, regex_pos - 1), ARGS.comments.substr(regex_pos + 1)); + OUTPUT_OPTIONS.comments = extractRegex(ARGS.comments); } catch (e) { print_error("ERROR: Invalid --comments: " + e.message); } @@ -379,7 +387,7 @@ async.eachLimit(files, 1, function (file, cb) { var regex; try { - regex = ARGS.mangle_regex ? new RegExp(ARGS.mangle_regex) : null; + regex = ARGS.mangle_regex ? extractRegex(ARGS.mangle_regex) : null; } catch (e) { print_error("ERROR: Invalid --mangle-regex: " + e.message); process.exit(1);