| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 8 | const RuntimeModule = require("../RuntimeModule");
|
|---|
| 9 | const Template = require("../Template");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 12 | /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
|
|---|
| 13 |
|
|---|
| 14 | class EnsureChunkRuntimeModule extends RuntimeModule {
|
|---|
| 15 | /**
|
|---|
| 16 | * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
|
|---|
| 17 | */
|
|---|
| 18 | constructor(runtimeRequirements) {
|
|---|
| 19 | super("ensure chunk");
|
|---|
| 20 | /** @type {ReadOnlyRuntimeRequirements} */
|
|---|
| 21 | this.runtimeRequirements = runtimeRequirements;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Generates runtime code for this runtime module.
|
|---|
| 26 | * @returns {string | null} runtime code
|
|---|
| 27 | */
|
|---|
| 28 | generate() {
|
|---|
| 29 | const compilation = /** @type {Compilation} */ (this.compilation);
|
|---|
| 30 | const { runtimeTemplate } = compilation;
|
|---|
| 31 | // Check if there are non initial chunks which need to be imported using require-ensure
|
|---|
| 32 | if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) {
|
|---|
| 33 | const withFetchPriority = this.runtimeRequirements.has(
|
|---|
| 34 | RuntimeGlobals.hasFetchPriority
|
|---|
| 35 | );
|
|---|
| 36 | const handlers = RuntimeGlobals.ensureChunkHandlers;
|
|---|
| 37 | return Template.asString([
|
|---|
| 38 | `${handlers} = {};`,
|
|---|
| 39 | "// This file contains only the entry chunk.",
|
|---|
| 40 | "// The chunk loading function for additional chunks",
|
|---|
| 41 | `${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.basicFunction(
|
|---|
| 42 | `chunkId${withFetchPriority ? ", fetchPriority" : ""}`,
|
|---|
| 43 | [
|
|---|
| 44 | `return Promise.all(Object.keys(${handlers}).reduce(${runtimeTemplate.basicFunction(
|
|---|
| 45 | "promises, key",
|
|---|
| 46 | [
|
|---|
| 47 | `${handlers}[key](chunkId, promises${
|
|---|
| 48 | withFetchPriority ? ", fetchPriority" : ""
|
|---|
| 49 | });`,
|
|---|
| 50 | "return promises;"
|
|---|
| 51 | ]
|
|---|
| 52 | )}, []));`
|
|---|
| 53 | ]
|
|---|
| 54 | )};`
|
|---|
| 55 | ]);
|
|---|
| 56 | }
|
|---|
| 57 | // There ensureChunk is used somewhere in the tree, so we need an empty requireEnsure
|
|---|
| 58 | // function. This can happen with multiple entrypoints.
|
|---|
| 59 | return Template.asString([
|
|---|
| 60 | "// The chunk loading function for additional chunks",
|
|---|
| 61 | "// Since all referenced chunks are already included",
|
|---|
| 62 | "// in this file, this function is empty here.",
|
|---|
| 63 | `${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.returningFunction(
|
|---|
| 64 | "Promise.resolve()"
|
|---|
| 65 | )};`
|
|---|
| 66 | ]);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | module.exports = EnsureChunkRuntimeModule;
|
|---|