Add hook types for templates

This commit is contained in:
Florent Cailhol 2018-11-02 15:39:51 +01:00
parent c828bfa5c7
commit 84c915b191
8 changed files with 61 additions and 25 deletions

View File

@ -91,12 +91,15 @@ class AmdMainTemplatePlugin {
} }
}; };
for (const template of [mainTemplate, chunkTemplate]) { mainTemplate.hooks.renderWithEntry.tap(
template.hooks.renderWithEntry.tap( "AmdMainTemplatePlugin",
"AmdMainTemplatePlugin", onRenderWithEntry
onRenderWithEntry );
);
} chunkTemplate.hooks.renderWithEntry.tap(
"AmdMainTemplatePlugin",
onRenderWithEntry
);
mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => { mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
hash.update("exports amd"); hash.update("exports amd");

View File

@ -35,8 +35,11 @@ module.exports = class ChunkTemplate {
"moduleTemplate", "moduleTemplate",
"renderContext" "renderContext"
]), ]),
/** @type {SyncWaterfallHook<Source, Chunk>} */
renderWithEntry: new SyncWaterfallHook(["source", "chunk"]), renderWithEntry: new SyncWaterfallHook(["source", "chunk"]),
/** @type {SyncHook<Hash>} */
hash: new SyncHook(["hash"]), hash: new SyncHook(["hash"]),
/** @type {SyncHook<Hash, Chunk>} */
hashForChunk: new SyncHook(["hash", "chunk"]) hashForChunk: new SyncHook(["hash", "chunk"])
}); });
} }

View File

@ -33,16 +33,19 @@ class ExportPropertyMainTemplatePlugin {
const { mainTemplate, chunkTemplate } = compilation; const { mainTemplate, chunkTemplate } = compilation;
const onRenderWithEntry = (source, chunk, hash) => { const onRenderWithEntry = (source, chunk, hash) => {
const postfix = `${accessorToObjectAccess([].concat(this.property))}`; const postfix = accessorToObjectAccess([].concat(this.property));
return new ConcatSource(source, postfix); return new ConcatSource(source, postfix);
}; };
for (const template of [mainTemplate, chunkTemplate]) { mainTemplate.hooks.renderWithEntry.tap(
template.hooks.renderWithEntry.tap( "ExportPropertyMainTemplatePlugin",
"ExportPropertyMainTemplatePlugin", onRenderWithEntry
onRenderWithEntry );
);
} chunkTemplate.hooks.renderWithEntry.tap(
"ExportPropertyMainTemplatePlugin",
onRenderWithEntry
);
mainTemplate.hooks.hash.tap("ExportPropertyMainTemplatePlugin", hash => { mainTemplate.hooks.hash.tap("ExportPropertyMainTemplatePlugin", hash => {
hash.update("export property"); hash.update("export property");

View File

@ -11,6 +11,7 @@ const Template = require("./Template");
/** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */ /** @typedef {import("./ModuleTemplate")} ModuleTemplate */
/** @typedef {import("./ModuleTemplate").RenderContext} RenderContext */ /** @typedef {import("./ModuleTemplate").RenderContext} RenderContext */
/** @typedef {import("./util/createHash").Hash} Hash */
module.exports = class HotUpdateChunkTemplate { module.exports = class HotUpdateChunkTemplate {
constructor(outputOptions) { constructor(outputOptions) {
@ -29,6 +30,7 @@ module.exports = class HotUpdateChunkTemplate {
"renderContext", "renderContext",
"hash" "hash"
]), ]),
/** @type {SyncHook<Hash>} */
hash: new SyncHook(["hash"]) hash: new SyncHook(["hash"])
}); });
} }
@ -60,6 +62,11 @@ module.exports = class HotUpdateChunkTemplate {
return source; return source;
} }
/**
* Updates hash with information from this template
* @param {Hash} hash the hash to update
* @returns {void}
*/
updateHash(hash) { updateHash(hash) {
hash.update("HotUpdateChunkTemplate"); hash.update("HotUpdateChunkTemplate");
hash.update("1"); hash.update("1");

View File

@ -102,6 +102,7 @@ module.exports = class MainTemplate {
]), ]),
/** @type {SyncWaterfallHook<string, RenderBootstrapContext>} */ /** @type {SyncWaterfallHook<string, RenderBootstrapContext>} */
bootstrap: new SyncWaterfallHook(["source", "renderContext"]), bootstrap: new SyncWaterfallHook(["source", "renderContext"]),
/** @type {SyncWaterfallHook<string, Chunk, string>} */
localVars: new SyncWaterfallHook(["source", "chunk", "hash"]), localVars: new SyncWaterfallHook(["source", "chunk", "hash"]),
/** @type {SyncWaterfallHook<string, RenderBootstrapContext>} */ /** @type {SyncWaterfallHook<string, RenderBootstrapContext>} */
require: new SyncWaterfallHook(["source", "renderContext"]), require: new SyncWaterfallHook(["source", "renderContext"]),
@ -117,7 +118,9 @@ module.exports = class MainTemplate {
"moduleTemplate", "moduleTemplate",
"renderContext" "renderContext"
]), ]),
/** @type {SyncWaterfallHook<Source, Chunk, string>} */
renderWithEntry: new SyncWaterfallHook(["source", "chunk", "hash"]), renderWithEntry: new SyncWaterfallHook(["source", "chunk", "hash"]),
/** @type {SyncWaterfallHook<string, Chunk, string, number|string>} */
moduleRequire: new SyncWaterfallHook([ moduleRequire: new SyncWaterfallHook([
"source", "source",
"chunk", "chunk",
@ -130,9 +133,13 @@ module.exports = class MainTemplate {
"expressions", "expressions",
"renderContext" "renderContext"
]), ]),
/** @type {SyncWaterfallHook<string, number>} */
currentHash: new SyncWaterfallHook(["source", "requestedLength"]), currentHash: new SyncWaterfallHook(["source", "requestedLength"]),
/** @type {SyncWaterfallHook<string, object>} */
assetPath: new SyncWaterfallHook(["path", "options"]), assetPath: new SyncWaterfallHook(["path", "options"]),
/** @type {SyncHook<Hash>} */
hash: new SyncHook(["hash"]), hash: new SyncHook(["hash"]),
/** @type {SyncHook<Hash, Chunk>} */
hashForChunk: new SyncHook(["hash", "chunk"]) hashForChunk: new SyncHook(["hash", "chunk"])
}); });
this.hooks.startup.tap( this.hooks.startup.tap(

View File

@ -14,6 +14,7 @@ const { SyncWaterfallHook, SyncHook } = require("tapable");
/** @typedef {import("./Module")} Module */ /** @typedef {import("./Module")} Module */
/** @typedef {import("./ModuleGraph")} ModuleGraph */ /** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */ /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("./util/createHash").Hash} Hash */
/** /**
* @typedef {Object} RenderContext * @typedef {Object} RenderContext
@ -37,6 +38,7 @@ module.exports = class ModuleTemplate {
render: new SyncWaterfallHook(["source", "module", "context"]), render: new SyncWaterfallHook(["source", "module", "context"]),
/** @type {SyncWaterfallHook<Source, Module, RenderContext>} */ /** @type {SyncWaterfallHook<Source, Module, RenderContext>} */
package: new SyncWaterfallHook(["source", "module", "context"]), package: new SyncWaterfallHook(["source", "module", "context"]),
/** @type {SyncHook<Hash>} */
hash: new SyncHook(["hash"]) hash: new SyncHook(["hash"])
}); });
} }
@ -83,6 +85,11 @@ module.exports = class ModuleTemplate {
} }
} }
/**
* Updates hash with information from this template
* @param {Hash} hash the hash to update
* @returns {void}
*/
updateHash(hash) { updateHash(hash) {
hash.update("1"); hash.update("1");
this.hooks.hash.call(hash); this.hooks.hash.call(hash);

View File

@ -45,12 +45,15 @@ class SetVarMainTemplatePlugin {
} }
}; };
for (const template of [mainTemplate, chunkTemplate]) { mainTemplate.hooks.renderWithEntry.tap(
template.hooks.renderWithEntry.tap( "SetVarMainTemplatePlugin",
"SetVarMainTemplatePlugin", onRenderWithEntry
onRenderWithEntry );
);
} chunkTemplate.hooks.renderWithEntry.tap(
"SetVarMainTemplatePlugin",
onRenderWithEntry
);
mainTemplate.hooks.hash.tap("SetVarMainTemplatePlugin", hash => { mainTemplate.hooks.hash.tap("SetVarMainTemplatePlugin", hash => {
hash.update("set var"); hash.update("set var");

View File

@ -295,12 +295,15 @@ class UmdMainTemplatePlugin {
); );
}; };
for (const template of [mainTemplate, chunkTemplate]) { mainTemplate.hooks.renderWithEntry.tap(
template.hooks.renderWithEntry.tap( "UmdMainTemplatePlugin",
"UmdMainTemplatePlugin", onRenderWithEntry
onRenderWithEntry );
);
} chunkTemplate.hooks.renderWithEntry.tap(
"UmdMainTemplatePlugin",
onRenderWithEntry
);
mainTemplate.hooks.hash.tap("UmdMainTemplatePlugin", hash => { mainTemplate.hooks.hash.tap("UmdMainTemplatePlugin", hash => {
hash.update("umd"); hash.update("umd");