Skip to content

Commit e400afb

Browse files
committed
Merge pull request webpack#560 from bernii/master
Add ability to skip files in UglifyJs via user-defined function
2 parents b02ef8b + 836aac7 commit e400afb

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

lib/optimize/UglifyJsPlugin.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ function UglifyJsPlugin(options) {
1515
this.options = options;
1616
}
1717
module.exports = UglifyJsPlugin;
18+
1819
UglifyJsPlugin.prototype.apply = function(compiler) {
1920
var options = this.options;
21+
var that = this;
22+
options.test = options.test || /\.js($|\?)/i;
23+
2024
var requestShortener = new RequestShortener(compiler.context);
2125
compiler.plugin("compilation", function(compilation) {
2226
compilation.plugin("build-module", function(module) {
@@ -33,11 +37,8 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
3337
compilation.additionalChunkAssets.forEach(function(file) {
3438
files.push(file);
3539
});
40+
files = files.filter(that.matchObject.bind(that, options));
3641
files.forEach(function(file) {
37-
if(!/\.js($|\?)/i.test(file)) {
38-
// UglifyJs only applies to javascript
39-
return;
40-
}
4142
var oldWarnFunction = uglify.AST_Node.warn_function;
4243
var warnings = [];
4344
try {
@@ -117,4 +118,31 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
117118
context.minimize = true;
118119
});
119120
});
120-
};
121+
};
122+
123+
function asRegExp(test) {
124+
if(typeof test == "string") test = new RegExp("^"+test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
125+
return test;
126+
}
127+
128+
UglifyJsPlugin.prototype.matchPart = function matchPart(str, test) {
129+
if(!test) return true;
130+
test = asRegExp(test);
131+
if(Array.isArray(test)) {
132+
return test.map(asRegExp).filter(function(regExp) {
133+
return regExp.test(str);
134+
}).length > 0;
135+
} else {
136+
return test.test(str);
137+
}
138+
};
139+
140+
UglifyJsPlugin.prototype.matchObject = function matchObject(obj, str) {
141+
if(obj.test)
142+
if(!this.matchPart(str, obj.test)) return false;
143+
if(obj.include)
144+
if(!this.matchPart(str, obj.include)) return false;
145+
if(obj.exclude)
146+
if(this.matchPart(str, obj.exclude)) return false;
147+
return true;
148+
};

0 commit comments

Comments
 (0)