Skip to content

Commit 9f6a601

Browse files
uncleyoTheLarkInn
authored andcommitted
Fix multiconfig watch (webpack#3737)
* Fix MultiCompiler watch mode stats output Previously, stats would contain stale configuration child stats. This is very confusing, since it looks as if some things are emitted when they shouldn't be. Add support for making assertions to stats in watch tests. * Fix test hanging on Windows/Linux Parallel watchpack's (happens when MultiCompiler is in use) do not emit events in watch tests, unless some timeout is added. This does not seem to be an issue on macOS. * Correctly close watcher in watch tests This fixes macOS build, where changes keep being triggered
1 parent 70bf6c2 commit 9f6a601

7 files changed

Lines changed: 54 additions & 14 deletions

File tree

‎lib/MultiCompiler.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,13 @@ MultiCompiler.prototype.watch = function(watchOptions, handler) {
143143
handler(err);
144144
if(stats) {
145145
allStats[compilerIdx] = stats;
146-
compilerStatus[compilerIdx] = true;
146+
compilerStatus[compilerIdx] = "new";
147147
if(compilerStatus.every(Boolean)) {
148-
var multiStats = new MultiStats(allStats);
148+
var freshStats = allStats.filter(function(s, idx) {
149+
return compilerStatus[idx] === "new";
150+
});
151+
compilerStatus.fill(true);
152+
var multiStats = new MultiStats(freshStats);
149153
handler(null, multiStats)
150154
}
151155
}

‎test/WatchTestCases.test.js‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,27 @@ describe("WatchTestCases", function() {
8989
var configPath = path.join(testDirectory, "webpack.config.js");
9090
if(fs.existsSync(configPath))
9191
options = require(configPath);
92-
if(!options.context) options.context = tempDirectory;
93-
if(!options.entry) options.entry = "./index.js";
94-
if(!options.target) options.target = "async-node";
95-
if(!options.output) options.output = {};
96-
if(!options.output.path) options.output.path = outputDirectory;
97-
if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
98-
if(!options.output.filename) options.output.filename = "bundle.js";
99-
92+
var applyConfig = function(options) {
93+
if(!options.context) options.context = tempDirectory;
94+
if(!options.entry) options.entry = "./index.js";
95+
if(!options.target) options.target = "async-node";
96+
if(!options.output) options.output = {};
97+
if(!options.output.path) options.output.path = outputDirectory;
98+
if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
99+
if(!options.output.filename) options.output.filename = "bundle.js";
100+
}
101+
if(Array.isArray(options)) {
102+
options.forEach(applyConfig)
103+
} else {
104+
applyConfig(options)
105+
}
100106
var runIdx = 0;
101107
var run = runs[runIdx];
102108
var lastHash = "";
103109
copyDiff(path.join(testDirectory, run.name), tempDirectory);
104110

105111
var compiler = webpack(options);
106-
compiler.watch({}, function(err, stats) {
112+
var watching = compiler.watch({}, function(err, stats) {
107113
if(stats.hash === lastHash)
108114
return;
109115
lastHash = stats.hash;
@@ -142,11 +148,11 @@ describe("WatchTestCases", function() {
142148
var p = path.join(currentDirectory, module);
143149
content = fs.readFileSync(p, "utf-8");
144150
}
145-
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP) {" + content + "\n})", p);
151+
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON) {" + content + "\n})", p);
146152
var module = {
147153
exports: {}
148154
};
149-
fn.call(module.exports, _require.bind(null, path.dirname(p)), module, module.exports, path.dirname(p), p, _it, run.name);
155+
fn.call(module.exports, _require.bind(null, path.dirname(p)), module, module.exports, path.dirname(p), p, _it, run.name, jsonStats);
150156
return module.exports;
151157
} else if(testConfig.modules && module in testConfig.modules) {
152158
return testConfig.modules[module];
@@ -166,8 +172,11 @@ describe("WatchTestCases", function() {
166172
runIdx++;
167173
if(runIdx < runs.length) {
168174
run = runs[runIdx];
169-
copyDiff(path.join(testDirectory, run.name), tempDirectory);
175+
setTimeout(function() {
176+
copyDiff(path.join(testDirectory, run.name), tempDirectory);
177+
}, 50);
170178
} else {
179+
watching.close();
171180
process.nextTick(done);
172181
}
173182
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "0";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require("./changing-file")
2+
it("should watch for changes", function() {
3+
switch(WATCH_STEP) {
4+
case "0":
5+
STATS_JSON.children.should.have.size(2);
6+
break;
7+
case "1":
8+
STATS_JSON.children.should.have.size(1);
9+
break;
10+
}
11+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "0";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "1";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = [{
2+
name: "changing",
3+
entry: "./index.js",
4+
output: {
5+
filename: "./bundle.js"
6+
}
7+
}, {
8+
name: "static",
9+
entry: "./static-file.js",
10+
output: {
11+
filename: "./static.js"
12+
}
13+
}];

0 commit comments

Comments
 (0)