UNPKG

3.02 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const ChunkGroup = require("./ChunkGroup");
9
10/** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
11/** @typedef {import("./Chunk")} Chunk */
12
13/** @typedef {{ name?: string } & Omit<EntryDescription, "import">} EntryOptions */
14
15/**
16 * Entrypoint serves as an encapsulation primitive for chunks that are
17 * a part of a single ChunkGroup. They represent all bundles that need to be loaded for a
18 * single instance of a page. Multi-page application architectures will typically yield multiple Entrypoint objects
19 * inside of the compilation, whereas a Single Page App may only contain one with many lazy-loaded chunks.
20 */
21class Entrypoint extends ChunkGroup {
22 /**
23 * Creates an instance of Entrypoint.
24 * @param {EntryOptions | string} entryOptions the options for the entrypoint (or name)
25 * @param {boolean=} initial false, when the entrypoint is not initial loaded
26 */
27 constructor(entryOptions, initial = true) {
28 if (typeof entryOptions === "string") {
29 entryOptions = { name: entryOptions };
30 }
31 super({
32 name: entryOptions.name
33 });
34 this.options = entryOptions;
35 /** @type {Chunk=} */
36 this._runtimeChunk = undefined;
37 /** @type {Chunk=} */
38 this._entrypointChunk = undefined;
39 /** @type {boolean} */
40 this._initial = initial;
41 }
42
43 /**
44 * @returns {boolean} true, when this chunk group will be loaded on initial page load
45 */
46 isInitial() {
47 return this._initial;
48 }
49
50 /**
51 * Sets the runtimeChunk for an entrypoint.
52 * @param {Chunk} chunk the chunk being set as the runtime chunk.
53 * @returns {void}
54 */
55 setRuntimeChunk(chunk) {
56 this._runtimeChunk = chunk;
57 }
58
59 /**
60 * Fetches the chunk reference containing the webpack bootstrap code
61 * @returns {Chunk | null} returns the runtime chunk or null if there is none
62 */
63 getRuntimeChunk() {
64 if (this._runtimeChunk) return this._runtimeChunk;
65 for (const parent of this.parentsIterable) {
66 if (parent instanceof Entrypoint) return parent.getRuntimeChunk();
67 }
68 return null;
69 }
70
71 /**
72 * Sets the chunk with the entrypoint modules for an entrypoint.
73 * @param {Chunk} chunk the chunk being set as the entrypoint chunk.
74 * @returns {void}
75 */
76 setEntrypointChunk(chunk) {
77 this._entrypointChunk = chunk;
78 }
79
80 /**
81 * Returns the chunk which contains the entrypoint modules
82 * (or at least the execution of them)
83 * @returns {Chunk} chunk
84 */
85 getEntrypointChunk() {
86 return this._entrypointChunk;
87 }
88
89 /**
90 * @param {Chunk} oldChunk chunk to be replaced
91 * @param {Chunk} newChunk New chunk that will be replaced with
92 * @returns {boolean} returns true if the replacement was successful
93 */
94 replaceChunk(oldChunk, newChunk) {
95 if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk;
96 if (this._entrypointChunk === oldChunk) this._entrypointChunk = newChunk;
97 return super.replaceChunk(oldChunk, newChunk);
98 }
99}
100
101module.exports = Entrypoint;