DependenciesBlock.js 3.5 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 makeSerializable = require("./util/makeSerializable");
  7. /** @import AsyncDependenciesBlock from "./AsyncDependenciesBlock" */
  8. /** @import Dependency, { UpdateHashContext } from "./Dependency" */
  9. /**
  10. * @import {
  11. * ObjectDeserializerContext,
  12. * ObjectSerializerContext
  13. * } from "./serialization/ObjectMiddleware"
  14. */
  15. /** @import Hash from "./util/Hash" */
  16. /** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
  17. /**
  18. * DependenciesBlock is the base class for all Module classes in webpack. It describes a
  19. * "block" of dependencies which are pointers to other DependenciesBlock instances. For example
  20. * when a Module has a CommonJs require statement, the DependencyBlock for the CommonJs module
  21. * would be added as a dependency to the Module. DependenciesBlock is inherited by two types of classes:
  22. * Module subclasses and AsyncDependenciesBlock subclasses. The only difference between the two is that
  23. * AsyncDependenciesBlock subclasses are used for code-splitting (async boundary) and Module subclasses are not.
  24. */
  25. class DependenciesBlock {
  26. constructor() {
  27. /** @type {Dependency[]} */
  28. this.dependencies = [];
  29. /** @type {AsyncDependenciesBlock[]} */
  30. this.blocks = [];
  31. /** @type {DependenciesBlock | undefined} */
  32. this.parent = undefined;
  33. }
  34. getRootBlock() {
  35. /** @type {DependenciesBlock} */
  36. let current = this;
  37. while (current.parent) current = current.parent;
  38. return current;
  39. }
  40. /**
  41. * Adds a DependencyBlock to DependencyBlock relationship.
  42. * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
  43. * @param {AsyncDependenciesBlock} block block being added
  44. * @returns {void}
  45. */
  46. addBlock(block) {
  47. this.blocks.push(block);
  48. block.parent = this;
  49. }
  50. /**
  51. * Adds the provided dependency to the dependencies block.
  52. * @param {Dependency} dependency dependency being tied to block.
  53. * This is an "edge" pointing to another "node" on module graph.
  54. * @returns {void}
  55. */
  56. addDependency(dependency) {
  57. this.dependencies.push(dependency);
  58. }
  59. /**
  60. * Removes dependency.
  61. * @param {Dependency} dependency dependency being removed
  62. * @returns {void}
  63. */
  64. removeDependency(dependency) {
  65. const idx = this.dependencies.indexOf(dependency);
  66. if (idx >= 0) {
  67. this.dependencies.splice(idx, 1);
  68. }
  69. }
  70. /**
  71. * Clear dependencies and blocks.
  72. * @returns {void}
  73. */
  74. clearDependenciesAndBlocks() {
  75. this.dependencies.length = 0;
  76. this.blocks.length = 0;
  77. }
  78. /**
  79. * Updates the hash with the data contributed by this instance.
  80. * @param {Hash} hash the hash used to track dependencies
  81. * @param {UpdateHashContext} context context
  82. * @returns {void}
  83. */
  84. updateHash(hash, context) {
  85. for (const dep of this.dependencies) {
  86. dep.updateHash(hash, context);
  87. }
  88. for (const block of this.blocks) {
  89. block.updateHash(hash, context);
  90. }
  91. }
  92. /**
  93. * Serializes this instance into the provided serializer context.
  94. * @param {ObjectSerializerContext} context context
  95. */
  96. serialize({ write }) {
  97. write(this.dependencies);
  98. write(this.blocks);
  99. }
  100. /**
  101. * Restores this instance from the provided deserializer context.
  102. * @param {ObjectDeserializerContext} context context
  103. */
  104. deserialize({ read }) {
  105. this.dependencies = read();
  106. this.blocks = read();
  107. for (const block of this.blocks) {
  108. block.parent = this;
  109. }
  110. }
  111. }
  112. makeSerializable(DependenciesBlock, "webpack/lib/DependenciesBlock");
  113. module.exports = DependenciesBlock;