support for regex based name filtering

This commit is contained in:
Gagan Jakhotiya 2019-10-22 19:32:48 +05:30
parent 0b3705e82f
commit da25046b81
6 changed files with 26 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,6 +107,7 @@ function minify(files, options) {
properties: false,
reserved: [],
toplevel: false,
regex: null,
}, true);
if (options.mangle.properties) {
if (typeof options.mangle.properties != "object") {

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

@ -9,6 +9,14 @@ function read(path) {
describe("bin/uglifyjs", function() {
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
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 produce a functional build when using --self", function(done) {
this.timeout(30000);
var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS';