| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- */
- "use strict";
- const RuntimeGlobals = require("../RuntimeGlobals");
- const RuntimeModule = require("../RuntimeModule");
- const Template = require("../Template");
- /** @import Compilation from "../Compilation" */
- /** @import { ChunkChildIdsByOrdersMap } from "../Chunk" */
- class ChunkPreloadTriggerRuntimeModule extends RuntimeModule {
- /**
- * @param {ChunkChildIdsByOrdersMap} chunkMap map from chunk to chunks
- * @param {string=} handlerKey the `ensureChunkHandlers` key to install under (default `"preload"`); `"cssPreload"` preloads only the CSS handler so it stays CSS-only even when the build also registers a JS preload handler
- */
- constructor(chunkMap, handlerKey = "preload") {
- super(`chunk ${handlerKey} trigger`, RuntimeModule.STAGE_TRIGGER);
- /** @type {ChunkChildIdsByOrdersMap} */
- this.chunkMap = chunkMap;
- /** @type {string} */
- this.handlerKey = handlerKey;
- }
- /**
- * Generates runtime code for this runtime module.
- * @returns {string | null} runtime code
- */
- generate() {
- const { chunkMap, handlerKey } = this;
- const compilation = /** @type {Compilation} */ (this.compilation);
- const { runtimeTemplate } = compilation;
- // `cssPreload` calls only the CSS preload handler (`preloadChunkHandlers.s`)
- // so it stays CSS-only regardless of a JS preload handler registered
- // elsewhere; plain `preload` fans out to every handler via `preloadChunk`.
- const body =
- handlerKey === "cssPreload"
- ? [
- `var handler = ${RuntimeGlobals.preloadChunkHandlers}.s;`,
- "var chunks = chunkToChildrenMap[chunkId];",
- "if (handler && Array.isArray(chunks)) chunks.forEach(handler);"
- ]
- : [
- "var chunks = chunkToChildrenMap[chunkId];",
- `Array.isArray(chunks) && chunks.map(${RuntimeGlobals.preloadChunk});`
- ];
- return Template.asString([
- `var chunkToChildrenMap = ${JSON.stringify(chunkMap, null, "\t")};`,
- `${
- RuntimeGlobals.ensureChunkHandlers
- }.${handlerKey} = ${runtimeTemplate.basicFunction("chunkId", body)};`
- ]);
- }
- }
- module.exports = ChunkPreloadTriggerRuntimeModule;
|