1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
8 | const RuntimeModule = require("../RuntimeModule");
|
9 | const Template = require("../Template");
|
10 |
|
11 | class EnsureChunkRuntimeModule extends RuntimeModule {
|
12 | constructor(runtimeRequirements) {
|
13 | super("ensure chunk");
|
14 | this.runtimeRequirements = runtimeRequirements;
|
15 | }
|
16 |
|
17 | |
18 |
|
19 |
|
20 | generate() {
|
21 | const { runtimeTemplate } = this.compilation;
|
22 |
|
23 | if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) {
|
24 | const handlers = RuntimeGlobals.ensureChunkHandlers;
|
25 | return Template.asString([
|
26 | `${handlers} = {};`,
|
27 | "// This file contains only the entry chunk.",
|
28 | "// The chunk loading function for additional chunks",
|
29 | `${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.basicFunction(
|
30 | "chunkId",
|
31 | [
|
32 | `return Promise.all(Object.keys(${handlers}).reduce(${runtimeTemplate.basicFunction(
|
33 | "promises, key",
|
34 | [`${handlers}[key](chunkId, promises);`, "return promises;"]
|
35 | )}, []));`
|
36 | ]
|
37 | )};`
|
38 | ]);
|
39 | } else {
|
40 |
|
41 |
|
42 | return Template.asString([
|
43 | "// The chunk loading function for additional chunks",
|
44 | "// Since all referenced chunks are already included",
|
45 | "// in this file, this function is empty here.",
|
46 | `${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.returningFunction(
|
47 | "Promise.resolve()"
|
48 | )};`
|
49 | ]);
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
54 | module.exports = EnsureChunkRuntimeModule;
|