| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const RuntimeModule = require("../RuntimeModule");
|
|---|
| 8 | const Template = require("../Template");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 11 |
|
|---|
| 12 | class ChunkPrefetchFunctionRuntimeModule extends RuntimeModule {
|
|---|
| 13 | /**
|
|---|
| 14 | * @param {"prefetch" | "preload"} type "prefetch" or "preload" chunk type function
|
|---|
| 15 | * @param {string} runtimeFunction the runtime function name
|
|---|
| 16 | * @param {string} runtimeHandlers the runtime handlers
|
|---|
| 17 | */
|
|---|
| 18 | constructor(type, runtimeFunction, runtimeHandlers) {
|
|---|
| 19 | super(`chunk ${type} function`);
|
|---|
| 20 | /** @type {string} */
|
|---|
| 21 | this.runtimeFunction = runtimeFunction;
|
|---|
| 22 | /** @type {string} */
|
|---|
| 23 | this.runtimeHandlers = runtimeHandlers;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Generates runtime code for this runtime module.
|
|---|
| 28 | * @returns {string | null} runtime code
|
|---|
| 29 | */
|
|---|
| 30 | generate() {
|
|---|
| 31 | const { runtimeFunction, runtimeHandlers } = this;
|
|---|
| 32 | const compilation = /** @type {Compilation} */ (this.compilation);
|
|---|
| 33 | const { runtimeTemplate } = compilation;
|
|---|
| 34 | return Template.asString([
|
|---|
| 35 | `${runtimeHandlers} = {};`,
|
|---|
| 36 | `${runtimeFunction} = ${runtimeTemplate.basicFunction("chunkId", [
|
|---|
| 37 | // map is shorter than forEach
|
|---|
| 38 | `Object.keys(${runtimeHandlers}).map(${runtimeTemplate.basicFunction(
|
|---|
| 39 | "key",
|
|---|
| 40 | `${runtimeHandlers}[key](chunkId);`
|
|---|
| 41 | )});`
|
|---|
| 42 | ])}`
|
|---|
| 43 | ]);
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | module.exports = ChunkPrefetchFunctionRuntimeModule;
|
|---|