Skip to content

Commit fb49376

Browse files
committed
Added multi pass hot mode webpack#669
1 parent 18fe8e7 commit fb49376

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

lib/Compilation.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,10 @@ Compilation.prototype.seal = function seal(callback) {
510510
this.applyPlugins("before-hash");
511511
this.createHash();
512512
this.applyPlugins("after-hash");
513-
this.applyPlugins("before-chunk-assets");
514-
this.createChunkAssets();
513+
if(this.applyPluginsBailResult("should-generate-chunk-assets") !== false) {
514+
this.applyPlugins("before-chunk-assets");
515+
this.createChunkAssets();
516+
}
515517
this.applyPlugins("additional-chunk-assets", this.chunks);
516518
this.summarizeDependencies();
517519
if(shouldRecord)

lib/Compiler.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Watching.prototype._go = function() {
4242
this.running = true;
4343
this.invalid = false;
4444
this.compiler.applyPluginsAsync("watch-run", this, function(err) {
45-
this.compiler.compile(function(err, compilation) {
45+
this.compiler.compile(function onCompiled(err, compilation) {
4646
if(err) return this._done(err);
4747
if(this.invalid) return this._done();
4848

@@ -57,6 +57,20 @@ Watching.prototype._go = function() {
5757
this.compiler.emitRecords(function(err) {
5858
if(err) return this._done(err);
5959

60+
if(compilation.applyPluginsBailResult("need-additional-pass")) {
61+
compilation.needAdditionalPass = true;
62+
63+
var stats = compilation.getStats();
64+
stats.startTime = this.startTime;
65+
stats.endTime = new Date().getTime();
66+
this.compiler.applyPlugins("done", stats);
67+
68+
this.compiler.applyPluginsAsync("additional-pass", function(err) {
69+
if(err) return this._done(err);
70+
this.compiler.compile(onCompiled.bind(this));
71+
}.bind(this));
72+
return;
73+
}
6074
return this._done(null, compilation);
6175
}.bind(this));
6276
}.bind(this));
@@ -166,7 +180,7 @@ Compiler.prototype.run = function(callback) {
166180
this.readRecords(function(err) {
167181
if(err) return callback(err);
168182

169-
this.compile(function(err, compilation) {
183+
this.compile(function onCompiled(err, compilation) {
170184
if(err) return callback(err);
171185

172186
if(this.applyPluginsBailResult("should-emit", compilation) === false) {
@@ -180,6 +194,21 @@ Compiler.prototype.run = function(callback) {
180194
this.emitAssets(compilation, function(err) {
181195
if(err) return callback(err);
182196

197+
if(compilation.applyPluginsBailResult("need-additional-pass")) {
198+
compilation.needAdditionalPass = true;
199+
200+
var stats = compilation.getStats();
201+
stats.startTime = startTime;
202+
stats.endTime = new Date().getTime();
203+
this.applyPlugins("done", stats);
204+
205+
this.applyPluginsAsync("additional-pass", function(err) {
206+
if(err) return callback(err);
207+
this.compile(onCompiled.bind(this));
208+
}.bind(this));
209+
return;
210+
}
211+
183212
this.emitRecords(function(err) {
184213
if(err) return callback(err);
185214

lib/HotModuleReplacementPlugin.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ var RawSource = require("webpack-core/lib/RawSource");
1010
var ConstDependency = require("./dependencies/ConstDependency");
1111
var NullFactory = require("./NullFactory");
1212

13-
function HotModuleReplacementPlugin() {
13+
function HotModuleReplacementPlugin(options) {
14+
options = options || {};
15+
this.multiStep = options.multiStep;
1416
}
1517
module.exports = HotModuleReplacementPlugin;
1618

1719
HotModuleReplacementPlugin.prototype.apply = function(compiler) {
20+
var multiStep = this.multiStep;
1821
var hotUpdateChunkFilename = compiler.options.output.hotUpdateChunkFilename;
1922
var hotUpdateMainFilename = compiler.options.output.hotUpdateMainFilename;
2023
compiler.plugin("compilation", function(compilation, params) {
@@ -52,20 +55,33 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
5255
records.chunkModuleIds[chunk.id] = chunk.modules.map(function(m) { return m.id; });
5356
});
5457
});
58+
var initialPass = false;
59+
var recompilation = false;
5560
compilation.plugin("after-hash", function() {
5661
var records = this.records;
57-
if(!records) return;
62+
if(!records) return initialPass = true;
63+
if(!records.hash)
64+
initialPass = true;
5865
var lastHash = records.hash || "x";
5966
var preHash = records.preHash || "x";
6067
var prepreHash = records.prepreHash || "x";
6168
if(preHash === this.hash) {
69+
recompilation = true;
6270
this.modifyHash(prepreHash);
6371
return;
6472
}
6573
records.prepreHash = records.hash || "x";
6674
records.preHash = this.hash;
6775
this.modifyHash(records.prepreHash);
6876
});
77+
compilation.plugin("should-generate-chunk-assets", function() {
78+
if(multiStep && !recompilation && !initialPass)
79+
return false;
80+
});
81+
compilation.plugin("need-additional-pass", function() {
82+
if(multiStep && !recompilation && !initialPass)
83+
return true;
84+
});
6985
compilation.plugin("additional-chunk-assets", function() {
7086
var records = this.records;
7187
if(records.hash === this.hash) return;
@@ -184,7 +200,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
184200
params = [arg];
185201
}
186202
if(arg.isArray()){
187-
params = arg.items.filter(function(param) {
203+
params = arg.items.filter(function(param) {
188204
return param.isString();
189205
});
190206
}
@@ -437,11 +453,11 @@ var hotInitCode = Template.getFunctionContent(function() {
437453
options = {};
438454
callback = callback || function(err) { if(err) throw err };
439455
}
440-
456+
441457
function getAffectedStuff(module) {
442458
var outdatedModules = [module];
443459
var outdatedDependencies = {};
444-
460+
445461
var queue = outdatedModules.slice();
446462
while(queue.length > 0) {
447463
var moduleId = queue.pop();
@@ -472,7 +488,7 @@ var hotInitCode = Template.getFunctionContent(function() {
472488
queue.push(parentId);
473489
}
474490
}
475-
491+
476492
return [outdatedModules, outdatedDependencies];
477493
}
478494
function addAllToSet(a, b) {

lib/Stats.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Stats.prototype.toJson = function toJson(options, forToString) {
108108
if(showTimings && this.startTime && this.endTime) {
109109
obj.time = this.endTime - this.startTime;
110110
}
111+
if(compilation.needAdditionalPass) {
112+
obj.needAdditionalPass = true
113+
}
111114
if(showAssets) {
112115
var assetsByFile = {};
113116
obj.assetsByChunkName = {};
@@ -608,6 +611,9 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
608611
newline();
609612
});
610613
}
614+
if(obj.needAdditionalPass) {
615+
yellow("Compilation needs an additional pass and will compile again.");
616+
}
611617

612618
while(buf[buf.length-1] === "\n") buf.pop();
613619
return buf.join("");

0 commit comments

Comments
 (0)