1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
9 | const RuntimeModule = require("../RuntimeModule");
|
10 | const Template = require("../Template");
|
11 |
|
12 | class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
|
13 | constructor(asyncChunkLoading) {
|
14 | super("startup chunk dependencies", RuntimeModule.STAGE_TRIGGER);
|
15 | this.asyncChunkLoading = asyncChunkLoading;
|
16 | }
|
17 |
|
18 | |
19 |
|
20 |
|
21 | generate() {
|
22 | const { chunkGraph, chunk, compilation } = this;
|
23 | const { runtimeTemplate } = compilation;
|
24 | const chunkIds = Array.from(
|
25 | chunkGraph.getChunkEntryDependentChunksIterable(chunk)
|
26 | ).map(chunk => {
|
27 | return chunk.id;
|
28 | });
|
29 | return Template.asString([
|
30 | `var next = ${RuntimeGlobals.startup};`,
|
31 | `${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction(
|
32 | "",
|
33 | !this.asyncChunkLoading
|
34 | ? chunkIds
|
35 | .map(
|
36 | id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});`
|
37 | )
|
38 | .concat("return next();")
|
39 | : chunkIds.length === 1
|
40 | ? `return ${RuntimeGlobals.ensureChunk}(${JSON.stringify(
|
41 | chunkIds[0]
|
42 | )}).then(next);`
|
43 | : chunkIds.length > 2
|
44 | ? [
|
45 | // using map is shorter for 3 or more chunks
|
46 | `return Promise.all(${JSON.stringify(chunkIds)}.map(${
|
47 | RuntimeGlobals.ensureChunk
|
48 | }, __webpack_require__)).then(next);`
|
49 | ]
|
50 | : [
|
51 | // calling ensureChunk directly is shorter for 0 - 2 chunks
|
52 | "return Promise.all([",
|
53 | Template.indent(
|
54 | chunkIds
|
55 | .map(
|
56 | id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)})`
|
57 | )
|
58 | .join(",\n")
|
59 | ),
|
60 | "]).then(next);"
|
61 | ]
|
62 | )};`
|
63 | ]);
|
64 | }
|
65 | }
|
66 |
|
67 | module.exports = StartupChunkDependenciesRuntimeModule;
|