Skip to content

Commit c0686c4

Browse files
committed
support an empty context
fixed webpack#524
1 parent 8bbc81f commit c0686c4

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/ContextModule.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ ContextModule.prototype.build = function(options, compilation, resolver, fs, cal
6666
var addon = this.addon;
6767
this.resolveDependencies(fs, this.context, this.recursive, this.regExp, function(err, dependencies) {
6868
if(err) return callback(err);
69-
dependencies.forEach(function(dep) {
70-
dep.userRequest = dep.request;
71-
dep.request = addon + dep.userRequest;
72-
});
69+
if(dependencies) {
70+
dependencies.forEach(function(dep) {
71+
dep.userRequest = dep.request;
72+
dep.request = addon + dep.userRequest;
73+
});
74+
}
7375
this.dependencies = dependencies;
7476
callback();
7577
}.bind(this));
7678
};
7779

7880
ContextModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
79-
if(this.dependencies.length > 0) {
81+
if(this.dependencies && this.dependencies.length > 0) {
8082
var map = {};
8183
this.dependencies.slice().sort(function(a,b) {
8284
if(a.userRequest === b.userRequest) return 0;
@@ -105,9 +107,10 @@ ContextModule.prototype.source = function(dependencyTemplates, outputOptions, re
105107
"function webpackContext(req) {\n",
106108
"\tthrow new Error(\"Cannot find module '\" + req + \"'.\");\n",
107109
"}\n",
108-
"webpackContext.resolve = webpackContext;\n",
109110
"webpackContext.keys = function() { return []; };\n",
110-
"module.exports = webpackContext;\n"
111+
"webpackContext.resolve = webpackContext;\n",
112+
"module.exports = webpackContext;\n",
113+
"webpackContext.id = "+this.id+";\n",
111114
];
112115
}
113116
if(this.useSourceMap) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
it("should support an empty context", function() {
2+
var c = require.context(".", true, /^nothing$/);
3+
c.id.should.be.type("number");
4+
(function() {
5+
c.resolve("");
6+
}).should.throw();
7+
(function() {
8+
c("");
9+
}).should.throw();
10+
c.keys().should.be.eql([]);
11+
});

0 commit comments

Comments
 (0)