Change --mangle-regex to accept a full regex

This commit is contained in:
Joao Carlos
2015-06-09 15:14:41 +03:00
parent 9aef34a816
commit 0b82e1cd5b
2 changed files with 12 additions and 4 deletions

View File

@@ -253,7 +253,7 @@ cover most standard JS and DOM properties defined in various browsers. Pass
`--reserve-domprops` to read that in. `--reserve-domprops` to read that in.
You can also use a regular expression to define which property names should be 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. that start with an underscore.
When you compress multiple files using this option, in order for them to When you compress multiple files using this option, in order for them to

View File

@@ -192,6 +192,15 @@ function writeNameCache(key, cache) {
return UglifyJS.writeNameCache(ARGS.name_cache, 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) { if (ARGS.quotes === true) {
ARGS.quotes = 3; ARGS.quotes = 3;
} }
@@ -218,9 +227,8 @@ if (BEAUTIFY)
if (ARGS.comments != null) { if (ARGS.comments != null) {
if (/^\/.*\/[a-zA-Z]*$/.test(ARGS.comments)) { if (/^\/.*\/[a-zA-Z]*$/.test(ARGS.comments)) {
var regex_pos = ARGS.comments.lastIndexOf("/");
try { 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) { } catch (e) {
print_error("ERROR: Invalid --comments: " + e.message); print_error("ERROR: Invalid --comments: " + e.message);
} }
@@ -379,7 +387,7 @@ async.eachLimit(files, 1, function (file, cb) {
var regex; var regex;
try { try {
regex = ARGS.mangle_regex ? new RegExp(ARGS.mangle_regex) : null; regex = ARGS.mangle_regex ? extractRegex(ARGS.mangle_regex) : null;
} catch (e) { } catch (e) {
print_error("ERROR: Invalid --mangle-regex: " + e.message); print_error("ERROR: Invalid --mangle-regex: " + e.message);
process.exit(1); process.exit(1);