[79a0317] | 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 | this.runtimeRequirements = runtimeRequirements;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @returns {string | null} runtime code
|
---|
| 25 | */
|
---|
| 26 | generate() {
|
---|
| 27 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
| 28 | const { runtimeTemplate } = compilation;
|
---|
| 29 | // Check if there are non initial chunks which need to be imported using require-ensure
|
---|
| 30 | if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) {
|
---|
| 31 | const withFetchPriority = this.runtimeRequirements.has(
|
---|
| 32 | RuntimeGlobals.hasFetchPriority
|
---|
| 33 | );
|
---|
| 34 | const handlers = RuntimeGlobals.ensureChunkHandlers;
|
---|
| 35 | return Template.asString([
|
---|
| 36 | `${handlers} = {};`,
|
---|
| 37 | "// This file contains only the entry chunk.",
|
---|
| 38 | "// The chunk loading function for additional chunks",
|
---|
| 39 | `${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.basicFunction(
|
---|
| 40 | `chunkId${withFetchPriority ? ", fetchPriority" : ""}`,
|
---|
| 41 | [
|
---|
| 42 | `return Promise.all(Object.keys(${handlers}).reduce(${runtimeTemplate.basicFunction(
|
---|
| 43 | "promises, key",
|
---|
| 44 | [
|
---|
| 45 | `${handlers}[key](chunkId, promises${
|
---|
| 46 | withFetchPriority ? ", fetchPriority" : ""
|
---|
| 47 | });`,
|
---|
| 48 | "return promises;"
|
---|
| 49 | ]
|
---|
| 50 | )}, []));`
|
---|
| 51 | ]
|
---|
| 52 | )};`
|
---|
| 53 | ]);
|
---|
| 54 | }
|
---|
| 55 | // There ensureChunk is used somewhere in the tree, so we need an empty requireEnsure
|
---|
| 56 | // function. This can happen with multiple entrypoints.
|
---|
| 57 | return Template.asString([
|
---|
| 58 | "// The chunk loading function for additional chunks",
|
---|
| 59 | "// Since all referenced chunks are already included",
|
---|
| 60 | "// in this file, this function is empty here.",
|
---|
| 61 | `${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.returningFunction(
|
---|
| 62 | "Promise.resolve()"
|
---|
| 63 | )};`
|
---|
| 64 | ]);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | module.exports = EnsureChunkRuntimeModule;
|
---|