Skip to content

Commit 9cbce10

Browse files
Factorize code with UglifyJSPlugin
1 parent 6deb702 commit 9cbce10

File tree

3 files changed

+32
-53
lines changed

3 files changed

+32
-53
lines changed

lib/ModuleFilenameHelpers.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ function getHash(str) {
4141
return hash.digest("hex").substr(0, 4);
4242
}
4343

44+
function asRegExp(test) {
45+
if(typeof test == "string") test = new RegExp("^"+test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
46+
return test;
47+
}
48+
4449
ModuleFilenameHelpers.createFilename = function createFilename(module, moduleFilenameTemplate, requestShortener) {
4550
if(typeof module === "string") {
4651
var shortIdentifier = requestShortener.shorten(module);
@@ -114,3 +119,26 @@ ModuleFilenameHelpers.replaceDuplicates = function replaceDuplicates(array, fn,
114119
} else return item;
115120
});
116121
};
122+
123+
124+
ModuleFilenameHelpers.matchPart = function matchPart(str, test) {
125+
if(!test) return true;
126+
test = asRegExp(test);
127+
if(Array.isArray(test)) {
128+
return test.map(asRegExp).filter(function(regExp) {
129+
return regExp.test(str);
130+
}).length > 0;
131+
} else {
132+
return test.test(str);
133+
}
134+
};
135+
136+
ModuleFilenameHelpers.matchObject = function matchObject(obj, str) {
137+
if(obj.test)
138+
if(!ModuleFilenameHelpers.matchPart(str, obj.test)) return false;
139+
if(obj.include)
140+
if(!ModuleFilenameHelpers.matchPart(str, obj.include)) return false;
141+
if(obj.exclude)
142+
if(ModuleFilenameHelpers.matchPart(str, obj.exclude)) return false;
143+
return true;
144+
};

lib/SourceMapDevToolPlugin.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
3434
var fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
3535
var requestShortener = new RequestShortener(compiler.context);
3636
var cheapMode = this.cheapMode;
37-
var that = this;
3837
var options = this.options;
38+
options.test = options.test || /\.js($|\?)/i;
3939
compiler.plugin("compilation", function(compilation) {
4040
if(cheapMode) {
4141
compilation.moduleTemplate.plugin("module", function(source, module) {
@@ -52,7 +52,7 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
5252
var allModuleFilenames = [];
5353
var tasks = [];
5454
chunks.forEach(function(chunk) {
55-
chunk.files.filter(that.matchObject.bind(that, options)).map(function(file) {
55+
chunk.files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)).map(function(file) {
5656
var asset = this.assets[file];
5757
if(asset.__SourceMapDevTool_Data) {
5858
var data = asset.__SourceMapDevTool_Data;
@@ -164,28 +164,6 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
164164
});
165165
};
166166

167-
function asRegExp(test) {
168-
if(typeof test == "string") test = new RegExp("^"+test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
169-
return test;
170-
}
171-
SourceMapDevToolPlugin.prototype.matchPart = function matchPart(str, test) {
172-
if(!test) return true;
173-
test = asRegExp(test);
174-
if(Array.isArray(test)) {
175-
return test.map(asRegExp).filter(function(regExp) {
176-
return regExp.test(str);
177-
}).length > 0;
178-
} else {
179-
return test.test(str);
180-
}
181-
};
182-
183-
SourceMapDevToolPlugin.prototype.matchObject = function matchObject(obj, str) {
184-
if(obj.exclude)
185-
if(this.matchPart(str, obj.exclude)) return false;
186-
return true;
187-
};
188-
189167
function basename(name) {
190168
if(name.indexOf("/") < 0) return name;
191169
return name.substr(name.lastIndexOf("/")+1);

lib/optimize/UglifyJsPlugin.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var SourceMapConsumer = require("webpack-core/lib/source-map").SourceMapConsumer
66
var SourceMapSource = require("webpack-core/lib/SourceMapSource");
77
var RawSource = require("webpack-core/lib/RawSource");
88
var RequestShortener = require("../RequestShortener");
9+
var ModuleFilenameHelpers = require("../ModuleFilenameHelpers");
910
var uglify = require("uglify-js");
1011

1112
function UglifyJsPlugin(options) {
@@ -19,7 +20,6 @@ module.exports = UglifyJsPlugin;
1920

2021
UglifyJsPlugin.prototype.apply = function(compiler) {
2122
var options = this.options;
22-
var that = this;
2323
options.test = options.test || /\.js($|\?)/i;
2424

2525
var requestShortener = new RequestShortener(compiler.context);
@@ -40,7 +40,7 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
4040
compilation.additionalChunkAssets.forEach(function(file) {
4141
files.push(file);
4242
});
43-
files = files.filter(that.matchObject.bind(that, options));
43+
files = files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options));
4444
files.forEach(function(file) {
4545
var oldWarnFunction = uglify.AST_Node.warn_function;
4646
var warnings = [];
@@ -133,30 +133,3 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
133133
});
134134
});
135135
};
136-
137-
function asRegExp(test) {
138-
if(typeof test == "string") test = new RegExp("^"+test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
139-
return test;
140-
}
141-
142-
UglifyJsPlugin.prototype.matchPart = function matchPart(str, test) {
143-
if(!test) return true;
144-
test = asRegExp(test);
145-
if(Array.isArray(test)) {
146-
return test.map(asRegExp).filter(function(regExp) {
147-
return regExp.test(str);
148-
}).length > 0;
149-
} else {
150-
return test.test(str);
151-
}
152-
};
153-
154-
UglifyJsPlugin.prototype.matchObject = function matchObject(obj, str) {
155-
if(obj.test)
156-
if(!this.matchPart(str, obj.test)) return false;
157-
if(obj.include)
158-
if(!this.matchPart(str, obj.include)) return false;
159-
if(obj.exclude)
160-
if(this.matchPart(str, obj.exclude)) return false;
161-
return true;
162-
};

0 commit comments

Comments
 (0)