ModuleGraphConnection.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Dependency")} Dependency */
  7. /** @typedef {import("./Dependency").GetConditionFn} GetConditionFn */
  8. /** @typedef {import("./Module")} Module */
  9. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  10. /**
  11. * Module itself is not connected, but transitive modules are connected transitively.
  12. */
  13. const TRANSITIVE_ONLY = Symbol("transitive only");
  14. /**
  15. * While determining the active state, this flag is used to signal a circular connection.
  16. */
  17. const CIRCULAR_CONNECTION = Symbol("circular connection");
  18. /** @typedef {boolean | typeof TRANSITIVE_ONLY | typeof CIRCULAR_CONNECTION} ConnectionState */
  19. /**
  20. * @param {ConnectionState} a first
  21. * @param {ConnectionState} b second
  22. * @returns {ConnectionState} merged
  23. */
  24. const addConnectionStates = (a, b) => {
  25. if (a === true || b === true) return true;
  26. if (a === false) return b;
  27. if (b === false) return a;
  28. if (a === TRANSITIVE_ONLY) return b;
  29. if (b === TRANSITIVE_ONLY) return a;
  30. return a;
  31. };
  32. /**
  33. * @param {ConnectionState} a first
  34. * @param {ConnectionState} b second
  35. * @returns {ConnectionState} intersected
  36. */
  37. const intersectConnectionStates = (a, b) => {
  38. if (a === false || b === false) return false;
  39. if (a === true) return b;
  40. if (b === true) return a;
  41. if (a === CIRCULAR_CONNECTION) return b;
  42. if (b === CIRCULAR_CONNECTION) return a;
  43. return a;
  44. };
  45. class ModuleGraphConnection {
  46. /**
  47. * @param {Module | null} originModule the referencing module
  48. * @param {Dependency | null} dependency the referencing dependency
  49. * @param {Module} module the referenced module
  50. * @param {string=} explanation some extra detail
  51. * @param {boolean=} weak the reference is weak
  52. * @param {false | null | GetConditionFn=} condition condition for the connection
  53. */
  54. constructor(
  55. originModule,
  56. dependency,
  57. module,
  58. explanation,
  59. weak = false,
  60. condition = undefined
  61. ) {
  62. /** @type {Module | null} */
  63. this.originModule = originModule;
  64. /** @type {Module | null} */
  65. this.resolvedOriginModule = originModule;
  66. /** @type {Dependency | null} */
  67. this.dependency = dependency;
  68. /** @type {Module} */
  69. this.resolvedModule = module;
  70. /** @type {Module} */
  71. this.module = module;
  72. /** @type {boolean | undefined} */
  73. this.weak = weak;
  74. /** @type {boolean} */
  75. this.conditional = Boolean(condition);
  76. /** @type {boolean} */
  77. this._active = condition !== false;
  78. /** @type {false | null | GetConditionFn | undefined} */
  79. this.condition = condition || undefined;
  80. /** @type {Set<string> | undefined} */
  81. this.explanations = undefined;
  82. if (explanation) {
  83. this.explanations = new Set();
  84. this.explanations.add(explanation);
  85. }
  86. }
  87. clone() {
  88. const clone = new ModuleGraphConnection(
  89. this.resolvedOriginModule,
  90. this.dependency,
  91. this.resolvedModule,
  92. undefined,
  93. this.weak,
  94. this.condition
  95. );
  96. clone.originModule = this.originModule;
  97. clone.module = this.module;
  98. clone.conditional = this.conditional;
  99. clone._active = this._active;
  100. if (this.explanations) clone.explanations = new Set(this.explanations);
  101. return clone;
  102. }
  103. /**
  104. * @param {GetConditionFn} condition condition for the connection
  105. * @returns {void}
  106. */
  107. addCondition(condition) {
  108. if (this.conditional) {
  109. const old =
  110. /** @type {GetConditionFn} */
  111. (this.condition);
  112. /** @type {GetConditionFn} */
  113. (this.condition) = (c, r) =>
  114. intersectConnectionStates(old(c, r), condition(c, r));
  115. } else if (this._active) {
  116. this.conditional = true;
  117. this.condition = condition;
  118. }
  119. }
  120. /**
  121. * @param {string} explanation the explanation to add
  122. * @returns {void}
  123. */
  124. addExplanation(explanation) {
  125. if (this.explanations === undefined) {
  126. this.explanations = new Set();
  127. }
  128. this.explanations.add(explanation);
  129. }
  130. get explanation() {
  131. if (this.explanations === undefined) return "";
  132. return [...this.explanations].join(" ");
  133. }
  134. /**
  135. * @param {RuntimeSpec} runtime the runtime
  136. * @returns {boolean} true, if the connection is active
  137. */
  138. isActive(runtime) {
  139. if (!this.conditional) return this._active;
  140. return (
  141. /** @type {GetConditionFn} */ (this.condition)(this, runtime) !== false
  142. );
  143. }
  144. /**
  145. * @param {RuntimeSpec} runtime the runtime
  146. * @returns {boolean} true, if the connection is active
  147. */
  148. isTargetActive(runtime) {
  149. if (!this.conditional) return this._active;
  150. return (
  151. /** @type {GetConditionFn} */ (this.condition)(this, runtime) === true
  152. );
  153. }
  154. /**
  155. * @param {RuntimeSpec} runtime the runtime
  156. * @returns {ConnectionState} true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active
  157. */
  158. getActiveState(runtime) {
  159. if (!this.conditional) return this._active;
  160. return /** @type {GetConditionFn} */ (this.condition)(this, runtime);
  161. }
  162. /**
  163. * @param {boolean} value active or not
  164. * @returns {void}
  165. */
  166. setActive(value) {
  167. this.conditional = false;
  168. this._active = value;
  169. }
  170. }
  171. /** @typedef {typeof TRANSITIVE_ONLY} TRANSITIVE_ONLY */
  172. /** @typedef {typeof CIRCULAR_CONNECTION} CIRCULAR_CONNECTION */
  173. module.exports = ModuleGraphConnection;
  174. module.exports.CIRCULAR_CONNECTION = /** @type {typeof CIRCULAR_CONNECTION} */ (
  175. CIRCULAR_CONNECTION
  176. );
  177. module.exports.TRANSITIVE_ONLY = /** @type {typeof TRANSITIVE_ONLY} */ (
  178. TRANSITIVE_ONLY
  179. );
  180. module.exports.addConnectionStates = addConnectionStates;