[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const util = require("util");
|
---|
| 9 | const ExportsInfo = require("./ExportsInfo");
|
---|
| 10 | const ModuleGraphConnection = require("./ModuleGraphConnection");
|
---|
| 11 | const SortableSet = require("./util/SortableSet");
|
---|
| 12 | const WeakTupleMap = require("./util/WeakTupleMap");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
|
---|
| 15 | /** @typedef {import("./Dependency")} Dependency */
|
---|
| 16 | /** @typedef {import("./ExportsInfo").ExportInfo} ExportInfo */
|
---|
| 17 | /** @typedef {import("./Module")} Module */
|
---|
| 18 | /** @typedef {import("./ModuleProfile")} ModuleProfile */
|
---|
| 19 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
---|
| 20 | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * @callback OptimizationBailoutFunction
|
---|
| 24 | * @param {RequestShortener} requestShortener
|
---|
| 25 | * @returns {string}
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | const EMPTY_SET = new Set();
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * @param {SortableSet<ModuleGraphConnection>} set input
|
---|
| 32 | * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]>} mapped by origin module
|
---|
| 33 | */
|
---|
| 34 | const getConnectionsByOriginModule = set => {
|
---|
| 35 | const map = new Map();
|
---|
| 36 | /** @type {Module | 0} */
|
---|
| 37 | let lastModule = 0;
|
---|
| 38 | /** @type {ModuleGraphConnection[] | undefined} */
|
---|
| 39 | let lastList;
|
---|
| 40 | for (const connection of set) {
|
---|
| 41 | const { originModule } = connection;
|
---|
| 42 | if (lastModule === originModule) {
|
---|
| 43 | /** @type {ModuleGraphConnection[]} */
|
---|
| 44 | (lastList).push(connection);
|
---|
| 45 | } else {
|
---|
| 46 | lastModule = /** @type {Module} */ (originModule);
|
---|
| 47 | const list = map.get(originModule);
|
---|
| 48 | if (list !== undefined) {
|
---|
| 49 | lastList = list;
|
---|
| 50 | list.push(connection);
|
---|
| 51 | } else {
|
---|
| 52 | const list = [connection];
|
---|
| 53 | lastList = list;
|
---|
| 54 | map.set(originModule, list);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | return map;
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * @param {SortableSet<ModuleGraphConnection>} set input
|
---|
| 63 | * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]>} mapped by module
|
---|
| 64 | */
|
---|
| 65 | const getConnectionsByModule = set => {
|
---|
| 66 | const map = new Map();
|
---|
| 67 | /** @type {Module | 0} */
|
---|
| 68 | let lastModule = 0;
|
---|
| 69 | /** @type {ModuleGraphConnection[] | undefined} */
|
---|
| 70 | let lastList;
|
---|
| 71 | for (const connection of set) {
|
---|
| 72 | const { module } = connection;
|
---|
| 73 | if (lastModule === module) {
|
---|
| 74 | /** @type {ModuleGraphConnection[]} */
|
---|
| 75 | (lastList).push(connection);
|
---|
| 76 | } else {
|
---|
| 77 | lastModule = module;
|
---|
| 78 | const list = map.get(module);
|
---|
| 79 | if (list !== undefined) {
|
---|
| 80 | lastList = list;
|
---|
| 81 | list.push(connection);
|
---|
| 82 | } else {
|
---|
| 83 | const list = [connection];
|
---|
| 84 | lastList = list;
|
---|
| 85 | map.set(module, list);
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | return map;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | /** @typedef {SortableSet<ModuleGraphConnection>} IncomingConnections */
|
---|
| 93 | /** @typedef {SortableSet<ModuleGraphConnection>} OutgoingConnections */
|
---|
| 94 |
|
---|
| 95 | class ModuleGraphModule {
|
---|
| 96 | constructor() {
|
---|
| 97 | /** @type {IncomingConnections} */
|
---|
| 98 | this.incomingConnections = new SortableSet();
|
---|
| 99 | /** @type {OutgoingConnections | undefined} */
|
---|
| 100 | this.outgoingConnections = undefined;
|
---|
| 101 | /** @type {Module | null | undefined} */
|
---|
| 102 | this.issuer = undefined;
|
---|
| 103 | /** @type {(string | OptimizationBailoutFunction)[]} */
|
---|
| 104 | this.optimizationBailout = [];
|
---|
| 105 | /** @type {ExportsInfo} */
|
---|
| 106 | this.exports = new ExportsInfo();
|
---|
| 107 | /** @type {number | null} */
|
---|
| 108 | this.preOrderIndex = null;
|
---|
| 109 | /** @type {number | null} */
|
---|
| 110 | this.postOrderIndex = null;
|
---|
| 111 | /** @type {number | null} */
|
---|
| 112 | this.depth = null;
|
---|
| 113 | /** @type {ModuleProfile | undefined} */
|
---|
| 114 | this.profile = undefined;
|
---|
| 115 | /** @type {boolean} */
|
---|
| 116 | this.async = false;
|
---|
| 117 | /** @type {ModuleGraphConnection[] | undefined} */
|
---|
| 118 | this._unassignedConnections = undefined;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | class ModuleGraph {
|
---|
| 123 | constructor() {
|
---|
| 124 | /**
|
---|
| 125 | * @type {WeakMap<Dependency, ModuleGraphConnection | null>}
|
---|
| 126 | * @private
|
---|
| 127 | */
|
---|
| 128 | this._dependencyMap = new WeakMap();
|
---|
| 129 | /**
|
---|
| 130 | * @type {Map<Module, ModuleGraphModule>}
|
---|
| 131 | * @private
|
---|
| 132 | */
|
---|
| 133 | this._moduleMap = new Map();
|
---|
| 134 | /**
|
---|
| 135 | * @type {WeakMap<any, object>}
|
---|
| 136 | * @private
|
---|
| 137 | */
|
---|
| 138 | this._metaMap = new WeakMap();
|
---|
| 139 | /**
|
---|
| 140 | * @type {WeakTupleMap<any[], any> | undefined}
|
---|
| 141 | * @private
|
---|
| 142 | */
|
---|
| 143 | this._cache = undefined;
|
---|
| 144 | /**
|
---|
| 145 | * @type {Map<Module, WeakTupleMap<any, any>> | undefined}
|
---|
| 146 | * @private
|
---|
| 147 | */
|
---|
| 148 | this._moduleMemCaches = undefined;
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * @type {string | undefined}
|
---|
| 152 | * @private
|
---|
| 153 | */
|
---|
| 154 | this._cacheStage = undefined;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * @param {Module} module the module
|
---|
| 159 | * @returns {ModuleGraphModule} the internal module
|
---|
| 160 | */
|
---|
| 161 | _getModuleGraphModule(module) {
|
---|
| 162 | let mgm = this._moduleMap.get(module);
|
---|
| 163 | if (mgm === undefined) {
|
---|
| 164 | mgm = new ModuleGraphModule();
|
---|
| 165 | this._moduleMap.set(module, mgm);
|
---|
| 166 | }
|
---|
| 167 | return mgm;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * @param {Dependency} dependency the dependency
|
---|
| 172 | * @param {DependenciesBlock} block parent block
|
---|
| 173 | * @param {Module} module parent module
|
---|
| 174 | * @param {number=} indexInBlock position in block
|
---|
| 175 | * @returns {void}
|
---|
| 176 | */
|
---|
| 177 | setParents(dependency, block, module, indexInBlock = -1) {
|
---|
| 178 | dependency._parentDependenciesBlockIndex = indexInBlock;
|
---|
| 179 | dependency._parentDependenciesBlock = block;
|
---|
| 180 | dependency._parentModule = module;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * @param {Dependency} dependency the dependency
|
---|
| 185 | * @returns {Module | undefined} parent module
|
---|
| 186 | */
|
---|
| 187 | getParentModule(dependency) {
|
---|
| 188 | return dependency._parentModule;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /**
|
---|
| 192 | * @param {Dependency} dependency the dependency
|
---|
| 193 | * @returns {DependenciesBlock | undefined} parent block
|
---|
| 194 | */
|
---|
| 195 | getParentBlock(dependency) {
|
---|
| 196 | return dependency._parentDependenciesBlock;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * @param {Dependency} dependency the dependency
|
---|
| 201 | * @returns {number} index
|
---|
| 202 | */
|
---|
| 203 | getParentBlockIndex(dependency) {
|
---|
| 204 | return dependency._parentDependenciesBlockIndex;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * @param {Module | null} originModule the referencing module
|
---|
| 209 | * @param {Dependency} dependency the referencing dependency
|
---|
| 210 | * @param {Module} module the referenced module
|
---|
| 211 | * @returns {void}
|
---|
| 212 | */
|
---|
| 213 | setResolvedModule(originModule, dependency, module) {
|
---|
| 214 | const connection = new ModuleGraphConnection(
|
---|
| 215 | originModule,
|
---|
| 216 | dependency,
|
---|
| 217 | module,
|
---|
| 218 | undefined,
|
---|
| 219 | dependency.weak,
|
---|
| 220 | dependency.getCondition(this)
|
---|
| 221 | );
|
---|
| 222 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
---|
| 223 | connections.add(connection);
|
---|
| 224 | if (originModule) {
|
---|
| 225 | const mgm = this._getModuleGraphModule(originModule);
|
---|
| 226 | if (mgm._unassignedConnections === undefined) {
|
---|
| 227 | mgm._unassignedConnections = [];
|
---|
| 228 | }
|
---|
| 229 | mgm._unassignedConnections.push(connection);
|
---|
| 230 | if (mgm.outgoingConnections === undefined) {
|
---|
| 231 | mgm.outgoingConnections = new SortableSet();
|
---|
| 232 | }
|
---|
| 233 | mgm.outgoingConnections.add(connection);
|
---|
| 234 | } else {
|
---|
| 235 | this._dependencyMap.set(dependency, connection);
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
| 240 | * @param {Dependency} dependency the referencing dependency
|
---|
| 241 | * @param {Module} module the referenced module
|
---|
| 242 | * @returns {void}
|
---|
| 243 | */
|
---|
| 244 | updateModule(dependency, module) {
|
---|
| 245 | const connection =
|
---|
| 246 | /** @type {ModuleGraphConnection} */
|
---|
| 247 | (this.getConnection(dependency));
|
---|
| 248 | if (connection.module === module) return;
|
---|
| 249 | const newConnection = connection.clone();
|
---|
| 250 | newConnection.module = module;
|
---|
| 251 | this._dependencyMap.set(dependency, newConnection);
|
---|
| 252 | connection.setActive(false);
|
---|
| 253 | const originMgm = this._getModuleGraphModule(
|
---|
| 254 | /** @type {Module} */ (connection.originModule)
|
---|
| 255 | );
|
---|
| 256 | /** @type {OutgoingConnections} */
|
---|
| 257 | (originMgm.outgoingConnections).add(newConnection);
|
---|
| 258 | const targetMgm = this._getModuleGraphModule(module);
|
---|
| 259 | targetMgm.incomingConnections.add(newConnection);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /**
|
---|
| 263 | * @param {Dependency} dependency the referencing dependency
|
---|
| 264 | * @returns {void}
|
---|
| 265 | */
|
---|
| 266 | removeConnection(dependency) {
|
---|
| 267 | const connection =
|
---|
| 268 | /** @type {ModuleGraphConnection} */
|
---|
| 269 | (this.getConnection(dependency));
|
---|
| 270 | const targetMgm = this._getModuleGraphModule(connection.module);
|
---|
| 271 | targetMgm.incomingConnections.delete(connection);
|
---|
| 272 | const originMgm = this._getModuleGraphModule(
|
---|
| 273 | /** @type {Module} */ (connection.originModule)
|
---|
| 274 | );
|
---|
| 275 | /** @type {OutgoingConnections} */
|
---|
| 276 | (originMgm.outgoingConnections).delete(connection);
|
---|
| 277 | this._dependencyMap.set(dependency, null);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * @param {Dependency} dependency the referencing dependency
|
---|
| 282 | * @param {string} explanation an explanation
|
---|
| 283 | * @returns {void}
|
---|
| 284 | */
|
---|
| 285 | addExplanation(dependency, explanation) {
|
---|
| 286 | const connection =
|
---|
| 287 | /** @type {ModuleGraphConnection} */
|
---|
| 288 | (this.getConnection(dependency));
|
---|
| 289 | connection.addExplanation(explanation);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /**
|
---|
| 293 | * @param {Module} sourceModule the source module
|
---|
| 294 | * @param {Module} targetModule the target module
|
---|
| 295 | * @returns {void}
|
---|
| 296 | */
|
---|
| 297 | cloneModuleAttributes(sourceModule, targetModule) {
|
---|
| 298 | const oldMgm = this._getModuleGraphModule(sourceModule);
|
---|
| 299 | const newMgm = this._getModuleGraphModule(targetModule);
|
---|
| 300 | newMgm.postOrderIndex = oldMgm.postOrderIndex;
|
---|
| 301 | newMgm.preOrderIndex = oldMgm.preOrderIndex;
|
---|
| 302 | newMgm.depth = oldMgm.depth;
|
---|
| 303 | newMgm.exports = oldMgm.exports;
|
---|
| 304 | newMgm.async = oldMgm.async;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /**
|
---|
| 308 | * @param {Module} module the module
|
---|
| 309 | * @returns {void}
|
---|
| 310 | */
|
---|
| 311 | removeModuleAttributes(module) {
|
---|
| 312 | const mgm = this._getModuleGraphModule(module);
|
---|
| 313 | mgm.postOrderIndex = null;
|
---|
| 314 | mgm.preOrderIndex = null;
|
---|
| 315 | mgm.depth = null;
|
---|
| 316 | mgm.async = false;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /**
|
---|
| 320 | * @returns {void}
|
---|
| 321 | */
|
---|
| 322 | removeAllModuleAttributes() {
|
---|
| 323 | for (const mgm of this._moduleMap.values()) {
|
---|
| 324 | mgm.postOrderIndex = null;
|
---|
| 325 | mgm.preOrderIndex = null;
|
---|
| 326 | mgm.depth = null;
|
---|
| 327 | mgm.async = false;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | * @param {Module} oldModule the old referencing module
|
---|
| 333 | * @param {Module} newModule the new referencing module
|
---|
| 334 | * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
|
---|
| 335 | * @returns {void}
|
---|
| 336 | */
|
---|
| 337 | moveModuleConnections(oldModule, newModule, filterConnection) {
|
---|
| 338 | if (oldModule === newModule) return;
|
---|
| 339 | const oldMgm = this._getModuleGraphModule(oldModule);
|
---|
| 340 | const newMgm = this._getModuleGraphModule(newModule);
|
---|
| 341 | // Outgoing connections
|
---|
| 342 | const oldConnections = oldMgm.outgoingConnections;
|
---|
| 343 | if (oldConnections !== undefined) {
|
---|
| 344 | if (newMgm.outgoingConnections === undefined) {
|
---|
| 345 | newMgm.outgoingConnections = new SortableSet();
|
---|
| 346 | }
|
---|
| 347 | const newConnections = newMgm.outgoingConnections;
|
---|
| 348 | for (const connection of oldConnections) {
|
---|
| 349 | if (filterConnection(connection)) {
|
---|
| 350 | connection.originModule = newModule;
|
---|
| 351 | newConnections.add(connection);
|
---|
| 352 | oldConnections.delete(connection);
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 | }
|
---|
| 356 | // Incoming connections
|
---|
| 357 | const oldConnections2 = oldMgm.incomingConnections;
|
---|
| 358 | const newConnections2 = newMgm.incomingConnections;
|
---|
| 359 | for (const connection of oldConnections2) {
|
---|
| 360 | if (filterConnection(connection)) {
|
---|
| 361 | connection.module = newModule;
|
---|
| 362 | newConnections2.add(connection);
|
---|
| 363 | oldConnections2.delete(connection);
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /**
|
---|
| 369 | * @param {Module} oldModule the old referencing module
|
---|
| 370 | * @param {Module} newModule the new referencing module
|
---|
| 371 | * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
|
---|
| 372 | * @returns {void}
|
---|
| 373 | */
|
---|
| 374 | copyOutgoingModuleConnections(oldModule, newModule, filterConnection) {
|
---|
| 375 | if (oldModule === newModule) return;
|
---|
| 376 | const oldMgm = this._getModuleGraphModule(oldModule);
|
---|
| 377 | const newMgm = this._getModuleGraphModule(newModule);
|
---|
| 378 | // Outgoing connections
|
---|
| 379 | const oldConnections = oldMgm.outgoingConnections;
|
---|
| 380 | if (oldConnections !== undefined) {
|
---|
| 381 | if (newMgm.outgoingConnections === undefined) {
|
---|
| 382 | newMgm.outgoingConnections = new SortableSet();
|
---|
| 383 | }
|
---|
| 384 | const newConnections = newMgm.outgoingConnections;
|
---|
| 385 | for (const connection of oldConnections) {
|
---|
| 386 | if (filterConnection(connection)) {
|
---|
| 387 | const newConnection = connection.clone();
|
---|
| 388 | newConnection.originModule = newModule;
|
---|
| 389 | newConnections.add(newConnection);
|
---|
| 390 | if (newConnection.module !== undefined) {
|
---|
| 391 | const otherMgm = this._getModuleGraphModule(newConnection.module);
|
---|
| 392 | otherMgm.incomingConnections.add(newConnection);
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | /**
|
---|
| 400 | * @param {Module} module the referenced module
|
---|
| 401 | * @param {string} explanation an explanation why it's referenced
|
---|
| 402 | * @returns {void}
|
---|
| 403 | */
|
---|
| 404 | addExtraReason(module, explanation) {
|
---|
| 405 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
---|
| 406 | connections.add(new ModuleGraphConnection(null, null, module, explanation));
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | /**
|
---|
| 410 | * @param {Dependency} dependency the dependency to look for a referenced module
|
---|
| 411 | * @returns {Module | null} the referenced module
|
---|
| 412 | */
|
---|
| 413 | getResolvedModule(dependency) {
|
---|
| 414 | const connection = this.getConnection(dependency);
|
---|
| 415 | return connection !== undefined ? connection.resolvedModule : null;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | /**
|
---|
| 419 | * @param {Dependency} dependency the dependency to look for a referenced module
|
---|
| 420 | * @returns {ModuleGraphConnection | undefined} the connection
|
---|
| 421 | */
|
---|
| 422 | getConnection(dependency) {
|
---|
| 423 | const connection = this._dependencyMap.get(dependency);
|
---|
| 424 | if (connection === undefined) {
|
---|
| 425 | const module = this.getParentModule(dependency);
|
---|
| 426 | if (module !== undefined) {
|
---|
| 427 | const mgm = this._getModuleGraphModule(module);
|
---|
| 428 | if (
|
---|
| 429 | mgm._unassignedConnections &&
|
---|
| 430 | mgm._unassignedConnections.length !== 0
|
---|
| 431 | ) {
|
---|
| 432 | let foundConnection;
|
---|
| 433 | for (const connection of mgm._unassignedConnections) {
|
---|
| 434 | this._dependencyMap.set(
|
---|
| 435 | /** @type {Dependency} */ (connection.dependency),
|
---|
| 436 | connection
|
---|
| 437 | );
|
---|
| 438 | if (connection.dependency === dependency)
|
---|
| 439 | foundConnection = connection;
|
---|
| 440 | }
|
---|
| 441 | mgm._unassignedConnections.length = 0;
|
---|
| 442 | if (foundConnection !== undefined) {
|
---|
| 443 | return foundConnection;
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 | }
|
---|
| 447 | this._dependencyMap.set(dependency, null);
|
---|
| 448 | return;
|
---|
| 449 | }
|
---|
| 450 | return connection === null ? undefined : connection;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | /**
|
---|
| 454 | * @param {Dependency} dependency the dependency to look for a referenced module
|
---|
| 455 | * @returns {Module | null} the referenced module
|
---|
| 456 | */
|
---|
| 457 | getModule(dependency) {
|
---|
| 458 | const connection = this.getConnection(dependency);
|
---|
| 459 | return connection !== undefined ? connection.module : null;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | /**
|
---|
| 463 | * @param {Dependency} dependency the dependency to look for a referencing module
|
---|
| 464 | * @returns {Module | null} the referencing module
|
---|
| 465 | */
|
---|
| 466 | getOrigin(dependency) {
|
---|
| 467 | const connection = this.getConnection(dependency);
|
---|
| 468 | return connection !== undefined ? connection.originModule : null;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | /**
|
---|
| 472 | * @param {Dependency} dependency the dependency to look for a referencing module
|
---|
| 473 | * @returns {Module | null} the original referencing module
|
---|
| 474 | */
|
---|
| 475 | getResolvedOrigin(dependency) {
|
---|
| 476 | const connection = this.getConnection(dependency);
|
---|
| 477 | return connection !== undefined ? connection.resolvedOriginModule : null;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | /**
|
---|
| 481 | * @param {Module} module the module
|
---|
| 482 | * @returns {Iterable<ModuleGraphConnection>} reasons why a module is included
|
---|
| 483 | */
|
---|
| 484 | getIncomingConnections(module) {
|
---|
| 485 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
---|
| 486 | return connections;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | /**
|
---|
| 490 | * @param {Module} module the module
|
---|
| 491 | * @returns {Iterable<ModuleGraphConnection>} list of outgoing connections
|
---|
| 492 | */
|
---|
| 493 | getOutgoingConnections(module) {
|
---|
| 494 | const connections = this._getModuleGraphModule(module).outgoingConnections;
|
---|
| 495 | return connections === undefined ? EMPTY_SET : connections;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | /**
|
---|
| 499 | * @param {Module} module the module
|
---|
| 500 | * @returns {readonly Map<Module | undefined | null, readonly ModuleGraphConnection[]>} reasons why a module is included, in a map by source module
|
---|
| 501 | */
|
---|
| 502 | getIncomingConnectionsByOriginModule(module) {
|
---|
| 503 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
---|
| 504 | return connections.getFromUnorderedCache(getConnectionsByOriginModule);
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | /**
|
---|
| 508 | * @param {Module} module the module
|
---|
| 509 | * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]> | undefined} connections to modules, in a map by module
|
---|
| 510 | */
|
---|
| 511 | getOutgoingConnectionsByModule(module) {
|
---|
| 512 | const connections = this._getModuleGraphModule(module).outgoingConnections;
|
---|
| 513 | return connections === undefined
|
---|
| 514 | ? undefined
|
---|
| 515 | : connections.getFromUnorderedCache(getConnectionsByModule);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | /**
|
---|
| 519 | * @param {Module} module the module
|
---|
| 520 | * @returns {ModuleProfile | undefined} the module profile
|
---|
| 521 | */
|
---|
| 522 | getProfile(module) {
|
---|
| 523 | const mgm = this._getModuleGraphModule(module);
|
---|
| 524 | return mgm.profile;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | /**
|
---|
| 528 | * @param {Module} module the module
|
---|
| 529 | * @param {ModuleProfile | undefined} profile the module profile
|
---|
| 530 | * @returns {void}
|
---|
| 531 | */
|
---|
| 532 | setProfile(module, profile) {
|
---|
| 533 | const mgm = this._getModuleGraphModule(module);
|
---|
| 534 | mgm.profile = profile;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | /**
|
---|
| 538 | * @param {Module} module the module
|
---|
| 539 | * @returns {Module | null | undefined} the issuer module
|
---|
| 540 | */
|
---|
| 541 | getIssuer(module) {
|
---|
| 542 | const mgm = this._getModuleGraphModule(module);
|
---|
| 543 | return mgm.issuer;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | /**
|
---|
| 547 | * @param {Module} module the module
|
---|
| 548 | * @param {Module | null} issuer the issuer module
|
---|
| 549 | * @returns {void}
|
---|
| 550 | */
|
---|
| 551 | setIssuer(module, issuer) {
|
---|
| 552 | const mgm = this._getModuleGraphModule(module);
|
---|
| 553 | mgm.issuer = issuer;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | /**
|
---|
| 557 | * @param {Module} module the module
|
---|
| 558 | * @param {Module | null} issuer the issuer module
|
---|
| 559 | * @returns {void}
|
---|
| 560 | */
|
---|
| 561 | setIssuerIfUnset(module, issuer) {
|
---|
| 562 | const mgm = this._getModuleGraphModule(module);
|
---|
| 563 | if (mgm.issuer === undefined) mgm.issuer = issuer;
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | /**
|
---|
| 567 | * @param {Module} module the module
|
---|
| 568 | * @returns {(string | OptimizationBailoutFunction)[]} optimization bailouts
|
---|
| 569 | */
|
---|
| 570 | getOptimizationBailout(module) {
|
---|
| 571 | const mgm = this._getModuleGraphModule(module);
|
---|
| 572 | return mgm.optimizationBailout;
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | /**
|
---|
| 576 | * @param {Module} module the module
|
---|
| 577 | * @returns {true | string[] | null} the provided exports
|
---|
| 578 | */
|
---|
| 579 | getProvidedExports(module) {
|
---|
| 580 | const mgm = this._getModuleGraphModule(module);
|
---|
| 581 | return mgm.exports.getProvidedExports();
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | /**
|
---|
| 585 | * @param {Module} module the module
|
---|
| 586 | * @param {string | string[]} exportName a name of an export
|
---|
| 587 | * @returns {boolean | null} true, if the export is provided by the module.
|
---|
| 588 | * null, if it's unknown.
|
---|
| 589 | * false, if it's not provided.
|
---|
| 590 | */
|
---|
| 591 | isExportProvided(module, exportName) {
|
---|
| 592 | const mgm = this._getModuleGraphModule(module);
|
---|
| 593 | const result = mgm.exports.isExportProvided(exportName);
|
---|
| 594 | return result === undefined ? null : result;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | /**
|
---|
| 598 | * @param {Module} module the module
|
---|
| 599 | * @returns {ExportsInfo} info about the exports
|
---|
| 600 | */
|
---|
| 601 | getExportsInfo(module) {
|
---|
| 602 | const mgm = this._getModuleGraphModule(module);
|
---|
| 603 | return mgm.exports;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | /**
|
---|
| 607 | * @param {Module} module the module
|
---|
| 608 | * @param {string} exportName the export
|
---|
| 609 | * @returns {ExportInfo} info about the export
|
---|
| 610 | */
|
---|
| 611 | getExportInfo(module, exportName) {
|
---|
| 612 | const mgm = this._getModuleGraphModule(module);
|
---|
| 613 | return mgm.exports.getExportInfo(exportName);
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /**
|
---|
| 617 | * @param {Module} module the module
|
---|
| 618 | * @param {string} exportName the export
|
---|
| 619 | * @returns {ExportInfo} info about the export (do not modify)
|
---|
| 620 | */
|
---|
| 621 | getReadOnlyExportInfo(module, exportName) {
|
---|
| 622 | const mgm = this._getModuleGraphModule(module);
|
---|
| 623 | return mgm.exports.getReadOnlyExportInfo(exportName);
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | /**
|
---|
| 627 | * @param {Module} module the module
|
---|
| 628 | * @param {RuntimeSpec} runtime the runtime
|
---|
| 629 | * @returns {false | true | SortableSet<string> | null} the used exports
|
---|
| 630 | * false: module is not used at all.
|
---|
| 631 | * true: the module namespace/object export is used.
|
---|
| 632 | * SortableSet<string>: these export names are used.
|
---|
| 633 | * empty SortableSet<string>: module is used but no export.
|
---|
| 634 | * null: unknown, worst case should be assumed.
|
---|
| 635 | */
|
---|
| 636 | getUsedExports(module, runtime) {
|
---|
| 637 | const mgm = this._getModuleGraphModule(module);
|
---|
| 638 | return mgm.exports.getUsedExports(runtime);
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | /**
|
---|
| 642 | * @param {Module} module the module
|
---|
| 643 | * @returns {number | null} the index of the module
|
---|
| 644 | */
|
---|
| 645 | getPreOrderIndex(module) {
|
---|
| 646 | const mgm = this._getModuleGraphModule(module);
|
---|
| 647 | return mgm.preOrderIndex;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | /**
|
---|
| 651 | * @param {Module} module the module
|
---|
| 652 | * @returns {number | null} the index of the module
|
---|
| 653 | */
|
---|
| 654 | getPostOrderIndex(module) {
|
---|
| 655 | const mgm = this._getModuleGraphModule(module);
|
---|
| 656 | return mgm.postOrderIndex;
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | /**
|
---|
| 660 | * @param {Module} module the module
|
---|
| 661 | * @param {number} index the index of the module
|
---|
| 662 | * @returns {void}
|
---|
| 663 | */
|
---|
| 664 | setPreOrderIndex(module, index) {
|
---|
| 665 | const mgm = this._getModuleGraphModule(module);
|
---|
| 666 | mgm.preOrderIndex = index;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | /**
|
---|
| 670 | * @param {Module} module the module
|
---|
| 671 | * @param {number} index the index of the module
|
---|
| 672 | * @returns {boolean} true, if the index was set
|
---|
| 673 | */
|
---|
| 674 | setPreOrderIndexIfUnset(module, index) {
|
---|
| 675 | const mgm = this._getModuleGraphModule(module);
|
---|
| 676 | if (mgm.preOrderIndex === null) {
|
---|
| 677 | mgm.preOrderIndex = index;
|
---|
| 678 | return true;
|
---|
| 679 | }
|
---|
| 680 | return false;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | /**
|
---|
| 684 | * @param {Module} module the module
|
---|
| 685 | * @param {number} index the index of the module
|
---|
| 686 | * @returns {void}
|
---|
| 687 | */
|
---|
| 688 | setPostOrderIndex(module, index) {
|
---|
| 689 | const mgm = this._getModuleGraphModule(module);
|
---|
| 690 | mgm.postOrderIndex = index;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | /**
|
---|
| 694 | * @param {Module} module the module
|
---|
| 695 | * @param {number} index the index of the module
|
---|
| 696 | * @returns {boolean} true, if the index was set
|
---|
| 697 | */
|
---|
| 698 | setPostOrderIndexIfUnset(module, index) {
|
---|
| 699 | const mgm = this._getModuleGraphModule(module);
|
---|
| 700 | if (mgm.postOrderIndex === null) {
|
---|
| 701 | mgm.postOrderIndex = index;
|
---|
| 702 | return true;
|
---|
| 703 | }
|
---|
| 704 | return false;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | /**
|
---|
| 708 | * @param {Module} module the module
|
---|
| 709 | * @returns {number | null} the depth of the module
|
---|
| 710 | */
|
---|
| 711 | getDepth(module) {
|
---|
| 712 | const mgm = this._getModuleGraphModule(module);
|
---|
| 713 | return mgm.depth;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | /**
|
---|
| 717 | * @param {Module} module the module
|
---|
| 718 | * @param {number} depth the depth of the module
|
---|
| 719 | * @returns {void}
|
---|
| 720 | */
|
---|
| 721 | setDepth(module, depth) {
|
---|
| 722 | const mgm = this._getModuleGraphModule(module);
|
---|
| 723 | mgm.depth = depth;
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | /**
|
---|
| 727 | * @param {Module} module the module
|
---|
| 728 | * @param {number} depth the depth of the module
|
---|
| 729 | * @returns {boolean} true, if the depth was set
|
---|
| 730 | */
|
---|
| 731 | setDepthIfLower(module, depth) {
|
---|
| 732 | const mgm = this._getModuleGraphModule(module);
|
---|
| 733 | if (mgm.depth === null || mgm.depth > depth) {
|
---|
| 734 | mgm.depth = depth;
|
---|
| 735 | return true;
|
---|
| 736 | }
|
---|
| 737 | return false;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | /**
|
---|
| 741 | * @param {Module} module the module
|
---|
| 742 | * @returns {boolean} true, if the module is async
|
---|
| 743 | */
|
---|
| 744 | isAsync(module) {
|
---|
| 745 | const mgm = this._getModuleGraphModule(module);
|
---|
| 746 | return mgm.async;
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | /**
|
---|
| 750 | * @param {Module} module the module
|
---|
| 751 | * @returns {void}
|
---|
| 752 | */
|
---|
| 753 | setAsync(module) {
|
---|
| 754 | const mgm = this._getModuleGraphModule(module);
|
---|
| 755 | mgm.async = true;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | /**
|
---|
| 759 | * @param {any} thing any thing
|
---|
| 760 | * @returns {object} metadata
|
---|
| 761 | */
|
---|
| 762 | getMeta(thing) {
|
---|
| 763 | let meta = this._metaMap.get(thing);
|
---|
| 764 | if (meta === undefined) {
|
---|
| 765 | meta = Object.create(null);
|
---|
| 766 | this._metaMap.set(thing, /** @type {object} */ (meta));
|
---|
| 767 | }
|
---|
| 768 | return /** @type {object} */ (meta);
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | /**
|
---|
| 772 | * @param {any} thing any thing
|
---|
| 773 | * @returns {object | undefined} metadata
|
---|
| 774 | */
|
---|
| 775 | getMetaIfExisting(thing) {
|
---|
| 776 | return this._metaMap.get(thing);
|
---|
| 777 | }
|
---|
| 778 |
|
---|
| 779 | /**
|
---|
| 780 | * @param {string=} cacheStage a persistent stage name for caching
|
---|
| 781 | */
|
---|
| 782 | freeze(cacheStage) {
|
---|
| 783 | this._cache = new WeakTupleMap();
|
---|
| 784 | this._cacheStage = cacheStage;
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | unfreeze() {
|
---|
| 788 | this._cache = undefined;
|
---|
| 789 | this._cacheStage = undefined;
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | /**
|
---|
| 793 | * @template {any[]} T
|
---|
| 794 | * @template V
|
---|
| 795 | * @param {(moduleGraph: ModuleGraph, ...args: T) => V} fn computer
|
---|
| 796 | * @param {T} args arguments
|
---|
| 797 | * @returns {V} computed value or cached
|
---|
| 798 | */
|
---|
| 799 | cached(fn, ...args) {
|
---|
| 800 | if (this._cache === undefined) return fn(this, ...args);
|
---|
| 801 | return this._cache.provide(fn, ...args, () => fn(this, ...args));
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 | /**
|
---|
| 805 | * @param {Map<Module, WeakTupleMap<any, any>>} moduleMemCaches mem caches for modules for better caching
|
---|
| 806 | */
|
---|
| 807 | setModuleMemCaches(moduleMemCaches) {
|
---|
| 808 | this._moduleMemCaches = moduleMemCaches;
|
---|
| 809 | }
|
---|
| 810 |
|
---|
| 811 | /**
|
---|
| 812 | * @param {Dependency} dependency dependency
|
---|
| 813 | * @param {...any} args arguments, last argument is a function called with moduleGraph, dependency, ...args
|
---|
| 814 | * @returns {any} computed value or cached
|
---|
| 815 | */
|
---|
| 816 | dependencyCacheProvide(dependency, ...args) {
|
---|
| 817 | /** @type {(moduleGraph: ModuleGraph, dependency: Dependency, ...args: any[]) => any} */
|
---|
| 818 | const fn = args.pop();
|
---|
| 819 | if (this._moduleMemCaches && this._cacheStage) {
|
---|
| 820 | const memCache = this._moduleMemCaches.get(
|
---|
| 821 | /** @type {Module} */ (this.getParentModule(dependency))
|
---|
| 822 | );
|
---|
| 823 | if (memCache !== undefined) {
|
---|
| 824 | return memCache.provide(dependency, this._cacheStage, ...args, () =>
|
---|
| 825 | fn(this, dependency, ...args)
|
---|
| 826 | );
|
---|
| 827 | }
|
---|
| 828 | }
|
---|
| 829 | if (this._cache === undefined) return fn(this, dependency, ...args);
|
---|
| 830 | return this._cache.provide(dependency, ...args, () =>
|
---|
| 831 | fn(this, dependency, ...args)
|
---|
| 832 | );
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | // TODO remove in webpack 6
|
---|
| 836 | /**
|
---|
| 837 | * @param {Module} module the module
|
---|
| 838 | * @param {string} deprecateMessage message for the deprecation message
|
---|
| 839 | * @param {string} deprecationCode code for the deprecation
|
---|
| 840 | * @returns {ModuleGraph} the module graph
|
---|
| 841 | */
|
---|
| 842 | static getModuleGraphForModule(module, deprecateMessage, deprecationCode) {
|
---|
| 843 | const fn = deprecateMap.get(deprecateMessage);
|
---|
| 844 | if (fn) return fn(module);
|
---|
| 845 | const newFn = util.deprecate(
|
---|
| 846 | /**
|
---|
| 847 | * @param {Module} module the module
|
---|
| 848 | * @returns {ModuleGraph} the module graph
|
---|
| 849 | */
|
---|
| 850 | module => {
|
---|
| 851 | const moduleGraph = moduleGraphForModuleMap.get(module);
|
---|
| 852 | if (!moduleGraph)
|
---|
| 853 | throw new Error(
|
---|
| 854 | `${
|
---|
| 855 | deprecateMessage
|
---|
| 856 | }There was no ModuleGraph assigned to the Module for backward-compat (Use the new API)`
|
---|
| 857 | );
|
---|
| 858 | return moduleGraph;
|
---|
| 859 | },
|
---|
| 860 | `${deprecateMessage}: Use new ModuleGraph API`,
|
---|
| 861 | deprecationCode
|
---|
| 862 | );
|
---|
| 863 | deprecateMap.set(deprecateMessage, newFn);
|
---|
| 864 | return newFn(module);
|
---|
| 865 | }
|
---|
| 866 |
|
---|
| 867 | // TODO remove in webpack 6
|
---|
| 868 | /**
|
---|
| 869 | * @param {Module} module the module
|
---|
| 870 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
| 871 | * @returns {void}
|
---|
| 872 | */
|
---|
| 873 | static setModuleGraphForModule(module, moduleGraph) {
|
---|
| 874 | moduleGraphForModuleMap.set(module, moduleGraph);
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 | // TODO remove in webpack 6
|
---|
| 878 | /**
|
---|
| 879 | * @param {Module} module the module
|
---|
| 880 | * @returns {void}
|
---|
| 881 | */
|
---|
| 882 | static clearModuleGraphForModule(module) {
|
---|
| 883 | moduleGraphForModuleMap.delete(module);
|
---|
| 884 | }
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | // TODO remove in webpack 6
|
---|
| 888 | /** @type {WeakMap<Module, ModuleGraph>} */
|
---|
| 889 | const moduleGraphForModuleMap = new WeakMap();
|
---|
| 890 |
|
---|
| 891 | // TODO remove in webpack 6
|
---|
| 892 | /** @type {Map<string, (module: Module) => ModuleGraph>} */
|
---|
| 893 | const deprecateMap = new Map();
|
---|
| 894 |
|
---|
| 895 | module.exports = ModuleGraph;
|
---|
| 896 | module.exports.ModuleGraphConnection = ModuleGraphConnection;
|
---|