2016-07-13 17:03:14 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2018-04-27 10:50:46 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-20 00:06:59 +08:00
|
|
|
const ChunkGroup = require("./ChunkGroup");
|
2018-04-30 15:21:42 +08:00
|
|
|
|
2020-09-08 00:02:14 +08:00
|
|
|
/** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
|
2018-07-09 20:48:28 +08:00
|
|
|
/** @typedef {import("./Chunk")} Chunk */
|
2018-04-30 15:22:07 +08:00
|
|
|
|
2020-09-08 00:02:14 +08:00
|
|
|
/** @typedef {{ name?: string } & Omit<EntryDescription, "import">} EntryOptions */
|
|
|
|
|
2018-04-25 05:27:25 +08:00
|
|
|
/**
|
2018-04-30 15:21:42 +08:00
|
|
|
* Entrypoint serves as an encapsulation primitive for chunks that are
|
2018-04-27 10:50:46 +08:00
|
|
|
* a part of a single ChunkGroup. They represent all bundles that need to be loaded for a
|
|
|
|
* single instance of a page. Multi-page application architectures will typically yield multiple Entrypoint objects
|
|
|
|
* inside of the compilation, whereas a Single Page App may only contain one with many lazy-loaded chunks.
|
2018-04-25 05:27:25 +08:00
|
|
|
*/
|
2018-01-20 00:06:59 +08:00
|
|
|
class Entrypoint extends ChunkGroup {
|
2018-04-25 05:27:25 +08:00
|
|
|
/**
|
|
|
|
* Creates an instance of Entrypoint.
|
2020-09-08 00:02:14 +08:00
|
|
|
* @param {EntryOptions | string} entryOptions the options for the entrypoint (or name)
|
|
|
|
* @param {boolean=} initial false, when the entrypoint is not initial loaded
|
2018-04-25 05:27:25 +08:00
|
|
|
*/
|
2020-09-08 00:02:14 +08:00
|
|
|
constructor(entryOptions, initial = true) {
|
|
|
|
if (typeof entryOptions === "string") {
|
|
|
|
entryOptions = { name: entryOptions };
|
|
|
|
}
|
|
|
|
super({
|
|
|
|
name: entryOptions.name
|
|
|
|
});
|
|
|
|
this.options = entryOptions;
|
2018-04-27 10:50:46 +08:00
|
|
|
/** @type {Chunk=} */
|
2020-07-21 16:22:10 +08:00
|
|
|
this._runtimeChunk = undefined;
|
|
|
|
/** @type {Chunk=} */
|
|
|
|
this._entrypointChunk = undefined;
|
2020-09-08 00:02:14 +08:00
|
|
|
/** @type {boolean} */
|
|
|
|
this._initial = initial;
|
2016-12-29 14:58:26 +08:00
|
|
|
}
|
|
|
|
|
2018-04-25 05:27:25 +08:00
|
|
|
/**
|
2020-09-08 00:02:14 +08:00
|
|
|
* @returns {boolean} true, when this chunk group will be loaded on initial page load
|
2018-04-25 05:27:25 +08:00
|
|
|
*/
|
2018-01-20 00:06:59 +08:00
|
|
|
isInitial() {
|
2020-09-08 00:02:14 +08:00
|
|
|
return this._initial;
|
2018-01-09 16:40:30 +08:00
|
|
|
}
|
|
|
|
|
2018-04-25 05:27:25 +08:00
|
|
|
/**
|
2018-04-30 15:21:42 +08:00
|
|
|
* Sets the runtimeChunk for an entrypoint.
|
2018-04-25 05:27:25 +08:00
|
|
|
* @param {Chunk} chunk the chunk being set as the runtime chunk.
|
2018-05-08 20:31:51 +08:00
|
|
|
* @returns {void}
|
2018-04-25 05:27:25 +08:00
|
|
|
*/
|
2018-01-11 00:08:56 +08:00
|
|
|
setRuntimeChunk(chunk) {
|
2020-07-21 16:22:10 +08:00
|
|
|
this._runtimeChunk = chunk;
|
2018-01-11 00:08:56 +08:00
|
|
|
}
|
|
|
|
|
2018-04-25 05:27:25 +08:00
|
|
|
/**
|
2018-04-30 15:21:42 +08:00
|
|
|
* Fetches the chunk reference containing the webpack bootstrap code
|
2020-02-07 17:05:51 +08:00
|
|
|
* @returns {Chunk | null} returns the runtime chunk or null if there is none
|
2018-04-25 05:27:25 +08:00
|
|
|
*/
|
2017-04-23 00:59:15 +08:00
|
|
|
getRuntimeChunk() {
|
2020-07-21 16:22:10 +08:00
|
|
|
if (this._runtimeChunk) return this._runtimeChunk;
|
2020-02-07 17:05:51 +08:00
|
|
|
for (const parent of this.parentsIterable) {
|
|
|
|
if (parent instanceof Entrypoint) return parent.getRuntimeChunk();
|
|
|
|
}
|
|
|
|
return null;
|
2017-04-23 00:59:15 +08:00
|
|
|
}
|
2018-10-17 23:14:50 +08:00
|
|
|
|
2020-07-21 16:22:10 +08:00
|
|
|
/**
|
|
|
|
* Sets the chunk with the entrypoint modules for an entrypoint.
|
|
|
|
* @param {Chunk} chunk the chunk being set as the entrypoint chunk.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
setEntrypointChunk(chunk) {
|
|
|
|
this._entrypointChunk = chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the chunk which contains the entrypoint modules
|
|
|
|
* (or at least the execution of them)
|
|
|
|
* @returns {Chunk} chunk
|
|
|
|
*/
|
|
|
|
getEntrypointChunk() {
|
2023-06-17 03:44:20 +08:00
|
|
|
return /** @type {Chunk} */ (this._entrypointChunk);
|
2020-07-21 16:22:10 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 23:14:50 +08:00
|
|
|
/**
|
|
|
|
* @param {Chunk} oldChunk chunk to be replaced
|
2019-08-10 16:27:56 +08:00
|
|
|
* @param {Chunk} newChunk New chunk that will be replaced with
|
2024-02-21 22:55:02 +08:00
|
|
|
* @returns {boolean | undefined} returns true if the replacement was successful
|
2018-10-17 23:14:50 +08:00
|
|
|
*/
|
|
|
|
replaceChunk(oldChunk, newChunk) {
|
2020-07-21 16:22:10 +08:00
|
|
|
if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk;
|
|
|
|
if (this._entrypointChunk === oldChunk) this._entrypointChunk = newChunk;
|
2018-10-17 23:14:50 +08:00
|
|
|
return super.replaceChunk(oldChunk, newChunk);
|
|
|
|
}
|
2016-11-29 08:38:17 +08:00
|
|
|
}
|
2016-12-29 14:58:26 +08:00
|
|
|
|
|
|
|
module.exports = Entrypoint;
|