Entrypoint.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ChunkGroup = require("./ChunkGroup");
  7. const SortableSet = require("./util/SortableSet");
  8. /**
  9. * @import {
  10. * EntryDescriptionNormalized as EntryDescription
  11. * } from "../declarations/WebpackOptions"
  12. */
  13. /** @import Chunk from "./Chunk" */
  14. /** @typedef {{ name?: string, worklet?: boolean } & Omit<EntryDescription, "import">} EntryOptions */
  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. */
  21. class 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. /** @type {SortableSet<Entrypoint>} */
  42. this._dependOn = new SortableSet();
  43. }
  44. /**
  45. * Indicates whether this chunk group is loaded as part of the initial page
  46. * load instead of being created lazily.
  47. * @returns {boolean} true, when this chunk group will be loaded on initial page load
  48. */
  49. isInitial() {
  50. return this._initial;
  51. }
  52. /**
  53. * Sets the runtimeChunk for an entrypoint.
  54. * @param {Chunk} chunk the chunk being set as the runtime chunk.
  55. * @returns {void}
  56. */
  57. setRuntimeChunk(chunk) {
  58. this._runtimeChunk = chunk;
  59. }
  60. /**
  61. * Fetches the chunk reference containing the webpack bootstrap code
  62. * @returns {Chunk | null} returns the runtime chunk or null if there is none
  63. */
  64. getRuntimeChunk() {
  65. if (this._runtimeChunk) return this._runtimeChunk;
  66. for (const parent of this.parentsIterable) {
  67. if (parent instanceof Entrypoint) return parent.getRuntimeChunk();
  68. }
  69. return null;
  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. * Returns the chunk which contains the entrypoint modules
  81. * (or at least the execution of them)
  82. * @returns {Chunk} chunk
  83. */
  84. getEntrypointChunk() {
  85. return /** @type {Chunk} */ (this._entrypointChunk);
  86. }
  87. /**
  88. * Replaces one member chunk with another while preserving the group's
  89. * ordering and avoiding duplicates.
  90. * @param {Chunk} oldChunk chunk to be replaced
  91. * @param {Chunk} newChunk New chunk that will be replaced with
  92. * @returns {boolean | undefined} 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. * @param {Entrypoint} entrypoint the entrypoint
  101. * @returns {void}
  102. */
  103. addDependOn(entrypoint) {
  104. this._dependOn.add(entrypoint);
  105. }
  106. /**
  107. * @param {Entrypoint} entrypoint the entrypoint
  108. * @returns {boolean} true if the entrypoint is in the dependOn set
  109. */
  110. dependOn(entrypoint) {
  111. return this._dependOn.has(entrypoint);
  112. }
  113. }
  114. module.exports = Entrypoint;