2018-11-23 16:37:33 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const RuntimeModule = require("../RuntimeModule");
|
|
|
|
const Template = require("../Template");
|
|
|
|
|
2023-06-13 01:24:59 +08:00
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
|
2023-06-13 01:24:59 +08:00
|
|
|
|
2018-11-23 16:37:33 +08:00
|
|
|
class EnsureChunkRuntimeModule extends RuntimeModule {
|
2023-05-25 03:37:58 +08:00
|
|
|
/**
|
2024-03-18 23:28:40 +08:00
|
|
|
* @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
|
2023-05-25 03:37:58 +08:00
|
|
|
*/
|
2018-12-29 19:48:59 +08:00
|
|
|
constructor(runtimeRequirements) {
|
2018-11-23 16:37:33 +08:00
|
|
|
super("ensure chunk");
|
2018-12-29 19:48:59 +08:00
|
|
|
this.runtimeRequirements = runtimeRequirements;
|
2018-11-23 16:37:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-17 01:13:03 +08:00
|
|
|
* @returns {string | null} runtime code
|
2018-11-23 16:37:33 +08:00
|
|
|
*/
|
|
|
|
generate() {
|
2023-06-13 01:24:59 +08:00
|
|
|
const compilation = /** @type {Compilation} */ (this.compilation);
|
|
|
|
const { runtimeTemplate } = compilation;
|
2018-11-23 16:37:33 +08:00
|
|
|
// Check if there are non initial chunks which need to be imported using require-ensure
|
2018-12-29 19:48:59 +08:00
|
|
|
if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) {
|
2023-06-13 05:17:53 +08:00
|
|
|
const withFetchPriority = this.runtimeRequirements.has(
|
|
|
|
RuntimeGlobals.hasFetchPriority
|
|
|
|
);
|
2018-11-23 16:37:33 +08:00
|
|
|
const handlers = RuntimeGlobals.ensureChunkHandlers;
|
|
|
|
return Template.asString([
|
2018-11-29 21:42:16 +08:00
|
|
|
`${handlers} = {};`,
|
2018-11-23 16:37:33 +08:00
|
|
|
"// This file contains only the entry chunk.",
|
|
|
|
"// The chunk loading function for additional chunks",
|
2021-05-11 15:31:46 +08:00
|
|
|
`${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.basicFunction(
|
2023-06-13 05:17:53 +08:00
|
|
|
`chunkId${withFetchPriority ? ", fetchPriority" : ""}`,
|
2021-05-11 15:31:46 +08:00
|
|
|
[
|
|
|
|
`return Promise.all(Object.keys(${handlers}).reduce(${runtimeTemplate.basicFunction(
|
|
|
|
"promises, key",
|
2023-06-13 05:17:53 +08:00
|
|
|
[
|
|
|
|
`${handlers}[key](chunkId, promises${
|
2023-06-13 05:32:42 +08:00
|
|
|
withFetchPriority ? ", fetchPriority" : ""
|
2023-06-13 05:17:53 +08:00
|
|
|
});`,
|
|
|
|
"return promises;"
|
|
|
|
]
|
2021-05-11 15:31:46 +08:00
|
|
|
)}, []));`
|
|
|
|
]
|
|
|
|
)};`
|
2018-11-23 16:37:33 +08:00
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// There ensureChunk is used somewhere in the tree, so we need an empty requireEnsure
|
|
|
|
// function. This can happen with multiple entrypoints.
|
|
|
|
return Template.asString([
|
|
|
|
"// The chunk loading function for additional chunks",
|
|
|
|
"// Since all referenced chunks are already included",
|
|
|
|
"// in this file, this function is empty here.",
|
2019-08-27 02:21:07 +08:00
|
|
|
`${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.returningFunction(
|
|
|
|
"Promise.resolve()"
|
|
|
|
)};`
|
2018-11-23 16:37:33 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EnsureChunkRuntimeModule;
|