Skip to content

Commit f9e8daf

Browse files
author
Beau Roberts
committed
Fixes issue webpack#2299 for 1.x
Fixes an issue where multiple calls to loadModule from within a loader would resolve modules using the context of the *last loaded module* rather than the module *doing the loading*, which is problematic if the modules being loaded are in different directories.
1 parent da40998 commit f9e8daf

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

lib/dependencies/LoaderPlugin.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ LoaderPlugin.prototype.apply = function(compiler) {
2323
], true, "lm", false, function(err) {
2424
if(err) return callback(err);
2525

26-
module = dep.module;
27-
if(!module) return callback(new Error("Cannot load the module"));
28-
if(module.building) module.building.push(next);
26+
if(!dep.module) return callback(new Error("Cannot load the module"));
27+
if(dep.module.building) dep.module.building.push(next);
2928
else next();
3029

3130
function next(err) {

test/cases/loaders/issue-2299/a.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"imports": [
3+
"./subdir_1/b.json",
4+
"./subdir_2/c.json"
5+
],
6+
"a": true
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should be able to use loadModule multiple times within a loader, on files in different directories", function() {
2+
require('!./loader/index.js!./a.json').should.have.properties(['a', 'b', 'c']);
3+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var path = require('path');
2+
var async = require('async');
3+
var assign = require('object-assign');
4+
module.exports = function(content) {
5+
var cb = this.async();
6+
var json = JSON.parse(content);
7+
async.mapSeries(
8+
json.imports,
9+
function(url, callback) {
10+
this.loadModule(url, function(err, source, map, module) {
11+
if (err) {
12+
return callback(err);
13+
}
14+
callback(null, this.exec(source, url));
15+
}.bind(this))
16+
}.bind(this),
17+
function(err, results) {
18+
if (err) {
19+
return cb(err);
20+
}
21+
// Combine all the results into one object and return it
22+
cb(null, 'module.exports = ' + JSON.stringify(results.reduce(function(prev, result) {
23+
return assign({}, prev, result);
24+
}, json)));
25+
}
26+
);
27+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"b": true
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"c": true
3+
}

0 commit comments

Comments
 (0)