Entrypoint.js 3.7 KB

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