Skip to content

Commit fb689d9

Browse files
committed
Merge branch 'refactoring/plugin-system-compiler' into refactoring/plugin-system-all
2 parents cf4f795 + 6689539 commit fb689d9

7 files changed

Lines changed: 104 additions & 44 deletions

File tree

‎lib/Compiler.js‎

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
const path = require("path");
88
const util = require("util");
9-
const Tapable = require("tapable-old");
9+
const Tapable = require("tapable").Tapable;
10+
const SyncHook = require("tapable").SyncHook;
11+
const SyncBailHook = require("tapable").SyncBailHook;
12+
const AsyncParallelHook = require("tapable").AsyncParallelHook;
13+
const AsyncSeriesHook = require("tapable").AsyncSeriesHook;
1014

1115
const Compilation = require("./Compilation");
1216
const Stats = require("./Stats");
@@ -46,13 +50,13 @@ class Watching {
4650
this.startTime = Date.now();
4751
this.running = true;
4852
this.invalid = false;
49-
this.compiler.applyPluginsAsync("watch-run", this.compiler, err => {
53+
this.compiler.hooks.watchRun.callAsync(this.compiler, err => {
5054
if(err) return this._done(err);
5155
const onCompiled = (err, compilation) => {
5256
if(err) return this._done(err);
5357
if(this.invalid) return this._done();
5458

55-
if(this.compiler.applyPluginsBailResult("should-emit", compilation) === false) {
59+
if(this.compiler.hooks.shouldEmit.call(compilation) === false) {
5660
return this._done(null, compilation);
5761
}
5862

@@ -69,9 +73,9 @@ class Watching {
6973
const stats = new Stats(compilation);
7074
stats.startTime = this.startTime;
7175
stats.endTime = Date.now();
72-
this.compiler.applyPlugins("done", stats);
76+
this.compiler.hooks.done.call(stats);
7377

74-
this.compiler.applyPluginsAsync("additional-pass", err => {
78+
this.compiler.hooks.additionalPass.callAsync(err => {
7579
if(err) return this._done(err);
7680
this.compiler.compile(onCompiled);
7781
});
@@ -98,12 +102,12 @@ class Watching {
98102

99103
const stats = compilation ? this._getStats(compilation) : null;
100104
if(err) {
101-
this.compiler.applyPlugins("failed", err);
105+
this.compiler.hooks.failed.call(err);
102106
this.handler(err, stats);
103107
return;
104108
}
105109

106-
this.compiler.applyPlugins("done", stats);
110+
this.compiler.hooks.done.call(stats);
107111
this.handler(null, stats);
108112
if(!this.closed) {
109113
this.watch(Array.from(compilation.fileDependencies), Array.from(compilation.contextDependencies), Array.from(compilation.missingDependencies));
@@ -123,7 +127,7 @@ class Watching {
123127
this.compiler.contextTimestamps = contextTimestamps;
124128
this.invalidate();
125129
}, (fileName, changeTime) => {
126-
this.compiler.applyPlugins("invalid", fileName, changeTime);
130+
this.compiler.hooks.invalid.call(fileName, changeTime);
127131
});
128132
}
129133

@@ -159,11 +163,11 @@ class Watching {
159163
if(this.running) {
160164
this.invalid = true;
161165
this._done = () => {
162-
this.compiler.applyPlugins("watch-close");
166+
this.compiler.hooks.watchClose.call();
163167
callback();
164168
};
165169
} else {
166-
this.compiler.applyPlugins("watch-close");
170+
this.compiler.hooks.watchClose.call();
167171
callback();
168172
}
169173
}
@@ -172,6 +176,51 @@ class Watching {
172176
class Compiler extends Tapable {
173177
constructor() {
174178
super();
179+
this.hooks = {
180+
shouldEmit: new SyncBailHook(["compilation"]),
181+
done: new SyncHook(["stats"]),
182+
additionalPass: new AsyncSeriesHook([]),
183+
beforeRun: new AsyncSeriesHook(["compilation"]),
184+
run: new AsyncSeriesHook(["compilation"]),
185+
emit: new AsyncSeriesHook(["compilation"]),
186+
afterEmit: new AsyncSeriesHook(["compilation"]),
187+
thisCompilation: new SyncHook(["compilation", "params"]),
188+
compilation: new SyncHook(["compilation", "params"]),
189+
normalModuleFactory: new SyncHook(["normalModuleFactory"]),
190+
contextModuleFactory: new SyncHook(["contextModulefactory"]),
191+
beforeCompile: new AsyncSeriesHook(["params"]),
192+
compile: new SyncHook(["params"]),
193+
make: new AsyncParallelHook(["compilation"]),
194+
afterCompile: new AsyncSeriesHook(["compilation"]),
195+
watchRun: new AsyncSeriesHook(["compiler"]),
196+
failed: new SyncHook(["error"]),
197+
invalid: new SyncHook(["filename", "changeTime"]),
198+
watchClose: new SyncHook([]),
199+
200+
// TODO the following hooks are weirdly located here
201+
// TODO move them for webpack 5
202+
environment: new SyncHook([]),
203+
afterEnvironment: new SyncHook([]),
204+
afterPlugins: new SyncHook(["compiler"]),
205+
afterResolvers: new SyncHook(["compiler"]),
206+
entryOption: new SyncBailHook(["context", "entry"]),
207+
};
208+
this._pluginCompat.tap("Compiler", options => {
209+
switch(options.name) {
210+
case "additional-pass":
211+
case "before-run":
212+
case "run":
213+
case "emit":
214+
case "after-emit":
215+
case "before-compile":
216+
case "make":
217+
case "after-compile":
218+
case "watch-run":
219+
options.async = true;
220+
break;
221+
}
222+
});
223+
175224
this.outputPath = "";
176225
this.outputFileSystem = null;
177226
this.inputFileSystem = null;
@@ -287,11 +336,11 @@ class Compiler extends Tapable {
287336
const onCompiled = (err, compilation) => {
288337
if(err) return callback(err);
289338

290-
if(this.applyPluginsBailResult("should-emit", compilation) === false) {
339+
if(this.hooks.shouldEmit.call(compilation) === false) {
291340
const stats = new Stats(compilation);
292341
stats.startTime = startTime;
293342
stats.endTime = Date.now();
294-
this.applyPlugins("done", stats);
343+
this.hooks.done.call(stats);
295344
return callback(null, stats);
296345
}
297346

@@ -304,9 +353,9 @@ class Compiler extends Tapable {
304353
const stats = new Stats(compilation);
305354
stats.startTime = startTime;
306355
stats.endTime = Date.now();
307-
this.applyPlugins("done", stats);
356+
this.hooks.done.call(stats);
308357

309-
this.applyPluginsAsync("additional-pass", err => {
358+
this.hooks.additionalPass.callAsync(err => {
310359
if(err) return callback(err);
311360
this.compile(onCompiled);
312361
});
@@ -319,16 +368,16 @@ class Compiler extends Tapable {
319368
const stats = new Stats(compilation);
320369
stats.startTime = startTime;
321370
stats.endTime = Date.now();
322-
this.applyPlugins("done", stats);
371+
this.hooks.done.call(stats);
323372
return callback(null, stats);
324373
});
325374
});
326375
};
327376

328-
this.applyPluginsAsync("before-run", this, err => {
377+
this.hooks.beforeRun.callAsync(this, err => {
329378
if(err) return callback(err);
330379

331-
this.applyPluginsAsync("run", this, err => {
380+
this.hooks.run.callAsync(this, err => {
332381
if(err) return callback(err);
333382

334383
this.readRecords(err => {
@@ -405,15 +454,15 @@ class Compiler extends Tapable {
405454
}, err => {
406455
if(err) return callback(err);
407456

408-
this.applyPluginsAsyncSeries1("after-emit", compilation, err => {
457+
this.hooks.afterEmit.callAsync(compilation, err => {
409458
if(err) return callback(err);
410459

411460
return callback();
412461
});
413462
});
414463
};
415464

416-
this.applyPluginsAsync("emit", compilation, err => {
465+
this.hooks.emit.callAsync(compilation, err => {
417466
if(err) return callback(err);
418467
outputPath = compilation.getPath(this.outputPath);
419468
this.outputFileSystem.mkdirp(outputPath, emitFiles);
@@ -470,9 +519,11 @@ class Compiler extends Tapable {
470519
if(Array.isArray(plugins)) {
471520
plugins.forEach(plugin => childCompiler.apply(plugin));
472521
}
473-
for(const name in this._plugins) {
474-
if(["make", "compile", "emit", "after-emit", "invalid", "done", "this-compilation"].indexOf(name) < 0)
475-
childCompiler._plugins[name] = this._plugins[name].slice();
522+
for(const name in this.hooks) {
523+
if(["make", "compile", "emit", "afterEmit", "invalid", "done", "thisCompilation"].indexOf(name) < 0) {
524+
if(childCompiler.hooks[name])
525+
childCompiler.hooks[name].taps = this.hooks[name].taps.slice();
526+
}
476527
}
477528
childCompiler.name = compilerName;
478529
childCompiler.outputPath = this.outputPath;
@@ -516,20 +567,20 @@ class Compiler extends Tapable {
516567
compilation.name = this.name;
517568
compilation.records = this.records;
518569
compilation.compilationDependencies = params.compilationDependencies;
519-
this.applyPlugins("this-compilation", compilation, params);
520-
this.applyPlugins("compilation", compilation, params);
570+
this.hooks.thisCompilation.call(compilation, params);
571+
this.hooks.compilation.call(compilation, params);
521572
return compilation;
522573
}
523574

524575
createNormalModuleFactory() {
525576
const normalModuleFactory = new NormalModuleFactory(this.options.context, this.resolverFactory, this.options.module || {});
526-
this.applyPlugins("normal-module-factory", normalModuleFactory);
577+
this.hooks.normalModuleFactory.call(normalModuleFactory);
527578
return normalModuleFactory;
528579
}
529580

530581
createContextModuleFactory() {
531582
const contextModuleFactory = new ContextModuleFactory(this.resolverFactory, this.inputFileSystem);
532-
this.applyPlugins("context-module-factory", contextModuleFactory);
583+
this.hooks.contextModuleFactory.call(contextModuleFactory);
533584
return contextModuleFactory;
534585
}
535586

@@ -544,22 +595,22 @@ class Compiler extends Tapable {
544595

545596
compile(callback) {
546597
const params = this.newCompilationParams();
547-
this.applyPluginsAsync("before-compile", params, err => {
598+
this.hooks.beforeCompile.callAsync(params, err => {
548599
if(err) return callback(err);
549600

550-
this.applyPlugins("compile", params);
601+
this.hooks.compile.call(params);
551602

552603
const compilation = this.newCompilation(params);
553604

554-
this.applyPluginsParallel("make", compilation, err => {
605+
this.hooks.make.callAsync(compilation, err => {
555606
if(err) return callback(err);
556607

557608
compilation.finish();
558609

559610
compilation.seal(err => {
560611
if(err) return callback(err);
561612

562-
this.applyPluginsAsync("after-compile", compilation, err => {
613+
this.hooks.afterCompile.callAsync(compilation, err => {
563614
if(err) return callback(err);
564615

565616
return callback(null, compilation);

‎lib/MultiCompiler.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
*/
55
"use strict";
66

7-
const Tapable = require("tapable-old");
7+
const Tapable = require("tapable").Tapable;
8+
const SyncHook = require("tapable").SyncHook;
89
const asyncLib = require("async");
910
const MultiWatching = require("./MultiWatching");
1011
const MultiStats = require("./MultiStats");
1112

1213
module.exports = class MultiCompiler extends Tapable {
1314
constructor(compilers) {
1415
super();
16+
this.hooks = {
17+
done: new SyncHook(["stats"]),
18+
invalid: new SyncHook([]),
19+
watchClose: new SyncHook([])
20+
};
1521
if(!Array.isArray(compilers)) {
1622
compilers = Object.keys(compilers).map((name) => {
1723
compilers[name].name = name;
@@ -30,15 +36,15 @@ module.exports = class MultiCompiler extends Tapable {
3036
}
3137
compilerStats[idx] = stats;
3238
if(doneCompilers === this.compilers.length) {
33-
this.applyPlugins("done", new MultiStats(compilerStats));
39+
this.hooks.done.call(new MultiStats(compilerStats));
3440
}
3541
});
3642
compiler.plugin("invalid", () => {
3743
if(compilerDone) {
3844
compilerDone = false;
3945
doneCompilers--;
4046
}
41-
this.applyPlugins("invalid");
47+
this.hooks.invalid.call();
4248
});
4349
});
4450
}

‎lib/MultiWatching.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MultiWatching {
2222
asyncLib.forEach(this.watchings, (watching, finishedCallback) => {
2323
watching.close(finishedCallback);
2424
}, err => {
25-
this.compiler.applyPlugins("watch-close");
25+
this.compiler.hooks.watchClose.call();
2626
callback(err);
2727
});
2828

‎lib/WebpackOptionsApply.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class WebpackOptionsApply extends OptionsApply {
261261
);
262262

263263
compiler.apply(new EntryOptionPlugin());
264-
compiler.applyPluginsBailResult("entry-option", options.context, options.entry);
264+
compiler.hooks.entryOption.call(options.context, options.entry);
265265

266266
compiler.apply(
267267
new CompatibilityPlugin(),
@@ -330,7 +330,7 @@ class WebpackOptionsApply extends OptionsApply {
330330
compiler.apply(new CachePlugin(typeof options.cache === "object" ? options.cache : null));
331331
}
332332

333-
compiler.applyPlugins("after-plugins", compiler);
333+
compiler.hooks.afterPlugins.call(compiler);
334334
if(!compiler.inputFileSystem) throw new Error("No input filesystem provided");
335335
compiler.resolverFactory.plugin("resolve-options normal", resolveOptions => {
336336
return Object.assign({
@@ -348,7 +348,7 @@ class WebpackOptionsApply extends OptionsApply {
348348
fileSystem: compiler.inputFileSystem
349349
}, options.resolveLoader, resolveOptions);
350350
});
351-
compiler.applyPlugins("after-resolvers", compiler);
351+
compiler.hooks.afterResolvers.call(compiler);
352352
return options;
353353
}
354354
}

‎lib/webpack.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const webpack = (options, callback) => {
3131
if(options.plugins && Array.isArray(options.plugins)) {
3232
compiler.apply.apply(compiler, options.plugins);
3333
}
34-
compiler.applyPlugins("environment");
35-
compiler.applyPlugins("after-environment");
34+
compiler.hooks.environment.call();
35+
compiler.hooks.afterEnvironment.call();
3636
compiler.options = new WebpackOptionsApply().process(options, compiler);
3737
} else {
3838
throw new Error("Invalid argument: options");

‎test/MultiCompiler.unittest.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ describe("MultiCompiler", () => {
277277
});
278278

279279
it("returns a multi-watching object", () => {
280-
const result = JSON.stringify(env.result);
281-
result.should.be.exactly("{\"watchings\":[\"compiler1\",\"compiler2\"],\"compiler\":{\"_plugins\":{},\"compilers\":[{\"name\":\"compiler1\"},{\"name\":\"compiler2\"}]}}");
280+
const result = env.result;
281+
result.constructor.name.should.be.exactly("MultiWatching");
282282
});
283283

284284
it("calls watch on each compiler with original options", () => {

‎test/MultiWatching.unittest.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

3-
const Tapable = require("tapable-old");
3+
const Tapable = require("tapable").Tapable;
4+
const SyncHook = require("tapable").SyncHook;
45
require("should");
56
const sinon = require("sinon");
67
const MultiWatching = require("../lib/MultiWatching");
@@ -14,9 +15,11 @@ const createWatching = function() {
1415

1516
const createCompiler = () => {
1617
const compiler = {
17-
_plugins: {}
18+
hooks: {
19+
watchClose: new SyncHook([])
20+
}
1821
};
19-
Tapable.mixin(compiler);
22+
Tapable.addCompatLayer(compiler);
2023
return compiler;
2124
};
2225

0 commit comments

Comments
 (0)