DependenciesBlock.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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("./util/Hash")} Hash */
  13. /** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
  14. class DependenciesBlock {
  15. constructor() {
  16. /** @type {Dependency[]} */
  17. this.dependencies = [];
  18. /** @type {AsyncDependenciesBlock[]} */
  19. this.blocks = [];
  20. /** @type {DependenciesBlock} */
  21. this.parent = undefined;
  22. }
  23. getRootBlock() {
  24. /** @type {DependenciesBlock} */
  25. let current = this;
  26. while (current.parent) current = current.parent;
  27. return current;
  28. }
  29. /**
  30. * Adds a DependencyBlock to DependencyBlock relationship.
  31. * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
  32. *
  33. * @param {AsyncDependenciesBlock} block block being added
  34. * @returns {void}
  35. */
  36. addBlock(block) {
  37. this.blocks.push(block);
  38. block.parent = this;
  39. }
  40. /**
  41. * @param {Dependency} dependency dependency being tied to block.
  42. * This is an "edge" pointing to another "node" on module graph.
  43. * @returns {void}
  44. */
  45. addDependency(dependency) {
  46. this.dependencies.push(dependency);
  47. }
  48. /**
  49. * @param {Dependency} dependency dependency being removed
  50. * @returns {void}
  51. */
  52. removeDependency(dependency) {
  53. const idx = this.dependencies.indexOf(dependency);
  54. if (idx >= 0) {
  55. this.dependencies.splice(idx, 1);
  56. }
  57. }
  58. /**
  59. * Removes all dependencies and blocks
  60. * @returns {void}
  61. */
  62. clearDependenciesAndBlocks() {
  63. this.dependencies.length = 0;
  64. this.blocks.length = 0;
  65. }
  66. /**
  67. * @param {Hash} hash the hash used to track dependencies
  68. * @param {UpdateHashContext} context context
  69. * @returns {void}
  70. */
  71. updateHash(hash, context) {
  72. for (const dep of this.dependencies) {
  73. dep.updateHash(hash, context);
  74. }
  75. for (const block of this.blocks) {
  76. block.updateHash(hash, context);
  77. }
  78. }
  79. serialize({ write }) {
  80. write(this.dependencies);
  81. write(this.blocks);
  82. }
  83. deserialize({ read }) {
  84. this.dependencies = read();
  85. this.blocks = read();
  86. for (const block of this.blocks) {
  87. block.parent = this;
  88. }
  89. }
  90. }
  91. makeSerializable(DependenciesBlock, "webpack/lib/DependenciesBlock");
  92. module.exports = DependenciesBlock;