webpack/lib/ChunkTemplate.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-03-26 23:54:41 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2013-03-26 23:54:41 +08:00
2018-06-26 14:27:44 +08:00
const { SyncWaterfallHook, SyncHook } = require("tapable");
2013-03-26 23:54:41 +08:00
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Module")} Module} */
2018-06-25 16:44:54 +08:00
/** @typedef {import("./util/createHash").Hash} Hash} */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates} */
/** @typedef {import("webpack-sources").Source} Source} */
/** @typedef {import("./ModuleTemplate").RenderContext} RenderContext} */
/**
* @typedef {Object} RenderManifestOptions
* @property {Chunk} chunk the chunk used to render
2018-06-25 16:44:54 +08:00
* @property {string} hash
* @property {string} fullHash
* @property {TODO} outputOptions
* @property {{javascript: ModuleTemplate, webassembly: ModuleTemplate}} moduleTemplates
* @property {DependencyTemplates} dependencyTemplates
*/
2018-06-26 14:27:44 +08:00
module.exports = class ChunkTemplate {
constructor(outputOptions) {
2017-12-07 16:42:33 +08:00
this.outputOptions = outputOptions || {};
2018-07-30 20:25:40 +08:00
this.hooks = Object.freeze({
/** @type {SyncWaterfallHook<TODO[], RenderManifestOptions>} */
renderManifest: new SyncWaterfallHook(["result", "options"]),
/** @type {SyncWaterfallHook<Source, ModuleTemplate, RenderContext>} */
2018-02-25 09:00:20 +08:00
modules: new SyncWaterfallHook([
"source",
"moduleTemplate",
"renderContext"
2018-02-25 09:00:20 +08:00
]),
/** @type {SyncWaterfallHook<Source, ModuleTemplate, RenderContext>} */
2018-02-25 09:00:20 +08:00
render: new SyncWaterfallHook([
"source",
"moduleTemplate",
"renderContext"
2018-02-25 09:00:20 +08:00
]),
2017-11-29 01:43:01 +08:00
renderWithEntry: new SyncWaterfallHook(["source", "chunk"]),
hash: new SyncHook(["hash"]),
2018-02-25 09:00:20 +08:00
hashForChunk: new SyncHook(["hash", "chunk"])
2018-07-30 20:25:40 +08:00
});
}
/**
*
* @param {RenderManifestOptions} options render manifest options
* @returns {TODO[]} returns render manifest
*/
2017-10-30 20:56:57 +08:00
getRenderManifest(options) {
const result = [];
this.hooks.renderManifest.call(result, options);
2017-10-30 20:56:57 +08:00
return result;
}
2018-06-25 16:44:54 +08:00
/**
* Updates hash with information from this template
* @param {Hash} hash the hash to update
* @returns {void}
*/
updateHash(hash) {
hash.update("ChunkTemplate");
hash.update("2");
2017-11-29 01:43:01 +08:00
this.hooks.hash.call(hash);
}
2018-06-25 16:44:54 +08:00
/**
* Updates hash with chunk-specific information from this template
* @param {Hash} hash the hash to update
* @param {Chunk} chunk the chunk
* @returns {void}
*/
updateHashForChunk(hash, chunk) {
this.updateHash(hash);
2017-11-29 01:43:01 +08:00
this.hooks.hashForChunk.call(hash, chunk);
}
};