support for regex based name filtering

This commit is contained in:
Gagan Jakhotiya 2019-10-22 19:32:48 +05:30
parent 0b3705e82f
commit 4e2caea817
6 changed files with 54 additions and 0 deletions

View File

@ -63,6 +63,7 @@ a double dash to prevent input files being used as option arguments:
not used.
-m, --mangle [options] Mangle names/specify mangler options:
`reserved` List of names that should not be mangled.
`regex` Only mangle matched identifiers.
--mangle-props [options] Mangle properties/specify mangler options:
`builtins` Mangle property names that overlaps
with standard JavaScript globals.
@ -219,6 +220,13 @@ comma-separated list of names. For example:
to prevent the `require`, `exports` and `$` names from being changed.
If you want to mangle only the names matching a specific regex, use `regex` option.
For example:
uglifyjs ... -m regex='/^sw/'
to mangle only names that starts with `sw`
### CLI mangling property names (`--mangle-props`)
**Note:** THIS WILL PROBABLY BREAK YOUR CODE. Mangling property names
@ -783,6 +791,9 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `toplevel` (default `false`) -- Pass `true` to mangle names declared in the
top level scope.
- `regex` (default `null`) -- Pass a RegExp literal to only mangle names matching
the regular expression.
Examples:
```javascript

View File

@ -107,7 +107,12 @@ function minify(files, options) {
properties: false,
reserved: [],
toplevel: false,
regex: null,
}, true);
if (options.mangle.regex !== null &&
options.mangle.regex.constructor.name != "RegExp") {
throw "Input is not a RegExp";
}
if (options.mangle.properties) {
if (typeof options.mangle.properties != "object") {
options.mangle.properties = {};

View File

@ -386,6 +386,7 @@ function _default_mangler_options(options) {
keep_fnames : false,
reserved : [],
toplevel : false,
regex : null,
});
if (!Array.isArray(options.reserved)) options.reserved = [];
// Never mangle arguments
@ -450,6 +451,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
function mangle(def) {
if (options.reserved.has[def.name]) return;
if (options.regex && !options.regex.test(def.name)) return;
def.mangle(options);
}
@ -516,6 +518,7 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
if (def.global && options.cache) return;
if (def.unmangleable(options)) return;
if (options.reserved.has[def.name]) return;
if (options.regex && !options.regex.test(def.name)) return;
var redef = def.redefined();
var name = redef ? redef.rename || redef.name : next_name();
def.rename = name;

View File

@ -0,0 +1,2 @@
var foo = "asasas";
var _startsWithThis = "dddddd";

View File

@ -0,0 +1 @@
var foo="asasas";var d="dddddd";

View File

@ -744,4 +744,36 @@ describe("bin/uglifyjs", function() {
done();
}).stdin.end(code);
});
it("Should work with --mangle toplevel,regex='/^_startsWith/g;'", function(done) {
var command = uglifyjscmd + " test/input/name-regex/input.js -m toplevel,regex='/^_startsWith/g;'";
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, read("test/input/name-regex/output.js"));
done();
});
});
it("Should work without regex", function(done) {
var command = uglifyjscmd + " test/input/name-regex/input.js -m toplevel";
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, "var d=\"asasas\";var a=\"dddddd\";\n");
done();
});
});
it("Should give error against non-regex input", function(done) {
var command = uglifyjscmd + " test/input/name-regex/input.js -m toplevel,regex=g";
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stderr, "ERROR: Input is not a RegExp\n");
done();
});
});
it("Should give error against invalid input regex", function(done) {
var command = uglifyjscmd + " test/input/name-regex/input.js -m toplevel,regex=//";
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(true, String(stderr).indexOf("not a supported option") != -1);
done();
});
});
});