UNPKG

1.51 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3*/
4
5"use strict";
6
7const RuntimeGlobals = require("../RuntimeGlobals");
8const RuntimeModule = require("../RuntimeModule");
9const Template = require("../Template");
10
11/** @typedef {import("../Chunk")} Chunk */
12/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
13
14class 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.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
51module.exports = ChunkPrefetchStartupRuntimeModule;