Skip to content

Commit b7480d8

Browse files
committed
added more parameters to ContextReplacementPlugin
webpack#315
1 parent 472558c commit b7480d8

File tree

11 files changed

+82
-4
lines changed

11 files changed

+82
-4
lines changed

lib/ContextReplacementPlugin.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,49 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function ContextReplacementPlugin(resourceRegExp, newContentRegExp) {
5+
function ContextReplacementPlugin(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) {
66
this.resourceRegExp = resourceRegExp;
7-
this.newContentRegExp = newContentRegExp
7+
if(typeof newContentResource !== "string") {
8+
newContentRegExp = newContentRecursive;
9+
newContentRecursive = newContentResource;
10+
newContentResource = undefined;
11+
}
12+
if(typeof newContentRecursive !== "boolean") {
13+
newContentRegExp = newContentRecursive;
14+
newContentRecursive = undefined;
15+
}
16+
this.newContentResource = newContentResource;
17+
this.newContentRecursive = newContentRecursive;
18+
this.newContentRegExp = newContentRegExp;
819
}
920
module.exports = ContextReplacementPlugin;
1021
ContextReplacementPlugin.prototype.apply = function(compiler) {
1122
var resourceRegExp = this.resourceRegExp;
23+
var newContentResource = this.newContentResource;
24+
var newContentRecursive = this.newContentRecursive;
1225
var newContentRegExp = this.newContentRegExp;
1326
compiler.plugin("context-module-factory", function(cmf) {
1427
cmf.plugin("before-resolve", function(result, callback) {
1528
if(!result) return callback();
1629
if(resourceRegExp.test(result.request)) {
17-
result.regExp = newContentRegExp;
30+
if(typeof newContentResource !== "undefined")
31+
result.request = newContentResource;
32+
if(typeof newContentRecursive !== "undefined")
33+
result.recursive = newContentRecursive;
34+
if(typeof newContentRegExp !== "undefined")
35+
result.regExp = newContentRegExp;
1836
}
1937
return callback(null, result);
2038
});
2139
cmf.plugin("after-resolve", function(result, callback) {
2240
if(!result) return callback();
2341
if(resourceRegExp.test(result.resource)) {
24-
result.regExp = newContentRegExp;
42+
if(typeof newContentResource !== "undefined")
43+
result.resource = newContentResource;
44+
if(typeof newContentRecursive !== "undefined")
45+
result.recursive = newContentRecursive;
46+
if(typeof newContentRegExp !== "undefined")
47+
result.regExp = newContentRegExp;
2548
}
2649
return callback(null, result);
2750
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
it("should replace a context with a new resource and reqExp", function(done) {
2+
function rqInContext(x, callback) {
3+
require([x], function(x) {
4+
callback(x);
5+
});
6+
}
7+
rqInContext("replaced", function(r) {
8+
r.should.be.eql("ok");
9+
done();
10+
});
11+
});

test/configCases/context-replacement/a/new-context/node_modules/error.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/configCases/context-replacement/a/new-context/node_modules/replaced.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/Critical dependencies/, /a\/index\.js/]
3+
];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var path = require("path");
2+
var webpack = require("../../../../");
3+
4+
module.exports = {
5+
plugins: [
6+
new webpack.ContextReplacementPlugin(/context-replacement.a$/, path.join(__dirname, "new-context"), true, /^replaced$/)
7+
]
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This
2+
should
3+
result
4+
in
5+
an
6+
error
7+
}])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
it("should replace a context with a new regExp", function() {
2+
function rqInContext(x) {
3+
return require(x);
4+
}
5+
rqInContext("./only-this").should.be.eql("ok");
6+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "ok";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/Critical dependencies/, /b\/index\.js/]
3+
];

0 commit comments

Comments
 (0)