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 | /** @typedef {import("../Chunk")} Chunk */
|
---|
12 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
---|
13 |
|
---|
14 | class ChunkPrefetchStartupRuntimeModule extends RuntimeModule {
|
---|
15 | /**
|
---|
16 | * @param {{ onChunks: Chunk[], chunks: Set<Chunk> }[]} startupChunks chunk ids to trigger when chunks are loaded
|
---|
17 | */
|
---|
18 | constructor(startupChunks) {
|
---|
19 | super("startup prefetch", RuntimeModule.STAGE_TRIGGER);
|
---|
20 | this.startupChunks = startupChunks;
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @returns {string} runtime code
|
---|
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.basicFunction(
|
---|
36 | "",
|
---|
37 | chunks.size < 3
|
---|
38 | ? Array.from(
|
---|
39 | chunks,
|
---|
40 | c =>
|
---|
41 | `${RuntimeGlobals.prefetchChunk}(${JSON.stringify(c.id)});`
|
---|
42 | )
|
---|
43 | : `${JSON.stringify(Array.from(chunks, c => c.id))}.map(${
|
---|
44 | RuntimeGlobals.prefetchChunk
|
---|
45 | });`
|
---|
46 | )}, 5);`
|
---|
47 | )
|
---|
48 | );
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | module.exports = ChunkPrefetchStartupRuntimeModule;
|
---|