1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
9 | const RuntimeModule = require("../RuntimeModule");
|
---|
10 | const Template = require("../Template");
|
---|
11 |
|
---|
12 | /** @typedef {import("../Chunk")} Chunk */
|
---|
13 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
14 | /** @typedef {import("../Compilation")} Compilation */
|
---|
15 |
|
---|
16 | class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
|
---|
17 | /**
|
---|
18 | * @param {boolean} asyncChunkLoading use async chunk loading
|
---|
19 | */
|
---|
20 | constructor(asyncChunkLoading) {
|
---|
21 | super("startup chunk dependencies", RuntimeModule.STAGE_TRIGGER);
|
---|
22 | this.asyncChunkLoading = asyncChunkLoading;
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @returns {string | null} runtime code
|
---|
27 | */
|
---|
28 | generate() {
|
---|
29 | const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
|
---|
30 | const chunk = /** @type {Chunk} */ (this.chunk);
|
---|
31 | const chunkIds = Array.from(
|
---|
32 | chunkGraph.getChunkEntryDependentChunksIterable(chunk)
|
---|
33 | ).map(chunk => chunk.id);
|
---|
34 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
35 | const { runtimeTemplate } = compilation;
|
---|
36 | return Template.asString([
|
---|
37 | `var next = ${RuntimeGlobals.startup};`,
|
---|
38 | `${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction(
|
---|
39 | "",
|
---|
40 | !this.asyncChunkLoading
|
---|
41 | ? chunkIds
|
---|
42 | .map(
|
---|
43 | id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});`
|
---|
44 | )
|
---|
45 | .concat("return next();")
|
---|
46 | : chunkIds.length === 1
|
---|
47 | ? `return ${RuntimeGlobals.ensureChunk}(${JSON.stringify(
|
---|
48 | chunkIds[0]
|
---|
49 | )}).then(next);`
|
---|
50 | : chunkIds.length > 2
|
---|
51 | ? [
|
---|
52 | // using map is shorter for 3 or more chunks
|
---|
53 | `return Promise.all(${JSON.stringify(chunkIds)}.map(${
|
---|
54 | RuntimeGlobals.ensureChunk
|
---|
55 | }, ${RuntimeGlobals.require})).then(next);`
|
---|
56 | ]
|
---|
57 | : [
|
---|
58 | // calling ensureChunk directly is shorter for 0 - 2 chunks
|
---|
59 | "return Promise.all([",
|
---|
60 | Template.indent(
|
---|
61 | chunkIds
|
---|
62 | .map(
|
---|
63 | id =>
|
---|
64 | `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)})`
|
---|
65 | )
|
---|
66 | .join(",\n")
|
---|
67 | ),
|
---|
68 | "]).then(next);"
|
---|
69 | ]
|
---|
70 | )};`
|
---|
71 | ]);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | module.exports = StartupChunkDependenciesRuntimeModule;
|
---|