ModuleGraphConnection.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 | undefined} condition condition for the connection
  53. */
  54. constructor(
  55. originModule,
  56. dependency,
  57. module,
  58. explanation,
  59. weak = false,
  60. condition = undefined
  61. ) {
  62. this.originModule = originModule;
  63. this.resolvedOriginModule = originModule;
  64. this.dependency = dependency;
  65. this.resolvedModule = module;
  66. this.module = module;
  67. this.weak = weak;
  68. this.conditional = !!condition;
  69. this._active = condition !== false;
  70. /** @type {(function(ModuleGraphConnection, RuntimeSpec): ConnectionState) | undefined} */
  71. this.condition = condition || undefined;
  72. /** @type {Set<string> | undefined} */
  73. this.explanations = undefined;
  74. if (explanation) {
  75. this.explanations = new Set();
  76. this.explanations.add(explanation);
  77. }
  78. }
  79. clone() {
  80. const clone = new ModuleGraphConnection(
  81. this.resolvedOriginModule,
  82. this.dependency,
  83. this.resolvedModule,
  84. undefined,
  85. this.weak,
  86. this.condition
  87. );
  88. clone.originModule = this.originModule;
  89. clone.module = this.module;
  90. clone.conditional = this.conditional;
  91. clone._active = this._active;
  92. if (this.explanations) clone.explanations = new Set(this.explanations);
  93. return clone;
  94. }
  95. /**
  96. * @param {function(ModuleGraphConnection, RuntimeSpec): ConnectionState} condition condition for the connection
  97. * @returns {void}
  98. */
  99. addCondition(condition) {
  100. if (this.conditional) {
  101. const old =
  102. /** @type {(function(ModuleGraphConnection, RuntimeSpec): ConnectionState)} */
  103. (this.condition);
  104. this.condition = (c, r) =>
  105. intersectConnectionStates(old(c, r), condition(c, r));
  106. } else if (this._active) {
  107. this.conditional = true;
  108. this.condition = condition;
  109. }
  110. }
  111. /**
  112. * @param {string} explanation the explanation to add
  113. * @returns {void}
  114. */
  115. addExplanation(explanation) {
  116. if (this.explanations === undefined) {
  117. this.explanations = new Set();
  118. }
  119. this.explanations.add(explanation);
  120. }
  121. get explanation() {
  122. if (this.explanations === undefined) return "";
  123. return Array.from(this.explanations).join(" ");
  124. }
  125. // TODO webpack 5 remove
  126. get active() {
  127. throw new Error("Use getActiveState instead");
  128. }
  129. /**
  130. * @param {RuntimeSpec} runtime the runtime
  131. * @returns {boolean} true, if the connection is active
  132. */
  133. isActive(runtime) {
  134. if (!this.conditional) return this._active;
  135. return (
  136. /** @type {(function(ModuleGraphConnection, RuntimeSpec): ConnectionState)} */ (
  137. this.condition
  138. )(this, runtime) !== false
  139. );
  140. }
  141. /**
  142. * @param {RuntimeSpec} runtime the runtime
  143. * @returns {boolean} true, if the connection is active
  144. */
  145. isTargetActive(runtime) {
  146. if (!this.conditional) return this._active;
  147. return (
  148. /** @type {(function(ModuleGraphConnection, RuntimeSpec): ConnectionState)} */ (
  149. this.condition
  150. )(this, runtime) === true
  151. );
  152. }
  153. /**
  154. * @param {RuntimeSpec} runtime the runtime
  155. * @returns {ConnectionState} true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active
  156. */
  157. getActiveState(runtime) {
  158. if (!this.conditional) return this._active;
  159. return /** @type {(function(ModuleGraphConnection, RuntimeSpec): ConnectionState)} */ (
  160. this.condition
  161. )(this, runtime);
  162. }
  163. /**
  164. * @param {boolean} value active or not
  165. * @returns {void}
  166. */
  167. setActive(value) {
  168. this.conditional = false;
  169. this._active = value;
  170. }
  171. set active(value) {
  172. throw new Error("Use setActive instead");
  173. }
  174. }
  175. /** @typedef {typeof TRANSITIVE_ONLY} TRANSITIVE_ONLY */
  176. /** @typedef {typeof CIRCULAR_CONNECTION} CIRCULAR_CONNECTION */
  177. module.exports = ModuleGraphConnection;
  178. module.exports.addConnectionStates = addConnectionStates;
  179. module.exports.TRANSITIVE_ONLY = /** @type {typeof TRANSITIVE_ONLY} */ (
  180. TRANSITIVE_ONLY
  181. );
  182. module.exports.CIRCULAR_CONNECTION = /** @type {typeof CIRCULAR_CONNECTION} */ (
  183. CIRCULAR_CONNECTION
  184. );