ModuleGraphConnection.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * Adds connection states.
  21. * @param {ConnectionState} a first
  22. * @param {ConnectionState} b second
  23. * @returns {ConnectionState} merged
  24. */
  25. const addConnectionStates = (a, b) => {
  26. if (a === true || b === true) return true;
  27. if (a === false) return b;
  28. if (b === false) return a;
  29. if (a === TRANSITIVE_ONLY) return b;
  30. if (b === TRANSITIVE_ONLY) return a;
  31. return a;
  32. };
  33. /**
  34. * Intersect connection states.
  35. * @param {ConnectionState} a first
  36. * @param {ConnectionState} b second
  37. * @returns {ConnectionState} intersected
  38. */
  39. const intersectConnectionStates = (a, b) => {
  40. if (a === false || b === false) return false;
  41. if (a === true) return b;
  42. if (b === true) return a;
  43. if (a === CIRCULAR_CONNECTION) return b;
  44. if (b === CIRCULAR_CONNECTION) return a;
  45. return a;
  46. };
  47. class ModuleGraphConnection {
  48. /**
  49. * Creates an instance of ModuleGraphConnection.
  50. * @param {Module | null} originModule the referencing module
  51. * @param {Dependency | null} dependency the referencing dependency
  52. * @param {Module} module the referenced module
  53. * @param {string=} explanation some extra detail
  54. * @param {boolean=} weak the reference is weak
  55. * @param {false | null | GetConditionFn=} condition condition for the connection
  56. */
  57. constructor(
  58. originModule,
  59. dependency,
  60. module,
  61. explanation,
  62. weak = false,
  63. condition = undefined
  64. ) {
  65. /** @type {Module | null} */
  66. this.originModule = originModule;
  67. /** @type {Module | null} */
  68. this.resolvedOriginModule = originModule;
  69. /** @type {Dependency | null} */
  70. this.dependency = dependency;
  71. /** @type {Module} */
  72. this.resolvedModule = module;
  73. /** @type {Module} */
  74. this.module = module;
  75. /** @type {boolean | undefined} */
  76. this.weak = weak;
  77. /** @type {boolean} */
  78. this.conditional = Boolean(condition);
  79. /** @type {boolean} */
  80. this._active = condition !== false;
  81. /** @type {false | null | GetConditionFn | undefined} */
  82. this.condition = condition || undefined;
  83. /** @type {Set<string> | undefined} */
  84. this.explanations = undefined;
  85. if (explanation) {
  86. this.explanations = new Set();
  87. this.explanations.add(explanation);
  88. }
  89. }
  90. clone() {
  91. const clone = new ModuleGraphConnection(
  92. this.resolvedOriginModule,
  93. this.dependency,
  94. this.resolvedModule,
  95. undefined,
  96. this.weak,
  97. this.condition
  98. );
  99. clone.originModule = this.originModule;
  100. clone.module = this.module;
  101. clone.conditional = this.conditional;
  102. clone._active = this._active;
  103. if (this.explanations) clone.explanations = new Set(this.explanations);
  104. return clone;
  105. }
  106. /**
  107. * Adds the provided condition to the module graph connection.
  108. * @param {GetConditionFn} condition condition for the connection
  109. * @returns {void}
  110. */
  111. addCondition(condition) {
  112. if (this.conditional) {
  113. const old =
  114. /** @type {GetConditionFn} */
  115. (this.condition);
  116. /** @type {GetConditionFn} */
  117. (this.condition) = (c, r) =>
  118. intersectConnectionStates(old(c, r), condition(c, r));
  119. } else if (this._active) {
  120. this.conditional = true;
  121. this.condition = condition;
  122. }
  123. }
  124. /**
  125. * Adds the provided explanation to the module graph connection.
  126. * @param {string} explanation the explanation to add
  127. * @returns {void}
  128. */
  129. addExplanation(explanation) {
  130. if (this.explanations === undefined) {
  131. this.explanations = new Set();
  132. }
  133. this.explanations.add(explanation);
  134. }
  135. get explanation() {
  136. if (this.explanations === undefined) return "";
  137. return [...this.explanations].join(" ");
  138. }
  139. /**
  140. * Checks whether this module graph connection is active.
  141. * @param {RuntimeSpec} runtime the runtime
  142. * @returns {boolean} true, if the connection is active
  143. */
  144. isActive(runtime) {
  145. if (!this.conditional) return this._active;
  146. return (
  147. /** @type {GetConditionFn} */ (this.condition)(this, runtime) !== false
  148. );
  149. }
  150. /**
  151. * Checks whether this module graph connection is target active.
  152. * @param {RuntimeSpec} runtime the runtime
  153. * @returns {boolean} true, if the connection is active
  154. */
  155. isTargetActive(runtime) {
  156. if (!this.conditional) return this._active;
  157. return (
  158. /** @type {GetConditionFn} */ (this.condition)(this, runtime) === true
  159. );
  160. }
  161. /**
  162. * Returns true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active.
  163. * @param {RuntimeSpec} runtime the runtime
  164. * @returns {ConnectionState} true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active
  165. */
  166. getActiveState(runtime) {
  167. if (!this.conditional) return this._active;
  168. return /** @type {GetConditionFn} */ (this.condition)(this, runtime);
  169. }
  170. /**
  171. * Updates active using the provided value.
  172. * @param {boolean} value active or not
  173. * @returns {void}
  174. */
  175. setActive(value) {
  176. this.conditional = false;
  177. this._active = value;
  178. }
  179. }
  180. /** @typedef {typeof TRANSITIVE_ONLY} TRANSITIVE_ONLY */
  181. /** @typedef {typeof CIRCULAR_CONNECTION} CIRCULAR_CONNECTION */
  182. module.exports = ModuleGraphConnection;
  183. module.exports.CIRCULAR_CONNECTION = /** @type {typeof CIRCULAR_CONNECTION} */ (
  184. CIRCULAR_CONNECTION
  185. );
  186. module.exports.TRANSITIVE_ONLY = /** @type {typeof TRANSITIVE_ONLY} */ (
  187. TRANSITIVE_ONLY
  188. );
  189. module.exports.addConnectionStates = addConnectionStates;