| 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("../Compilation")} Compilation */
|
|---|
| 13 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
|---|
| 14 |
|
|---|
| 15 | class ChunkPrefetchStartupRuntimeModule extends RuntimeModule {
|
|---|
| 16 | /**
|
|---|
| 17 | * @param {{ onChunks: Chunk[], chunks: Set<Chunk> }[]} startupChunks chunk ids to trigger when chunks are loaded
|
|---|
| 18 | */
|
|---|
| 19 | constructor(startupChunks) {
|
|---|
| 20 | super("startup prefetch", RuntimeModule.STAGE_TRIGGER);
|
|---|
| 21 | this.startupChunks = startupChunks;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * @returns {string | null} runtime code
|
|---|
| 26 | */
|
|---|
| 27 | generate() {
|
|---|
| 28 | const { startupChunks } = this;
|
|---|
| 29 | const compilation = /** @type {Compilation} */ (this.compilation);
|
|---|
| 30 | const chunk = /** @type {Chunk} */ (this.chunk);
|
|---|
| 31 | const { runtimeTemplate } = compilation;
|
|---|
| 32 | return Template.asString(
|
|---|
| 33 | startupChunks.map(
|
|---|
| 34 | ({ onChunks, chunks }) =>
|
|---|
| 35 | `${RuntimeGlobals.onChunksLoaded}(0, ${JSON.stringify(
|
|---|
| 36 | // This need to include itself to delay execution after this chunk has been fully loaded
|
|---|
| 37 | onChunks.filter(c => c === chunk).map(c => c.id)
|
|---|
| 38 | )}, ${runtimeTemplate.basicFunction(
|
|---|
| 39 | "",
|
|---|
| 40 | chunks.size < 3
|
|---|
| 41 | ? Array.from(
|
|---|
| 42 | chunks,
|
|---|
| 43 | c =>
|
|---|
| 44 | `${RuntimeGlobals.prefetchChunk}(${JSON.stringify(c.id)});`
|
|---|
| 45 | )
|
|---|
| 46 | : `${JSON.stringify(Array.from(chunks, c => c.id))}.map(${
|
|---|
| 47 | RuntimeGlobals.prefetchChunk
|
|---|
| 48 | });`
|
|---|
| 49 | )}, 5);`
|
|---|
| 50 | )
|
|---|
| 51 | );
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | module.exports = ChunkPrefetchStartupRuntimeModule;
|
|---|