Skip to content

Commit 0014042

Browse files
committed
Move Watching to its own file
1 parent 7ed2bd2 commit 0014042

File tree

3 files changed

+164
-165
lines changed

3 files changed

+164
-165
lines changed

lib/Compiler.js

Lines changed: 2 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -14,166 +14,14 @@ const AsyncSeriesHook = require("tapable").AsyncSeriesHook;
1414

1515
const Compilation = require("./Compilation");
1616
const Stats = require("./Stats");
17+
const Watching = require("./Watching");
1718
const NormalModuleFactory = require("./NormalModuleFactory");
1819
const ContextModuleFactory = require("./ContextModuleFactory");
1920
const ResolverFactory = require("./ResolverFactory");
2021

2122
const RequestShortener = require("./RequestShortener");
2223
const makePathsRelative = require("./util/identifier").makePathsRelative;
2324

24-
class Watching {
25-
constructor(compiler, watchOptions, handler) {
26-
this.startTime = null;
27-
this.invalid = false;
28-
this.handler = handler;
29-
this.callbacks = [];
30-
this.closed = false;
31-
if(typeof watchOptions === "number") {
32-
this.watchOptions = {
33-
aggregateTimeout: watchOptions
34-
};
35-
} else if(watchOptions && typeof watchOptions === "object") {
36-
this.watchOptions = Object.assign({}, watchOptions);
37-
} else {
38-
this.watchOptions = {};
39-
}
40-
this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200;
41-
this.compiler = compiler;
42-
this.running = true;
43-
this.compiler.readRecords(err => {
44-
if(err) return this._done(err);
45-
46-
this._go();
47-
});
48-
}
49-
50-
_go() {
51-
this.startTime = Date.now();
52-
this.running = true;
53-
this.invalid = false;
54-
this.compiler.hooks.watchRun.callAsync(this.compiler, err => {
55-
if(err) return this._done(err);
56-
const onCompiled = (err, compilation) => {
57-
if(err) return this._done(err);
58-
if(this.invalid) return this._done();
59-
60-
if(this.compiler.hooks.shouldEmit.call(compilation) === false) {
61-
return this._done(null, compilation);
62-
}
63-
64-
this.compiler.emitAssets(compilation, err => {
65-
if(err) return this._done(err);
66-
if(this.invalid) return this._done();
67-
68-
this.compiler.emitRecords(err => {
69-
if(err) return this._done(err);
70-
71-
if(compilation.hooks.needAdditionalPass.call()) {
72-
compilation.needAdditionalPass = true;
73-
74-
const stats = new Stats(compilation);
75-
stats.startTime = this.startTime;
76-
stats.endTime = Date.now();
77-
this.compiler.hooks.done.call(stats);
78-
79-
this.compiler.hooks.additionalPass.callAsync(err => {
80-
if(err) return this._done(err);
81-
this.compiler.compile(onCompiled);
82-
});
83-
return;
84-
}
85-
return this._done(null, compilation);
86-
});
87-
});
88-
};
89-
this.compiler.compile(onCompiled);
90-
});
91-
}
92-
93-
_getStats(compilation) {
94-
const stats = new Stats(compilation);
95-
stats.startTime = this.startTime;
96-
stats.endTime = Date.now();
97-
return stats;
98-
}
99-
100-
_done(err, compilation) {
101-
this.running = false;
102-
if(this.invalid) return this._go();
103-
104-
const stats = compilation ? this._getStats(compilation) : null;
105-
if(err) {
106-
this.compiler.hooks.failed.call(err);
107-
this.handler(err, stats);
108-
return;
109-
}
110-
111-
this.compiler.hooks.done.call(stats);
112-
this.handler(null, stats);
113-
if(!this.closed) {
114-
this.watch(Array.from(compilation.fileDependencies), Array.from(compilation.contextDependencies), Array.from(compilation.missingDependencies));
115-
}
116-
this.callbacks.forEach(cb => cb());
117-
this.callbacks.length = 0;
118-
}
119-
120-
watch(files, dirs, missing) {
121-
this.pausedWatcher = null;
122-
this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, (err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) => {
123-
this.pausedWatcher = this.watcher;
124-
this.watcher = null;
125-
if(err) return this.handler(err);
126-
127-
this.compiler.fileTimestamps = fileTimestamps;
128-
this.compiler.contextTimestamps = contextTimestamps;
129-
this.invalidate();
130-
}, (fileName, changeTime) => {
131-
this.compiler.hooks.invalid.call(fileName, changeTime);
132-
});
133-
}
134-
135-
invalidate(callback) {
136-
if(callback) {
137-
this.callbacks.push(callback);
138-
}
139-
if(this.watcher) {
140-
this.pausedWatcher = this.watcher;
141-
this.watcher.pause();
142-
this.watcher = null;
143-
}
144-
if(this.running) {
145-
this.invalid = true;
146-
return false;
147-
} else {
148-
this._go();
149-
}
150-
}
151-
152-
close(callback) {
153-
if(callback === undefined) callback = () => {};
154-
155-
this.closed = true;
156-
if(this.watcher) {
157-
this.watcher.close();
158-
this.watcher = null;
159-
}
160-
if(this.pausedWatcher) {
161-
this.pausedWatcher.close();
162-
this.pausedWatcher = null;
163-
}
164-
if(this.running) {
165-
this.invalid = true;
166-
this._done = () => {
167-
this.compiler.hooks.watchClose.call();
168-
callback();
169-
};
170-
} else {
171-
this.compiler.hooks.watchClose.call();
172-
callback();
173-
}
174-
}
175-
}
176-
17725
class Compiler extends Tapable {
17826
constructor(context) {
17927
super();
@@ -331,8 +179,7 @@ class Compiler extends Tapable {
331179
watch(watchOptions, handler) {
332180
this.fileTimestamps = {};
333181
this.contextTimestamps = {};
334-
const watching = new Watching(this, watchOptions, handler);
335-
return watching;
182+
return new Watching(this, watchOptions, handler);
336183
}
337184

338185
run(callback) {
@@ -626,5 +473,4 @@ class Compiler extends Tapable {
626473
}
627474
}
628475

629-
Compiler.Watching = Watching;
630476
module.exports = Compiler;

lib/Watching.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const Stats = require("./Stats");
8+
9+
class Watching {
10+
constructor(compiler, watchOptions, handler) {
11+
this.startTime = null;
12+
this.invalid = false;
13+
this.handler = handler;
14+
this.callbacks = [];
15+
this.closed = false;
16+
if(typeof watchOptions === "number") {
17+
this.watchOptions = {
18+
aggregateTimeout: watchOptions
19+
};
20+
} else if(watchOptions && typeof watchOptions === "object") {
21+
this.watchOptions = Object.assign({}, watchOptions);
22+
} else {
23+
this.watchOptions = {};
24+
}
25+
this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200;
26+
this.compiler = compiler;
27+
this.running = true;
28+
this.compiler.readRecords(err => {
29+
if(err) return this._done(err);
30+
31+
this._go();
32+
});
33+
}
34+
35+
_go() {
36+
this.startTime = Date.now();
37+
this.running = true;
38+
this.invalid = false;
39+
this.compiler.hooks.watchRun.callAsync(this.compiler, err => {
40+
if(err) return this._done(err);
41+
const onCompiled = (err, compilation) => {
42+
if(err) return this._done(err);
43+
if(this.invalid) return this._done();
44+
45+
if(this.compiler.hooks.shouldEmit.call(compilation) === false) {
46+
return this._done(null, compilation);
47+
}
48+
49+
this.compiler.emitAssets(compilation, err => {
50+
if(err) return this._done(err);
51+
if(this.invalid) return this._done();
52+
53+
this.compiler.emitRecords(err => {
54+
if(err) return this._done(err);
55+
56+
if(compilation.hooks.needAdditionalPass.call()) {
57+
compilation.needAdditionalPass = true;
58+
59+
const stats = new Stats(compilation);
60+
stats.startTime = this.startTime;
61+
stats.endTime = Date.now();
62+
this.compiler.hooks.done.call(stats);
63+
64+
this.compiler.hooks.additionalPass.callAsync(err => {
65+
if(err) return this._done(err);
66+
this.compiler.compile(onCompiled);
67+
});
68+
return;
69+
}
70+
return this._done(null, compilation);
71+
});
72+
});
73+
};
74+
this.compiler.compile(onCompiled);
75+
});
76+
}
77+
78+
_getStats(compilation) {
79+
const stats = new Stats(compilation);
80+
stats.startTime = this.startTime;
81+
stats.endTime = Date.now();
82+
return stats;
83+
}
84+
85+
_done(err, compilation) {
86+
this.running = false;
87+
if(this.invalid) return this._go();
88+
89+
const stats = compilation ? this._getStats(compilation) : null;
90+
if(err) {
91+
this.compiler.hooks.failed.call(err);
92+
this.handler(err, stats);
93+
return;
94+
}
95+
96+
this.compiler.hooks.done.call(stats);
97+
this.handler(null, stats);
98+
if(!this.closed) {
99+
this.watch(Array.from(compilation.fileDependencies), Array.from(compilation.contextDependencies), Array.from(compilation.missingDependencies));
100+
}
101+
this.callbacks.forEach(cb => cb());
102+
this.callbacks.length = 0;
103+
}
104+
105+
watch(files, dirs, missing) {
106+
this.pausedWatcher = null;
107+
this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, (err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) => {
108+
this.pausedWatcher = this.watcher;
109+
this.watcher = null;
110+
if(err) return this.handler(err);
111+
112+
this.compiler.fileTimestamps = fileTimestamps;
113+
this.compiler.contextTimestamps = contextTimestamps;
114+
this.invalidate();
115+
}, (fileName, changeTime) => {
116+
this.compiler.hooks.invalid.call(fileName, changeTime);
117+
});
118+
}
119+
120+
invalidate(callback) {
121+
if(callback) {
122+
this.callbacks.push(callback);
123+
}
124+
if(this.watcher) {
125+
this.pausedWatcher = this.watcher;
126+
this.watcher.pause();
127+
this.watcher = null;
128+
}
129+
if(this.running) {
130+
this.invalid = true;
131+
return false;
132+
} else {
133+
this._go();
134+
}
135+
}
136+
137+
close(callback) {
138+
if(callback === undefined) callback = () => {};
139+
140+
this.closed = true;
141+
if(this.watcher) {
142+
this.watcher.close();
143+
this.watcher = null;
144+
}
145+
if(this.pausedWatcher) {
146+
this.pausedWatcher.close();
147+
this.pausedWatcher = null;
148+
}
149+
if(this.running) {
150+
this.invalid = true;
151+
this._done = () => {
152+
this.compiler.hooks.watchClose.call();
153+
callback();
154+
};
155+
} else {
156+
this.compiler.hooks.watchClose.call();
157+
callback();
158+
}
159+
}
160+
}
161+
162+
module.exports = Watching;

test/Compiler.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const sinon = require("sinon");
77

88
const webpack = require("../");
99
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
10-
const Compiler = require("../lib/Compiler");
1110
const MemoryFs = require("memory-fs");
1211

1312
describe("Compiler", () => {
@@ -273,14 +272,6 @@ describe("Compiler", () => {
273272
}
274273
});
275274
});
276-
describe("static method", () => {
277-
it("should have an method, Watching", (done) => {
278-
const actual = new Compiler.Watching(compiler, 1000, err => err);
279-
actual.running.should.be.exactly(true);
280-
actual.constructor.name.should.be.exactly("Watching");
281-
done();
282-
});
283-
});
284275
describe("constructor", () => {
285276
it("constructs Watching.watchOptions correctly when passed a number, string, or object for watchOptions", (done) => {
286277
const Watching1 = compiler.watch(1000, err => err);

0 commit comments

Comments
 (0)