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 | class EnsureChunkRuntimeModule extends RuntimeModule {
|
---|
12 | constructor(runtimeRequirements) {
|
---|
13 | super("ensure chunk");
|
---|
14 | this.runtimeRequirements = runtimeRequirements;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @returns {string} runtime code
|
---|
19 | */
|
---|
20 | generate() {
|
---|
21 | const { runtimeTemplate } = this.compilation;
|
---|
22 | // Check if there are non initial chunks which need to be imported using require-ensure
|
---|
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 | // There ensureChunk is used somewhere in the tree, so we need an empty requireEnsure
|
---|
41 | // function. This can happen with multiple entrypoints.
|
---|
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;
|
---|