forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NormalModuleReplacementPlugin.js
40 lines (38 loc) · 1.17 KB
/
NormalModuleReplacementPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var path = require("path");
function NormalModuleReplacementPlugin(resourceRegExp, newResource) {
this.resourceRegExp = resourceRegExp;
this.newResource = newResource;
}
module.exports = NormalModuleReplacementPlugin;
NormalModuleReplacementPlugin.prototype.apply = function(compiler) {
var resourceRegExp = this.resourceRegExp;
var newResource = this.newResource;
compiler.plugin("normal-module-factory", function(nmf) {
nmf.plugin("before-resolve", function(result, callback) {
if(!result) return callback();
if(resourceRegExp.test(result.request)) {
if(typeof newResource === "function") {
newResource(result);
} else {
result.request = newResource;
}
}
return callback(null, result);
});
nmf.plugin("after-resolve", function(result, callback) {
if(!result) return callback();
if(resourceRegExp.test(result.resource)) {
if(typeof newResource === "function") {
newResource(result);
} else {
result.resource = path.resolve(path.dirname(result.resource), newResource);
}
}
return callback(null, result);
});
});
};