Skip to content

Commit 88ad7ed

Browse files
committed
Merge branch 'refactoring/plugin-system-templates' into refactoring/plugin-system-all
2 parents 6ff8b37 + 56328a7 commit 88ad7ed

19 files changed

+111
-352
lines changed

lib/AmdMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AmdMainTemplatePlugin {
2626
).join(", ");
2727

2828
if(this.name) {
29-
const name = mainTemplate.applyPluginsWaterfall("asset-path", this.name, {
29+
const name = mainTemplate.getAssetPath(this.name, {
3030
hash,
3131
chunk
3232
});

lib/ChunkTemplate.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66

77
const ConcatSource = require("webpack-sources").ConcatSource;
88
const Template = require("./Template");
9+
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
10+
const SyncHook = require("tapable").SyncHook;
911

1012
module.exports = class ChunkTemplate extends Template {
1113
constructor(outputOptions) {
1214
super(outputOptions);
15+
this.hooks = {
16+
modules: new SyncWaterfallHook(["source", "chunk", "moduleTemplate", "dependencyTemplates"]),
17+
render: new SyncWaterfallHook(["source", "chunk", "moduleTemplate", "dependencyTemplates"]),
18+
renderWithEntry: new SyncWaterfallHook(["source", "chunk"]),
19+
hash: new SyncHook(["hash"]),
20+
hashForChunk: new SyncHook(["hash", "chunk"]),
21+
};
1322
}
1423

1524
getRenderManifest(options) {
@@ -59,10 +68,10 @@ module.exports = class ChunkTemplate extends Template {
5968

6069
renderJavascript(chunk, moduleTemplate, dependencyTemplates) {
6170
const moduleSources = this.renderChunkModules(chunk, m => true, moduleTemplate, dependencyTemplates);
62-
const core = this.applyPluginsWaterfall("modules", moduleSources, chunk, moduleTemplate, dependencyTemplates);
63-
let source = this.applyPluginsWaterfall("render", core, chunk, moduleTemplate, dependencyTemplates);
71+
const core = this.hooks.modules.call(moduleSources, chunk, moduleTemplate, dependencyTemplates);
72+
let source = this.hooks.render.call(core, chunk, moduleTemplate, dependencyTemplates);
6473
if(chunk.hasEntryModule()) {
65-
source = this.applyPluginsWaterfall("render-with-entry", source, chunk);
74+
source = this.hooks.renderWithEntry.call(source, chunk);
6675
}
6776
chunk.rendered = true;
6877
return new ConcatSource(source, ";");
@@ -75,11 +84,11 @@ module.exports = class ChunkTemplate extends Template {
7584
updateHash(hash) {
7685
hash.update("ChunkTemplate");
7786
hash.update("2");
78-
this.applyPlugins("hash", hash);
87+
this.hooks.hash.call(hash);
7988
}
8089

8190
updateHashForChunk(hash, chunk) {
8291
this.updateHash(hash);
83-
this.applyPlugins("hash-for-chunk", hash, chunk);
92+
this.hooks.hashForChunk.call(hash, chunk);
8493
}
8594
};

lib/Compilation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ class Compilation extends Tapable {
15911591
getPath(filename, data) {
15921592
data = data || {};
15931593
data.hash = data.hash || this.hash;
1594-
return this.mainTemplate.applyPluginsWaterfall("asset-path", filename, data);
1594+
return this.mainTemplate.getAssetPath(filename, data);
15951595
}
15961596

15971597
createChildCompiler(name, outputOptions, plugins) {

lib/HotModuleReplacementPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module.exports = class HotModuleReplacementPlugin {
162162
});
163163

164164
mainTemplate.plugin("bootstrap", (source, chunk, hash) => {
165-
source = mainTemplate.applyPluginsWaterfall("hot-bootstrap", source, chunk, hash);
165+
source = mainTemplate.hooks.hotBootstrap.call(source, chunk, hash);
166166
return mainTemplate.asString([
167167
source,
168168
"",

lib/HotUpdateChunkTemplate.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66

77
const Template = require("./Template");
88
const Chunk = require("./Chunk");
9+
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
10+
const SyncHook = require("tapable").SyncHook;
911

1012
module.exports = class HotUpdateChunkTemplate extends Template {
1113
constructor(outputOptions) {
1214
super(outputOptions);
15+
this.hooks = {
16+
modules: new SyncWaterfallHook(["source", "modules", "removedModules", "moduleTemplate", "dependencyTemplates"]),
17+
render: new SyncWaterfallHook(["source", "modules", "removedModules", "hash", "id", "moduleTemplate", "dependencyTemplates"]),
18+
hash: new SyncHook(["hash"]),
19+
};
1320
}
1421

1522
render(id, modules, removedModules, hash, moduleTemplate, dependencyTemplates) {
@@ -18,14 +25,14 @@ module.exports = class HotUpdateChunkTemplate extends Template {
1825
hotUpdateChunk.setModules(modules);
1926
hotUpdateChunk.removedModules = removedModules;
2027
const modulesSource = this.renderChunkModules(hotUpdateChunk, () => true, moduleTemplate, dependencyTemplates);
21-
const core = this.applyPluginsWaterfall("modules", modulesSource, modules, removedModules, moduleTemplate, dependencyTemplates);
22-
const source = this.applyPluginsWaterfall("render", core, modules, removedModules, hash, id, moduleTemplate, dependencyTemplates);
28+
const core = this.hooks.modules.call(modulesSource, modules, removedModules, moduleTemplate, dependencyTemplates);
29+
const source = this.hooks.render.call(core, modules, removedModules, hash, id, moduleTemplate, dependencyTemplates);
2330
return source;
2431
}
2532

2633
updateHash(hash) {
2734
hash.update("HotUpdateChunkTemplate");
2835
hash.update("1");
29-
this.applyPlugins("hash", hash);
36+
this.hooks.hash.call(hash);
3037
}
3138
};

lib/MainTemplate.js

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const ConcatSource = require("webpack-sources").ConcatSource;
88
const OriginalSource = require("webpack-sources").OriginalSource;
99
const PrefixSource = require("webpack-sources").PrefixSource;
1010
const Template = require("./Template");
11+
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
12+
const SyncHook = require("tapable").SyncHook;
13+
const SyncBailHook = require("tapable").SyncBailHook;
1114

1215
// require function shortcuts:
1316
// __webpack_require__.s = the module id of the entry point
@@ -28,6 +31,30 @@ const Template = require("./Template");
2831
module.exports = class MainTemplate extends Template {
2932
constructor(outputOptions) {
3033
super(outputOptions);
34+
this.hooks = {
35+
modules: new SyncWaterfallHook(["modules", "chunk", "hash", "moduleTemplate", "dependencyTemplates"]),
36+
moduleObj: new SyncWaterfallHook(["source", "chunk", "hash", "moduleIdExpression"]),
37+
requireEnsure: new SyncWaterfallHook(["source", "chunk", "hash", "chunkIdExpression"]),
38+
bootstrap: new SyncWaterfallHook(["source", "chunk", "hash", "moduleTemplate", "dependencyTemplates"]),
39+
localVars: new SyncWaterfallHook(["source", "chunk", "hash"]),
40+
require: new SyncWaterfallHook(["source", "chunk", "hash"]),
41+
requireExtensions: new SyncWaterfallHook(["source", "chunk", "hash"]),
42+
startup: new SyncWaterfallHook(["source", "chunk", "hash"]),
43+
render: new SyncWaterfallHook(["source", "chunk", "hash", "moduleTemplate", "dependencyTemplates"]),
44+
renderWithEntry: new SyncWaterfallHook(["source", "chunk", "hash"]),
45+
moduleRequire: new SyncWaterfallHook(["source", "chunk", "hash", "moduleIdExpression"]),
46+
addModule: new SyncWaterfallHook(["source", "chunk", "hash", "moduleIdExpression", "moduleExpression"]),
47+
currentHash: new SyncWaterfallHook(["source", "requestedLength"]),
48+
assetPath: new SyncWaterfallHook(["path", "options"]),
49+
hash: new SyncHook(["hash"]),
50+
hashForChunk: new SyncHook(["hash", "chunk"]),
51+
globalHashPaths: new SyncWaterfallHook(["paths"]),
52+
globalHash: new SyncBailHook(["chunk", "paths"]),
53+
54+
// TODO this should be moved somewhere else
55+
// It's weird here
56+
hotBootstrap: new SyncWaterfallHook(["source", "chunk", "hash"])
57+
};
3158
this.plugin("startup", (source, chunk, hash) => {
3259
const buf = [];
3360
if(chunk.entryModule) {
@@ -44,7 +71,7 @@ module.exports = class MainTemplate extends Template {
4471
source.add("/************************************************************************/\n");
4572
source.add("/******/ (");
4673
const modules = this.renderChunkModules(chunk, () => true, moduleTemplate, dependencyTemplates, "/******/ ");
47-
source.add(this.applyPluginsWaterfall("modules", modules, chunk, hash, moduleTemplate, dependencyTemplates));
74+
source.add(this.hooks.modules.call(modules, chunk, hash, moduleTemplate, dependencyTemplates));
4875
source.add(")");
4976
return source;
5077
});
@@ -64,7 +91,7 @@ module.exports = class MainTemplate extends Template {
6491
"}",
6592
"// Create a new module (and put it into the cache)",
6693
"var module = installedModules[moduleId] = {",
67-
this.indent(this.applyPluginsWaterfall("module-obj", "", chunk, hash, "moduleId")),
94+
this.indent(this.hooks.moduleObj.call("", chunk, hash, "moduleId")),
6895
"};",
6996
"",
7097
this.asString(outputOptions.strictModuleExceptionHandling ? [
@@ -106,7 +133,7 @@ module.exports = class MainTemplate extends Template {
106133
buf.push("// The chunk loading function for additional chunks");
107134
buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`);
108135
buf.push(this.indent("var promises = [];"));
109-
buf.push(this.indent(this.applyPluginsWaterfall("require-ensure", "", chunk, hash, "chunkId")));
136+
buf.push(this.indent(this.hooks.requireEnsure.call("", chunk, hash, "chunkId")));
110137
buf.push(this.indent("return Promise.all(promises);"));
111138
buf.push("};");
112139
}
@@ -211,37 +238,37 @@ module.exports = class MainTemplate extends Template {
211238

212239
render(hash, chunk, moduleTemplate, dependencyTemplates) {
213240
const buf = [];
214-
buf.push(this.applyPluginsWaterfall("bootstrap", "", chunk, hash, moduleTemplate, dependencyTemplates));
215-
buf.push(this.applyPluginsWaterfall("local-vars", "", chunk, hash));
241+
buf.push(this.hooks.bootstrap.call("", chunk, hash, moduleTemplate, dependencyTemplates));
242+
buf.push(this.hooks.localVars.call("", chunk, hash));
216243
buf.push("");
217244
buf.push("// The require function");
218245
buf.push(`function ${this.requireFn}(moduleId) {`);
219-
buf.push(this.indent(this.applyPluginsWaterfall("require", "", chunk, hash)));
246+
buf.push(this.indent(this.hooks.require.call("", chunk, hash)));
220247
buf.push("}");
221248
buf.push("");
222-
buf.push(this.asString(this.applyPluginsWaterfall("require-extensions", "", chunk, hash)));
249+
buf.push(this.asString(this.hooks.requireExtensions.call("", chunk, hash)));
223250
buf.push("");
224-
buf.push(this.asString(this.applyPluginsWaterfall("startup", "", chunk, hash)));
225-
let source = this.applyPluginsWaterfall("render", new OriginalSource(this.prefix(buf, " \t") + "\n", `webpack/bootstrap ${hash}`), chunk, hash, moduleTemplate, dependencyTemplates);
251+
buf.push(this.asString(this.hooks.startup.call("", chunk, hash)));
252+
let source = this.hooks.render.call(new OriginalSource(this.prefix(buf, " \t") + "\n", `webpack/bootstrap ${hash}`), chunk, hash, moduleTemplate, dependencyTemplates);
226253
if(chunk.hasEntryModule()) {
227-
source = this.applyPluginsWaterfall("render-with-entry", source, chunk, hash);
254+
source = this.hooks.renderWithEntry.call(source, chunk, hash);
228255
}
229256
if(!source) throw new Error("Compiler error: MainTemplate plugin 'render' should return something");
230257
chunk.rendered = true;
231258
return new ConcatSource(source, ";");
232259
}
233260

234261
renderRequireFunctionForModule(hash, chunk, varModuleId) {
235-
return this.applyPluginsWaterfall("module-require", this.requireFn, chunk, hash, varModuleId);
262+
return this.hooks.moduleRequire.call(this.requireFn, chunk, hash, varModuleId);
236263
}
237264

238265
renderAddModule(hash, chunk, varModuleId, varModule) {
239-
return this.applyPluginsWaterfall("add-module", `modules[${varModuleId}] = ${varModule};`, chunk, hash, varModuleId, varModule);
266+
return this.hooks.addModule.call(`modules[${varModuleId}] = ${varModule};`, chunk, hash, varModuleId, varModule);
240267
}
241268

242269
renderCurrentHashCode(hash, length) {
243270
length = length || Infinity;
244-
return this.applyPluginsWaterfall("current-hash", JSON.stringify(hash.substr(0, length)), length);
271+
return this.hooks.currentHash.call(JSON.stringify(hash.substr(0, length)), length);
245272
}
246273

247274
entryPointInChildren(chunk) {
@@ -259,23 +286,27 @@ module.exports = class MainTemplate extends Template {
259286
}
260287

261288
getPublicPath(options) {
262-
return this.applyPluginsWaterfall("asset-path", this.outputOptions.publicPath || "", options);
289+
return this.hooks.assetPath.call(this.outputOptions.publicPath || "", options);
290+
}
291+
292+
getAssetPath(path, options) {
293+
return this.hooks.assetPath.call(path, options);
263294
}
264295

265296
updateHash(hash) {
266297
hash.update("maintemplate");
267298
hash.update("3");
268299
hash.update(this.outputOptions.publicPath + "");
269-
this.applyPlugins("hash", hash);
300+
this.hooks.hash.call(hash);
270301
}
271302

272303
updateHashForChunk(hash, chunk) {
273304
this.updateHash(hash);
274-
this.applyPlugins("hash-for-chunk", hash, chunk);
305+
this.hooks.hashForChunk.call(hash, chunk);
275306
}
276307

277308
useChunkHash(chunk) {
278-
const paths = this.applyPluginsWaterfall("global-hash-paths", []);
279-
return !this.applyPluginsBailResult("global-hash", chunk, paths);
309+
const paths = this.hooks.globalHashPaths.call([]);
310+
return !this.hooks.globalHash.call(chunk, paths);
280311
}
281312
};

lib/ModuleTemplate.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@
55
"use strict";
66

77
const Template = require("./Template");
8+
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
9+
const SyncHook = require("tapable").SyncHook;
810

911
module.exports = class ModuleTemplate extends Template {
1012
constructor(outputOptions) {
1113
super(outputOptions);
14+
this.hooks = {
15+
content: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),
16+
module: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),
17+
render: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),
18+
package: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),
19+
hash: new SyncHook(["hash"])
20+
};
1221
}
1322

1423
render(module, dependencyTemplates, options) {
1524
const moduleSource = module.source(dependencyTemplates, this.outputOptions, this.requestShortener);
16-
const moduleSourcePostContent = this.applyPluginsWaterfall("content", moduleSource, module, options, dependencyTemplates);
17-
const moduleSourcePostModule = this.applyPluginsWaterfall("module", moduleSourcePostContent, module, options, dependencyTemplates);
18-
const moduleSourcePostRender = this.applyPluginsWaterfall("render", moduleSourcePostModule, module, options, dependencyTemplates);
19-
return this.applyPluginsWaterfall("package", moduleSourcePostRender, module, options, dependencyTemplates);
25+
const moduleSourcePostContent = this.hooks.content.call(moduleSource, module, options, dependencyTemplates);
26+
const moduleSourcePostModule = this.hooks.module.call(moduleSourcePostContent, module, options, dependencyTemplates);
27+
const moduleSourcePostRender = this.hooks.render.call(moduleSourcePostModule, module, options, dependencyTemplates);
28+
return this.hooks.package.call(moduleSourcePostRender, module, options, dependencyTemplates);
2029
}
2130

2231
updateHash(hash) {
2332
hash.update("1");
24-
this.applyPlugins("hash", hash);
33+
this.hooks.hash.call(hash);
2534
}
2635
};

lib/SetVarMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SetVarMainTemplatePlugin {
1515
apply(compilation) {
1616
const mainTemplate = compilation.mainTemplate;
1717
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
18-
const varExpression = mainTemplate.applyPluginsWaterfall("asset-path", this.varExpression, {
18+
const varExpression = mainTemplate.getAssetPath(this.varExpression, {
1919
hash,
2020
chunk
2121
});

lib/Template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
"use strict";
66

7-
const Tapable = require("tapable-old");
7+
const Tapable = require("tapable").Tapable;
88
const ConcatSource = require("webpack-sources").ConcatSource;
99

1010
const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);

lib/UmdMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class UmdMainTemplatePlugin {
5959
}
6060

6161
function replaceKeys(str) {
62-
return mainTemplate.applyPluginsWaterfall("asset-path", str, {
62+
return mainTemplate.getAssetPath(str, {
6363
hash,
6464
chunk
6565
});

0 commit comments

Comments
 (0)