| 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 ChunkGroup = require("./ChunkGroup");
|
|---|
| 9 | const SortableSet = require("./util/SortableSet");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
|
|---|
| 12 | /** @typedef {import("./Chunk")} Chunk */
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {{ name?: string } & Omit<EntryDescription, "import">} EntryOptions */
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Entrypoint serves as an encapsulation primitive for chunks that are
|
|---|
| 18 | * a part of a single ChunkGroup. They represent all bundles that need to be loaded for a
|
|---|
| 19 | * single instance of a page. Multi-page application architectures will typically yield multiple Entrypoint objects
|
|---|
| 20 | * inside of the compilation, whereas a Single Page App may only contain one with many lazy-loaded chunks.
|
|---|
| 21 | */
|
|---|
| 22 | class Entrypoint extends ChunkGroup {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of Entrypoint.
|
|---|
| 25 | * @param {EntryOptions | string} entryOptions the options for the entrypoint (or name)
|
|---|
| 26 | * @param {boolean=} initial false, when the entrypoint is not initial loaded
|
|---|
| 27 | */
|
|---|
| 28 | constructor(entryOptions, initial = true) {
|
|---|
| 29 | if (typeof entryOptions === "string") {
|
|---|
| 30 | entryOptions = { name: entryOptions };
|
|---|
| 31 | }
|
|---|
| 32 | super({
|
|---|
| 33 | name: entryOptions.name
|
|---|
| 34 | });
|
|---|
| 35 | this.options = entryOptions;
|
|---|
| 36 | /** @type {Chunk=} */
|
|---|
| 37 | this._runtimeChunk = undefined;
|
|---|
| 38 | /** @type {Chunk=} */
|
|---|
| 39 | this._entrypointChunk = undefined;
|
|---|
| 40 | /** @type {boolean} */
|
|---|
| 41 | this._initial = initial;
|
|---|
| 42 | /** @type {SortableSet<Entrypoint>} */
|
|---|
| 43 | this._dependOn = new SortableSet();
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Indicates whether this chunk group is loaded as part of the initial page
|
|---|
| 48 | * load instead of being created lazily.
|
|---|
| 49 | * @returns {boolean} true, when this chunk group will be loaded on initial page load
|
|---|
| 50 | */
|
|---|
| 51 | isInitial() {
|
|---|
| 52 | return this._initial;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Sets the runtimeChunk for an entrypoint.
|
|---|
| 57 | * @param {Chunk} chunk the chunk being set as the runtime chunk.
|
|---|
| 58 | * @returns {void}
|
|---|
| 59 | */
|
|---|
| 60 | setRuntimeChunk(chunk) {
|
|---|
| 61 | this._runtimeChunk = chunk;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Fetches the chunk reference containing the webpack bootstrap code
|
|---|
| 66 | * @returns {Chunk | null} returns the runtime chunk or null if there is none
|
|---|
| 67 | */
|
|---|
| 68 | getRuntimeChunk() {
|
|---|
| 69 | if (this._runtimeChunk) return this._runtimeChunk;
|
|---|
| 70 | for (const parent of this.parentsIterable) {
|
|---|
| 71 | if (parent instanceof Entrypoint) return parent.getRuntimeChunk();
|
|---|
| 72 | }
|
|---|
| 73 | return null;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Sets the chunk with the entrypoint modules for an entrypoint.
|
|---|
| 78 | * @param {Chunk} chunk the chunk being set as the entrypoint chunk.
|
|---|
| 79 | * @returns {void}
|
|---|
| 80 | */
|
|---|
| 81 | setEntrypointChunk(chunk) {
|
|---|
| 82 | this._entrypointChunk = chunk;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Returns the chunk which contains the entrypoint modules
|
|---|
| 87 | * (or at least the execution of them)
|
|---|
| 88 | * @returns {Chunk} chunk
|
|---|
| 89 | */
|
|---|
| 90 | getEntrypointChunk() {
|
|---|
| 91 | return /** @type {Chunk} */ (this._entrypointChunk);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Replaces one member chunk with another while preserving the group's
|
|---|
| 96 | * ordering and avoiding duplicates.
|
|---|
| 97 | * @param {Chunk} oldChunk chunk to be replaced
|
|---|
| 98 | * @param {Chunk} newChunk New chunk that will be replaced with
|
|---|
| 99 | * @returns {boolean | undefined} returns true if the replacement was successful
|
|---|
| 100 | */
|
|---|
| 101 | replaceChunk(oldChunk, newChunk) {
|
|---|
| 102 | if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk;
|
|---|
| 103 | if (this._entrypointChunk === oldChunk) this._entrypointChunk = newChunk;
|
|---|
| 104 | return super.replaceChunk(oldChunk, newChunk);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * @param {Entrypoint} entrypoint the entrypoint
|
|---|
| 109 | * @returns {void}
|
|---|
| 110 | */
|
|---|
| 111 | addDependOn(entrypoint) {
|
|---|
| 112 | this._dependOn.add(entrypoint);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * @param {Entrypoint} entrypoint the entrypoint
|
|---|
| 117 | * @returns {boolean} true if the entrypoint is in the dependOn set
|
|---|
| 118 | */
|
|---|
| 119 | dependOn(entrypoint) {
|
|---|
| 120 | return this._dependOn.has(entrypoint);
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | module.exports = Entrypoint;
|
|---|