ModuleGraphConnection.js 5.5 KB

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