DependenciesBlock.js 3.5 KB

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