Entrypoint.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * @returns {boolean} true, when this chunk group will be loaded on initial page load
  42. */
  43. isInitial() {
  44. return this._initial;
  45. }
  46. /**
  47. * Sets the runtimeChunk for an entrypoint.
  48. * @param {Chunk} chunk the chunk being set as the runtime chunk.
  49. * @returns {void}
  50. */
  51. setRuntimeChunk(chunk) {
  52. this._runtimeChunk = chunk;
  53. }
  54. /**
  55. * Fetches the chunk reference containing the webpack bootstrap code
  56. * @returns {Chunk | null} returns the runtime chunk or null if there is none
  57. */
  58. getRuntimeChunk() {
  59. if (this._runtimeChunk) return this._runtimeChunk;
  60. for (const parent of this.parentsIterable) {
  61. if (parent instanceof Entrypoint) return parent.getRuntimeChunk();
  62. }
  63. return null;
  64. }
  65. /**
  66. * Sets the chunk with the entrypoint modules for an entrypoint.
  67. * @param {Chunk} chunk the chunk being set as the entrypoint chunk.
  68. * @returns {void}
  69. */
  70. setEntrypointChunk(chunk) {
  71. this._entrypointChunk = chunk;
  72. }
  73. /**
  74. * Returns the chunk which contains the entrypoint modules
  75. * (or at least the execution of them)
  76. * @returns {Chunk} chunk
  77. */
  78. getEntrypointChunk() {
  79. return /** @type {Chunk} */ (this._entrypointChunk);
  80. }
  81. /**
  82. * @param {Chunk} oldChunk chunk to be replaced
  83. * @param {Chunk} newChunk New chunk that will be replaced with
  84. * @returns {boolean | undefined} returns true if the replacement was successful
  85. */
  86. replaceChunk(oldChunk, newChunk) {
  87. if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk;
  88. if (this._entrypointChunk === oldChunk) this._entrypointChunk = newChunk;
  89. return super.replaceChunk(oldChunk, newChunk);
  90. }
  91. /**
  92. * @param {Entrypoint} entrypoint the entrypoint
  93. * @returns {void}
  94. */
  95. addDependOn(entrypoint) {
  96. this._dependOn.add(entrypoint);
  97. }
  98. /**
  99. * @param {Entrypoint} entrypoint the entrypoint
  100. * @returns {boolean} true if the entrypoint is in the dependOn set
  101. */
  102. dependOn(entrypoint) {
  103. return this._dependOn.has(entrypoint);
  104. }
  105. }
  106. module.exports = Entrypoint;