support for regex based name filtering
This commit is contained in:
parent
0b3705e82f
commit
da25046b81
11
README.md
11
README.md
|
|
@ -63,6 +63,7 @@ a double dash to prevent input files being used as option arguments:
|
||||||
not used.
|
not used.
|
||||||
-m, --mangle [options] Mangle names/specify mangler options:
|
-m, --mangle [options] Mangle names/specify mangler options:
|
||||||
`reserved` List of names that should not be mangled.
|
`reserved` List of names that should not be mangled.
|
||||||
|
`regex` Only mangle matched identifiers.
|
||||||
--mangle-props [options] Mangle properties/specify mangler options:
|
--mangle-props [options] Mangle properties/specify mangler options:
|
||||||
`builtins` Mangle property names that overlaps
|
`builtins` Mangle property names that overlaps
|
||||||
with standard JavaScript globals.
|
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.
|
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`)
|
### CLI mangling property names (`--mangle-props`)
|
||||||
|
|
||||||
**Note:** THIS WILL PROBABLY BREAK YOUR CODE. Mangling property names
|
**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
|
- `toplevel` (default `false`) -- Pass `true` to mangle names declared in the
|
||||||
top level scope.
|
top level scope.
|
||||||
|
|
||||||
|
- `regex` (default `null`) -- Pass a RegExp literal to only mangle names matching
|
||||||
|
the regular expression.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,7 @@ function minify(files, options) {
|
||||||
properties: false,
|
properties: false,
|
||||||
reserved: [],
|
reserved: [],
|
||||||
toplevel: false,
|
toplevel: false,
|
||||||
|
regex: null,
|
||||||
}, true);
|
}, true);
|
||||||
if (options.mangle.properties) {
|
if (options.mangle.properties) {
|
||||||
if (typeof options.mangle.properties != "object") {
|
if (typeof options.mangle.properties != "object") {
|
||||||
|
|
|
||||||
|
|
@ -386,6 +386,7 @@ function _default_mangler_options(options) {
|
||||||
keep_fnames : false,
|
keep_fnames : false,
|
||||||
reserved : [],
|
reserved : [],
|
||||||
toplevel : false,
|
toplevel : false,
|
||||||
|
regex : null,
|
||||||
});
|
});
|
||||||
if (!Array.isArray(options.reserved)) options.reserved = [];
|
if (!Array.isArray(options.reserved)) options.reserved = [];
|
||||||
// Never mangle arguments
|
// Never mangle arguments
|
||||||
|
|
@ -450,6 +451,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
|
||||||
|
|
||||||
function mangle(def) {
|
function mangle(def) {
|
||||||
if (options.reserved.has[def.name]) return;
|
if (options.reserved.has[def.name]) return;
|
||||||
|
if (options.regex && !options.regex.test(def.name)) return;
|
||||||
def.mangle(options);
|
def.mangle(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -516,6 +518,7 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
||||||
if (def.global && options.cache) return;
|
if (def.global && options.cache) return;
|
||||||
if (def.unmangleable(options)) return;
|
if (def.unmangleable(options)) return;
|
||||||
if (options.reserved.has[def.name]) return;
|
if (options.reserved.has[def.name]) return;
|
||||||
|
if (options.regex && !options.regex.test(def.name)) return;
|
||||||
var redef = def.redefined();
|
var redef = def.redefined();
|
||||||
var name = redef ? redef.rename || redef.name : next_name();
|
var name = redef ? redef.rename || redef.name : next_name();
|
||||||
def.rename = name;
|
def.rename = name;
|
||||||
|
|
|
||||||
2
test/input/name-regex/input.js
Normal file
2
test/input/name-regex/input.js
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
var foo = "asasas";
|
||||||
|
var _startsWithThis = "dddddd";
|
||||||
1
test/input/name-regex/output.js
Normal file
1
test/input/name-regex/output.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
var foo="asasas";var d="dddddd";
|
||||||
|
|
@ -9,6 +9,14 @@ function read(path) {
|
||||||
|
|
||||||
describe("bin/uglifyjs", function() {
|
describe("bin/uglifyjs", function() {
|
||||||
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
|
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) {
|
it("Should produce a functional build when using --self", function(done) {
|
||||||
this.timeout(30000);
|
this.timeout(30000);
|
||||||
var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS';
|
var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user