ModuleGraphConnection.js 5.6 KB

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