Entrypoint.js 3.0 KB

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