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 |
|
12 |
|
13 |
|
14 | class ChunkPrefetchStartupRuntimeModule extends RuntimeModule {
|
15 | |
16 |
|
17 |
|
18 | constructor(startupChunks) {
|
19 | super("startup prefetch", RuntimeModule.STAGE_TRIGGER);
|
20 | this.startupChunks = startupChunks;
|
21 | }
|
22 |
|
23 | |
24 |
|
25 |
|
26 | generate() {
|
27 | const { startupChunks, chunk } = this;
|
28 | const { runtimeTemplate } = this.compilation;
|
29 | return Template.asString(
|
30 | startupChunks.map(
|
31 | ({ onChunks, chunks }) =>
|
32 | `${RuntimeGlobals.onChunksLoaded}(0, ${JSON.stringify(
|
33 | // This need to include itself to delay execution after this chunk has been fully loaded
|
34 | onChunks.filter(c => c === chunk).map(c => c.id)
|
35 | )}, ${runtimeTemplate.expressionFunction(
|
36 | chunks.size < 3
|
37 | ? Array.from(
|
38 | chunks,
|
39 | c =>
|
40 | `${RuntimeGlobals.prefetchChunk}(${JSON.stringify(c.id)})`
|
41 | ).join(", ")
|
42 | : `${JSON.stringify(Array.from(chunks, c => c.id))}.map(${
|
43 | RuntimeGlobals.prefetchChunk
|
44 | })`
|
45 | )}, 5);`
|
46 | )
|
47 | );
|
48 | }
|
49 | }
|
50 |
|
51 | module.exports = ChunkPrefetchStartupRuntimeModule;
|