ModuleGraphConnection.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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} */
  70. this.condition = condition || undefined;
  71. /** @type {Set<string>} */
  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 = this.condition;
  101. this.condition = (c, r) =>
  102. intersectConnectionStates(old(c, r), condition(c, r));
  103. } else if (this._active) {
  104. this.conditional = true;
  105. this.condition = condition;
  106. }
  107. }
  108. /**
  109. * @param {string} explanation the explanation to add
  110. * @returns {void}
  111. */
  112. addExplanation(explanation) {
  113. if (this.explanations === undefined) {
  114. this.explanations = new Set();
  115. }
  116. this.explanations.add(explanation);
  117. }
  118. get explanation() {
  119. if (this.explanations === undefined) return "";
  120. return Array.from(this.explanations).join(" ");
  121. }
  122. // TODO webpack 5 remove
  123. get active() {
  124. throw new Error("Use getActiveState instead");
  125. }
  126. /**
  127. * @param {RuntimeSpec} runtime the runtime
  128. * @returns {boolean} true, if the connection is active
  129. */
  130. isActive(runtime) {
  131. if (!this.conditional) return this._active;
  132. return this.condition(this, runtime) !== false;
  133. }
  134. /**
  135. * @param {RuntimeSpec} runtime the runtime
  136. * @returns {boolean} true, if the connection is active
  137. */
  138. isTargetActive(runtime) {
  139. if (!this.conditional) return this._active;
  140. return this.condition(this, runtime) === true;
  141. }
  142. /**
  143. * @param {RuntimeSpec} runtime the runtime
  144. * @returns {ConnectionState} true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active
  145. */
  146. getActiveState(runtime) {
  147. if (!this.conditional) return this._active;
  148. return this.condition(this, runtime);
  149. }
  150. /**
  151. * @param {boolean} value active or not
  152. * @returns {void}
  153. */
  154. setActive(value) {
  155. this.conditional = false;
  156. this._active = value;
  157. }
  158. set active(value) {
  159. throw new Error("Use setActive instead");
  160. }
  161. }
  162. /** @typedef {typeof TRANSITIVE_ONLY} TRANSITIVE_ONLY */
  163. /** @typedef {typeof CIRCULAR_CONNECTION} CIRCULAR_CONNECTION */
  164. module.exports = ModuleGraphConnection;
  165. module.exports.addConnectionStates = addConnectionStates;
  166. module.exports.TRANSITIVE_ONLY = /** @type {typeof TRANSITIVE_ONLY} */ (
  167. TRANSITIVE_ONLY
  168. );
  169. module.exports.CIRCULAR_CONNECTION = /** @type {typeof CIRCULAR_CONNECTION} */ (
  170. CIRCULAR_CONNECTION
  171. );