Skip to content

Commit 53bb15b

Browse files
authored
Merge pull request webpack#3934 from timse/refactor-watching-in-compiler
Refactor _done of Watching in compiler
2 parents b67d61a + ab30c6b commit 53bb15b

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

lib/Compilation.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ const ModuleDependencyError = require("./ModuleDependencyError");
1414
const Module = require("./Module");
1515
const Chunk = require("./Chunk");
1616
const Entrypoint = require("./Entrypoint");
17-
const Stats = require("./Stats");
1817
const MainTemplate = require("./MainTemplate");
1918
const ChunkTemplate = require("./ChunkTemplate");
2019
const HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
2120
const ModuleTemplate = require("./ModuleTemplate");
2221
const Dependency = require("./Dependency");
2322
const ChunkRenderError = require("./ChunkRenderError");
2423
const CachedSource = require("webpack-sources").CachedSource;
24+
const Stats = require("./Stats");
2525

2626
function byId(a, b) {
2727
if(a.id < b.id) return -1;
@@ -82,6 +82,10 @@ class Compilation extends Tapable {
8282
this.dependencyTemplates = new Map();
8383
}
8484

85+
getStats() {
86+
return new Stats(this);
87+
}
88+
8589
templatesPlugin(name, fn) {
8690
this.mainTemplate.plugin(name, fn);
8791
this.chunkTemplate.plugin(name, fn);
@@ -1214,10 +1218,6 @@ class Compilation extends Tapable {
12141218
return this.mainTemplate.applyPluginsWaterfall("asset-path", filename, data);
12151219
}
12161220

1217-
getStats() {
1218-
return new Stats(this);
1219-
}
1220-
12211221
createChildCompiler(name, outputOptions) {
12221222
return this.compiler.createChildCompiler(this, name, outputOptions);
12231223
}

lib/Compiler.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ var path = require("path");
66
var Tapable = require("tapable");
77

88
var Compilation = require("./Compilation");
9-
9+
var Stats = require("./Stats");
1010
var NormalModuleFactory = require("./NormalModuleFactory");
1111
var ContextModuleFactory = require("./ContextModuleFactory");
1212

1313
function Watching(compiler, watchOptions, handler) {
1414
this.startTime = null;
1515
this.invalid = false;
16-
this.error = null;
17-
this.stats = null;
1816
this.handler = handler;
1917
this.closed = false;
2018
if(typeof watchOptions === "number") {
@@ -61,7 +59,7 @@ Watching.prototype._go = function() {
6159
if(compilation.applyPluginsBailResult("need-additional-pass")) {
6260
compilation.needAdditionalPass = true;
6361

64-
var stats = compilation.getStats();
62+
var stats = new Stats(compilation);
6563
stats.startTime = self.startTime;
6664
stats.endTime = new Date().getTime();
6765
self.compiler.applyPlugins("done", stats);
@@ -79,22 +77,29 @@ Watching.prototype._go = function() {
7977
});
8078
};
8179

80+
Watching.prototype._getStats = function(compilation) {
81+
var stats = new Stats(compilation);
82+
stats.startTime = this.startTime;
83+
stats.endTime = new Date().getTime();
84+
return stats;
85+
};
86+
8287
Watching.prototype._done = function(err, compilation) {
8388
this.running = false;
8489
if(this.invalid) return this._go();
85-
this.error = err || null;
86-
this.stats = compilation ? compilation.getStats() : null;
87-
if(this.stats) {
88-
this.stats.startTime = this.startTime;
89-
this.stats.endTime = new Date().getTime();
90+
91+
var stats = this._getStats(compilation);
92+
if(err) {
93+
this.compiler.applyPlugins("failed", err);
94+
this.handler(err, stats);
95+
return;
9096
}
91-
if(this.stats)
92-
this.compiler.applyPlugins("done", this.stats);
93-
else
94-
this.compiler.applyPlugins("failed", this.error);
95-
this.handler(this.error, this.stats);
96-
if(!this.error && !this.closed)
97+
98+
this.compiler.applyPlugins("done", stats);
99+
this.handler(null, stats);
100+
if(!this.closed) {
97101
this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies);
102+
}
98103
};
99104

100105
Watching.prototype.watch = function(files, dirs, missing) {
@@ -232,7 +237,7 @@ Compiler.prototype.run = function(callback) {
232237
if(err) return callback(err);
233238

234239
if(self.applyPluginsBailResult("should-emit", compilation) === false) {
235-
var stats = compilation.getStats();
240+
var stats = new Stats(compilation);
236241
stats.startTime = startTime;
237242
stats.endTime = new Date().getTime();
238243
self.applyPlugins("done", stats);
@@ -245,7 +250,7 @@ Compiler.prototype.run = function(callback) {
245250
if(compilation.applyPluginsBailResult("need-additional-pass")) {
246251
compilation.needAdditionalPass = true;
247252

248-
var stats = compilation.getStats();
253+
var stats = new Stats(compilation);
249254
stats.startTime = startTime;
250255
stats.endTime = new Date().getTime();
251256
self.applyPlugins("done", stats);
@@ -260,7 +265,7 @@ Compiler.prototype.run = function(callback) {
260265
self.emitRecords(function(err) {
261266
if(err) return callback(err);
262267

263-
var stats = compilation.getStats();
268+
var stats = new Stats(compilation);
264269
stats.startTime = startTime;
265270
stats.endTime = new Date().getTime();
266271
self.applyPlugins("done", stats);

0 commit comments

Comments
 (0)