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