DependenciesBlock.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependenciesBlockVariable = require("./DependenciesBlockVariable");
  7. /** @typedef {import("./ChunkGroup")} ChunkGroup */
  8. /** @typedef {import("./Dependency")} Dependency */
  9. /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
  10. /** @typedef {import("./DependenciesBlockVariable")} DependenciesBlockVariable */
  11. /** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
  12. /** @typedef {import("./util/createHash").Hash} Hash */
  13. class DependenciesBlock {
  14. constructor() {
  15. /** @type {Dependency[]} */
  16. this.dependencies = [];
  17. /** @type {AsyncDependenciesBlock[]} */
  18. this.blocks = [];
  19. /** @type {DependenciesBlockVariable[]} */
  20. this.variables = [];
  21. }
  22. /**
  23. * Adds a DependencyBlock to DependencyBlock relationship.
  24. * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
  25. *
  26. * @param {AsyncDependenciesBlock} block block being added
  27. * @returns {void}
  28. */
  29. addBlock(block) {
  30. this.blocks.push(block);
  31. block.parent = this;
  32. }
  33. /**
  34. * @param {string} name name of dependency
  35. * @param {string} expression expression string for variable
  36. * @param {Dependency[]} dependencies dependency instances tied to variable
  37. * @returns {void}
  38. */
  39. addVariable(name, expression, dependencies) {
  40. for (let v of this.variables) {
  41. if (v.name === name && v.expression === expression) {
  42. return;
  43. }
  44. }
  45. this.variables.push(
  46. new DependenciesBlockVariable(name, expression, dependencies)
  47. );
  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. * @param {Hash} hash the hash used to track dependencies
  69. * @returns {void}
  70. */
  71. updateHash(hash) {
  72. for (const dep of this.dependencies) dep.updateHash(hash);
  73. for (const block of this.blocks) block.updateHash(hash);
  74. for (const variable of this.variables) variable.updateHash(hash);
  75. }
  76. disconnect() {
  77. for (const dep of this.dependencies) dep.disconnect();
  78. for (const block of this.blocks) block.disconnect();
  79. for (const variable of this.variables) variable.disconnect();
  80. }
  81. unseal() {
  82. for (const block of this.blocks) block.unseal();
  83. }
  84. /**
  85. * @param {DependencyFilterFunction} filter filter function for dependencies, gets passed all dependency ties from current instance
  86. * @returns {boolean} returns boolean for filter
  87. */
  88. hasDependencies(filter) {
  89. if (filter) {
  90. for (const dep of this.dependencies) {
  91. if (filter(dep)) return true;
  92. }
  93. } else {
  94. if (this.dependencies.length > 0) {
  95. return true;
  96. }
  97. }
  98. for (const block of this.blocks) {
  99. if (block.hasDependencies(filter)) return true;
  100. }
  101. for (const variable of this.variables) {
  102. if (variable.hasDependencies(filter)) return true;
  103. }
  104. return false;
  105. }
  106. sortItems() {
  107. for (const block of this.blocks) block.sortItems();
  108. }
  109. }
  110. module.exports = DependenciesBlock;