提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.4 KiB

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