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]) {
template.hooks.renderWithEntry.tap(
"AmdMainTemplatePlugin",
onRenderWithEntry
);
}
mainTemplate.hooks.renderWithEntry.tap(
"AmdMainTemplatePlugin",
onRenderWithEntry
);
chunkTemplate.hooks.renderWithEntry.tap(
"AmdMainTemplatePlugin",
onRenderWithEntry
);
mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
hash.update("exports amd");

View File

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

View File

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

View File

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

View File

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

View File

@ -14,6 +14,7 @@ const { SyncWaterfallHook, SyncHook } = require("tapable");
/** @typedef {import("./Module")} Module */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("./util/createHash").Hash} Hash */
/**
* @typedef {Object} RenderContext
@ -37,6 +38,7 @@ module.exports = class ModuleTemplate {
render: new SyncWaterfallHook(["source", "module", "context"]),
/** @type {SyncWaterfallHook<Source, Module, RenderContext>} */
package: new SyncWaterfallHook(["source", "module", "context"]),
/** @type {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) {
hash.update("1");
this.hooks.hash.call(hash);

View File

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

View File

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