webpack/lib/ChunkTemplate.js

83 lines
2.5 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} */
2019-07-17 22:02:33 +08:00
/** @typedef {import("./util/Hash")} Hash} */
/** @typedef {import("webpack-sources").Source} Source} */
/** @typedef {import("./ModuleTemplate").RenderContext} RenderContext} */
/** @typedef {import("./MainTemplate").UpdateHashForChunkContext} UpdateHashForChunkContext} */
/** @typedef {import("./Template").RenderManifestOptions} RenderManifestOptions} */
/** @typedef {import("./Template").RenderManifestEntry} RenderManifestEntry} */
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({
2018-12-09 19:54:17 +08:00
/** @type {SyncWaterfallHook<[RenderManifestEntry[], RenderManifestOptions]>} */
renderManifest: new SyncWaterfallHook(["result", "options"]),
2018-12-09 19:54:17 +08:00
/** @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
]),
2018-12-09 19:54:17 +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
]),
2018-12-09 19:54:17 +08:00
/** @type {SyncWaterfallHook<[Source, Chunk]>} */
2017-11-29 01:43:01 +08:00
renderWithEntry: new SyncWaterfallHook(["source", "chunk"]),
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Hash]>} */
2017-11-29 01:43:01 +08:00
hash: new SyncHook(["hash"]),
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Hash, Chunk]>} */
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 {RenderManifestEntry[]} 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");
2018-11-24 02:22:29 +08:00
hash.update("3");
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
* @param {UpdateHashForChunkContext} context options object
2018-06-25 16:44:54 +08:00
* @returns {void}
*/
updateHashForChunk(hash, chunk, context) {
this.updateHash(hash);
2017-11-29 01:43:01 +08:00
this.hooks.hashForChunk.call(hash, chunk);
}
};