DependenciesBlock.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * @param {AsyncDependenciesBlock} block block being added
  43. * @returns {void}
  44. */
  45. addBlock(block) {
  46. this.blocks.push(block);
  47. block.parent = this;
  48. }
  49. /**
  50. * @param {Dependency} dependency dependency being tied to block.
  51. * This is an "edge" pointing to another "node" on module graph.
  52. * @returns {void}
  53. */
  54. addDependency(dependency) {
  55. this.dependencies.push(dependency);
  56. }
  57. /**
  58. * @param {Dependency} dependency dependency being removed
  59. * @returns {void}
  60. */
  61. removeDependency(dependency) {
  62. const idx = this.dependencies.indexOf(dependency);
  63. if (idx >= 0) {
  64. this.dependencies.splice(idx, 1);
  65. }
  66. }
  67. /**
  68. * Removes all dependencies and blocks
  69. * @returns {void}
  70. */
  71. clearDependenciesAndBlocks() {
  72. this.dependencies.length = 0;
  73. this.blocks.length = 0;
  74. }
  75. /**
  76. * @param {Hash} hash the hash used to track dependencies
  77. * @param {UpdateHashContext} context context
  78. * @returns {void}
  79. */
  80. updateHash(hash, context) {
  81. for (const dep of this.dependencies) {
  82. dep.updateHash(hash, context);
  83. }
  84. for (const block of this.blocks) {
  85. block.updateHash(hash, context);
  86. }
  87. }
  88. /**
  89. * @param {ObjectSerializerContext} context context
  90. */
  91. serialize({ write }) {
  92. write(this.dependencies);
  93. write(this.blocks);
  94. }
  95. /**
  96. * @param {ObjectDeserializerContext} context context
  97. */
  98. deserialize({ read }) {
  99. this.dependencies = read();
  100. this.blocks = read();
  101. for (const block of this.blocks) {
  102. block.parent = this;
  103. }
  104. }
  105. }
  106. makeSerializable(DependenciesBlock, "webpack/lib/DependenciesBlock");
  107. module.exports = DependenciesBlock;