| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 9 | const RuntimeModule = require("../RuntimeModule");
|
|---|
| 10 | const Template = require("../Template");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 13 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
|---|
| 14 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 15 |
|
|---|
| 16 | class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
|
|---|
| 17 | /**
|
|---|
| 18 | * @param {boolean} asyncChunkLoading use async chunk loading
|
|---|
| 19 | */
|
|---|
| 20 | constructor(asyncChunkLoading) {
|
|---|
| 21 | super("startup chunk dependencies", RuntimeModule.STAGE_TRIGGER);
|
|---|
| 22 | /** @type {boolean} */
|
|---|
| 23 | this.asyncChunkLoading = asyncChunkLoading;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Generates runtime code for this runtime module.
|
|---|
| 28 | * @returns {string | null} runtime code
|
|---|
| 29 | */
|
|---|
| 30 | generate() {
|
|---|
| 31 | const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
|
|---|
| 32 | const chunk = /** @type {Chunk} */ (this.chunk);
|
|---|
| 33 | const chunkIds = [
|
|---|
| 34 | ...chunkGraph.getChunkEntryDependentChunksIterable(chunk)
|
|---|
| 35 | ].map((chunk) => chunk.id);
|
|---|
| 36 | const compilation = /** @type {Compilation} */ (this.compilation);
|
|---|
| 37 | const { runtimeTemplate } = compilation;
|
|---|
| 38 | return Template.asString([
|
|---|
| 39 | `var next = ${RuntimeGlobals.startup};`,
|
|---|
| 40 | `${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction(
|
|---|
| 41 | "",
|
|---|
| 42 | !this.asyncChunkLoading
|
|---|
| 43 | ? [
|
|---|
| 44 | ...chunkIds.map(
|
|---|
| 45 | (id) => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});`
|
|---|
| 46 | ),
|
|---|
| 47 | "return next();"
|
|---|
| 48 | ]
|
|---|
| 49 | : chunkIds.length === 1
|
|---|
| 50 | ? `return ${RuntimeGlobals.ensureChunk}(${JSON.stringify(
|
|---|
| 51 | chunkIds[0]
|
|---|
| 52 | )}).then(next);`
|
|---|
| 53 | : chunkIds.length > 2
|
|---|
| 54 | ? [
|
|---|
| 55 | // using map is shorter for 3 or more chunks
|
|---|
| 56 | `return Promise.all(${JSON.stringify(chunkIds)}.map(${
|
|---|
| 57 | RuntimeGlobals.ensureChunk
|
|---|
| 58 | }, ${RuntimeGlobals.require})).then(next);`
|
|---|
| 59 | ]
|
|---|
| 60 | : [
|
|---|
| 61 | // calling ensureChunk directly is shorter for 0 - 2 chunks
|
|---|
| 62 | "return Promise.all([",
|
|---|
| 63 | Template.indent(
|
|---|
| 64 | chunkIds
|
|---|
| 65 | .map(
|
|---|
| 66 | (id) =>
|
|---|
| 67 | `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)})`
|
|---|
| 68 | )
|
|---|
| 69 | .join(",\n")
|
|---|
| 70 | ),
|
|---|
| 71 | "]).then(next);"
|
|---|
| 72 | ]
|
|---|
| 73 | )};`
|
|---|
| 74 | ]);
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | module.exports = StartupChunkDependenciesRuntimeModule;
|
|---|