123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895 |
- "use strict";
- const util = require("util");
- const ExportsInfo = require("./ExportsInfo");
- const ModuleGraphConnection = require("./ModuleGraphConnection");
- const SortableSet = require("./util/SortableSet");
- const WeakTupleMap = require("./util/WeakTupleMap");
- const EMPTY_SET = new Set();
- const getConnectionsByOriginModule = set => {
- const map = new Map();
-
- let lastModule = 0;
-
- let lastList = undefined;
- for (const connection of set) {
- const { originModule } = connection;
- if (lastModule === originModule) {
-
- (lastList).push(connection);
- } else {
- lastModule = (originModule);
- const list = map.get(originModule);
- if (list !== undefined) {
- lastList = list;
- list.push(connection);
- } else {
- const list = [connection];
- lastList = list;
- map.set(originModule, list);
- }
- }
- }
- return map;
- };
- const getConnectionsByModule = set => {
- const map = new Map();
-
- let lastModule = 0;
-
- let lastList = undefined;
- for (const connection of set) {
- const { module } = connection;
- if (lastModule === module) {
-
- (lastList).push(connection);
- } else {
- lastModule = module;
- const list = map.get(module);
- if (list !== undefined) {
- lastList = list;
- list.push(connection);
- } else {
- const list = [connection];
- lastList = list;
- map.set(module, list);
- }
- }
- }
- return map;
- };
- class ModuleGraphModule {
- constructor() {
-
- this.incomingConnections = new SortableSet();
-
- this.outgoingConnections = undefined;
-
- this.issuer = undefined;
-
- this.optimizationBailout = [];
-
- this.exports = new ExportsInfo();
-
- this.preOrderIndex = null;
-
- this.postOrderIndex = null;
-
- this.depth = null;
-
- this.profile = undefined;
-
- this.async = false;
-
- this._unassignedConnections = undefined;
- }
- }
- class ModuleGraph {
- constructor() {
-
- this._dependencyMap = new WeakMap();
-
- this._moduleMap = new Map();
-
- this._metaMap = new WeakMap();
-
- this._cache = undefined;
-
- this._moduleMemCaches = undefined;
-
- this._cacheStage = undefined;
- }
-
- _getModuleGraphModule(module) {
- let mgm = this._moduleMap.get(module);
- if (mgm === undefined) {
- mgm = new ModuleGraphModule();
- this._moduleMap.set(module, mgm);
- }
- return mgm;
- }
-
- setParents(dependency, block, module, indexInBlock = -1) {
- dependency._parentDependenciesBlockIndex = indexInBlock;
- dependency._parentDependenciesBlock = block;
- dependency._parentModule = module;
- }
-
- getParentModule(dependency) {
- return dependency._parentModule;
- }
-
- getParentBlock(dependency) {
- return dependency._parentDependenciesBlock;
- }
-
- getParentBlockIndex(dependency) {
- return dependency._parentDependenciesBlockIndex;
- }
-
- setResolvedModule(originModule, dependency, module) {
- const connection = new ModuleGraphConnection(
- originModule,
- dependency,
- module,
- undefined,
- dependency.weak,
- dependency.getCondition(this)
- );
- const connections = this._getModuleGraphModule(module).incomingConnections;
- connections.add(connection);
- if (originModule) {
- const mgm = this._getModuleGraphModule(originModule);
- if (mgm._unassignedConnections === undefined) {
- mgm._unassignedConnections = [];
- }
- mgm._unassignedConnections.push(connection);
- if (mgm.outgoingConnections === undefined) {
- mgm.outgoingConnections = new SortableSet();
- }
- mgm.outgoingConnections.add(connection);
- } else {
- this._dependencyMap.set(dependency, connection);
- }
- }
-
- updateModule(dependency, module) {
- const connection =
-
- (this.getConnection(dependency));
- if (connection.module === module) return;
- const newConnection = connection.clone();
- newConnection.module = module;
- this._dependencyMap.set(dependency, newConnection);
- connection.setActive(false);
- const originMgm = this._getModuleGraphModule(
- (connection.originModule)
- );
-
- (originMgm.outgoingConnections).add(newConnection);
- const targetMgm = this._getModuleGraphModule(module);
- targetMgm.incomingConnections.add(newConnection);
- }
-
- removeConnection(dependency) {
- const connection =
-
- (this.getConnection(dependency));
- const targetMgm = this._getModuleGraphModule(connection.module);
- targetMgm.incomingConnections.delete(connection);
- const originMgm = this._getModuleGraphModule(
- (connection.originModule)
- );
-
- (originMgm.outgoingConnections).delete(connection);
- this._dependencyMap.set(dependency, null);
- }
-
- addExplanation(dependency, explanation) {
- const connection =
-
- (this.getConnection(dependency));
- connection.addExplanation(explanation);
- }
-
- cloneModuleAttributes(sourceModule, targetModule) {
- const oldMgm = this._getModuleGraphModule(sourceModule);
- const newMgm = this._getModuleGraphModule(targetModule);
- newMgm.postOrderIndex = oldMgm.postOrderIndex;
- newMgm.preOrderIndex = oldMgm.preOrderIndex;
- newMgm.depth = oldMgm.depth;
- newMgm.exports = oldMgm.exports;
- newMgm.async = oldMgm.async;
- }
-
- removeModuleAttributes(module) {
- const mgm = this._getModuleGraphModule(module);
- mgm.postOrderIndex = null;
- mgm.preOrderIndex = null;
- mgm.depth = null;
- mgm.async = false;
- }
-
- removeAllModuleAttributes() {
- for (const mgm of this._moduleMap.values()) {
- mgm.postOrderIndex = null;
- mgm.preOrderIndex = null;
- mgm.depth = null;
- mgm.async = false;
- }
- }
-
- moveModuleConnections(oldModule, newModule, filterConnection) {
- if (oldModule === newModule) return;
- const oldMgm = this._getModuleGraphModule(oldModule);
- const newMgm = this._getModuleGraphModule(newModule);
-
- const oldConnections = oldMgm.outgoingConnections;
- if (oldConnections !== undefined) {
- if (newMgm.outgoingConnections === undefined) {
- newMgm.outgoingConnections = new SortableSet();
- }
- const newConnections = newMgm.outgoingConnections;
- for (const connection of oldConnections) {
- if (filterConnection(connection)) {
- connection.originModule = newModule;
- newConnections.add(connection);
- oldConnections.delete(connection);
- }
- }
- }
-
- const oldConnections2 = oldMgm.incomingConnections;
- const newConnections2 = newMgm.incomingConnections;
- for (const connection of oldConnections2) {
- if (filterConnection(connection)) {
- connection.module = newModule;
- newConnections2.add(connection);
- oldConnections2.delete(connection);
- }
- }
- }
-
- copyOutgoingModuleConnections(oldModule, newModule, filterConnection) {
- if (oldModule === newModule) return;
- const oldMgm = this._getModuleGraphModule(oldModule);
- const newMgm = this._getModuleGraphModule(newModule);
-
- const oldConnections = oldMgm.outgoingConnections;
- if (oldConnections !== undefined) {
- if (newMgm.outgoingConnections === undefined) {
- newMgm.outgoingConnections = new SortableSet();
- }
- const newConnections = newMgm.outgoingConnections;
- for (const connection of oldConnections) {
- if (filterConnection(connection)) {
- const newConnection = connection.clone();
- newConnection.originModule = newModule;
- newConnections.add(newConnection);
- if (newConnection.module !== undefined) {
- const otherMgm = this._getModuleGraphModule(newConnection.module);
- otherMgm.incomingConnections.add(newConnection);
- }
- }
- }
- }
- }
-
- addExtraReason(module, explanation) {
- const connections = this._getModuleGraphModule(module).incomingConnections;
- connections.add(new ModuleGraphConnection(null, null, module, explanation));
- }
-
- getResolvedModule(dependency) {
- const connection = this.getConnection(dependency);
- return connection !== undefined ? connection.resolvedModule : null;
- }
-
- getConnection(dependency) {
- const connection = this._dependencyMap.get(dependency);
- if (connection === undefined) {
- const module = this.getParentModule(dependency);
- if (module !== undefined) {
- const mgm = this._getModuleGraphModule(module);
- if (
- mgm._unassignedConnections &&
- mgm._unassignedConnections.length !== 0
- ) {
- let foundConnection;
- for (const connection of mgm._unassignedConnections) {
- this._dependencyMap.set(
- (connection.dependency),
- connection
- );
- if (connection.dependency === dependency)
- foundConnection = connection;
- }
- mgm._unassignedConnections.length = 0;
- if (foundConnection !== undefined) {
- return foundConnection;
- }
- }
- }
- this._dependencyMap.set(dependency, null);
- return undefined;
- }
- return connection === null ? undefined : connection;
- }
-
- getModule(dependency) {
- const connection = this.getConnection(dependency);
- return connection !== undefined ? connection.module : null;
- }
-
- getOrigin(dependency) {
- const connection = this.getConnection(dependency);
- return connection !== undefined ? connection.originModule : null;
- }
-
- getResolvedOrigin(dependency) {
- const connection = this.getConnection(dependency);
- return connection !== undefined ? connection.resolvedOriginModule : null;
- }
-
- getIncomingConnections(module) {
- const connections = this._getModuleGraphModule(module).incomingConnections;
- return connections;
- }
-
- getOutgoingConnections(module) {
- const connections = this._getModuleGraphModule(module).outgoingConnections;
- return connections === undefined ? EMPTY_SET : connections;
- }
-
- getIncomingConnectionsByOriginModule(module) {
- const connections = this._getModuleGraphModule(module).incomingConnections;
- return connections.getFromUnorderedCache(getConnectionsByOriginModule);
- }
-
- getOutgoingConnectionsByModule(module) {
- const connections = this._getModuleGraphModule(module).outgoingConnections;
- return connections === undefined
- ? undefined
- : connections.getFromUnorderedCache(getConnectionsByModule);
- }
-
- getProfile(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.profile;
- }
-
- setProfile(module, profile) {
- const mgm = this._getModuleGraphModule(module);
- mgm.profile = profile;
- }
-
- getIssuer(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.issuer;
- }
-
- setIssuer(module, issuer) {
- const mgm = this._getModuleGraphModule(module);
- mgm.issuer = issuer;
- }
-
- setIssuerIfUnset(module, issuer) {
- const mgm = this._getModuleGraphModule(module);
- if (mgm.issuer === undefined) mgm.issuer = issuer;
- }
-
- getOptimizationBailout(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.optimizationBailout;
- }
-
- getProvidedExports(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.exports.getProvidedExports();
- }
-
- isExportProvided(module, exportName) {
- const mgm = this._getModuleGraphModule(module);
- const result = mgm.exports.isExportProvided(exportName);
- return result === undefined ? null : result;
- }
-
- getExportsInfo(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.exports;
- }
-
- getExportInfo(module, exportName) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.exports.getExportInfo(exportName);
- }
-
- getReadOnlyExportInfo(module, exportName) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.exports.getReadOnlyExportInfo(exportName);
- }
-
- getUsedExports(module, runtime) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.exports.getUsedExports(runtime);
- }
-
- getPreOrderIndex(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.preOrderIndex;
- }
-
- getPostOrderIndex(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.postOrderIndex;
- }
-
- setPreOrderIndex(module, index) {
- const mgm = this._getModuleGraphModule(module);
- mgm.preOrderIndex = index;
- }
-
- setPreOrderIndexIfUnset(module, index) {
- const mgm = this._getModuleGraphModule(module);
- if (mgm.preOrderIndex === null) {
- mgm.preOrderIndex = index;
- return true;
- }
- return false;
- }
-
- setPostOrderIndex(module, index) {
- const mgm = this._getModuleGraphModule(module);
- mgm.postOrderIndex = index;
- }
-
- setPostOrderIndexIfUnset(module, index) {
- const mgm = this._getModuleGraphModule(module);
- if (mgm.postOrderIndex === null) {
- mgm.postOrderIndex = index;
- return true;
- }
- return false;
- }
-
- getDepth(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.depth;
- }
-
- setDepth(module, depth) {
- const mgm = this._getModuleGraphModule(module);
- mgm.depth = depth;
- }
-
- setDepthIfLower(module, depth) {
- const mgm = this._getModuleGraphModule(module);
- if (mgm.depth === null || mgm.depth > depth) {
- mgm.depth = depth;
- return true;
- }
- return false;
- }
-
- isAsync(module) {
- const mgm = this._getModuleGraphModule(module);
- return mgm.async;
- }
-
- setAsync(module) {
- const mgm = this._getModuleGraphModule(module);
- mgm.async = true;
- }
-
- getMeta(thing) {
- let meta = this._metaMap.get(thing);
- if (meta === undefined) {
- meta = Object.create(null);
- this._metaMap.set(thing, (meta));
- }
- return (meta);
- }
-
- getMetaIfExisting(thing) {
- return this._metaMap.get(thing);
- }
-
- freeze(cacheStage) {
- this._cache = new WeakTupleMap();
- this._cacheStage = cacheStage;
- }
- unfreeze() {
- this._cache = undefined;
- this._cacheStage = undefined;
- }
-
- cached(fn, ...args) {
- if (this._cache === undefined) return fn(this, ...args);
- return this._cache.provide(fn, ...args, () => fn(this, ...args));
- }
-
- setModuleMemCaches(moduleMemCaches) {
- this._moduleMemCaches = moduleMemCaches;
- }
-
- dependencyCacheProvide(dependency, ...args) {
-
- const fn = args.pop();
- if (this._moduleMemCaches && this._cacheStage) {
- const memCache = this._moduleMemCaches.get(
- (this.getParentModule(dependency))
- );
- if (memCache !== undefined) {
- return memCache.provide(dependency, this._cacheStage, ...args, () =>
- fn(this, dependency, ...args)
- );
- }
- }
- if (this._cache === undefined) return fn(this, dependency, ...args);
- return this._cache.provide(dependency, ...args, () =>
- fn(this, dependency, ...args)
- );
- }
-
-
- static getModuleGraphForModule(module, deprecateMessage, deprecationCode) {
- const fn = deprecateMap.get(deprecateMessage);
- if (fn) return fn(module);
- const newFn = util.deprecate(
-
- module => {
- const moduleGraph = moduleGraphForModuleMap.get(module);
- if (!moduleGraph)
- throw new Error(
- deprecateMessage +
- "There was no ModuleGraph assigned to the Module for backward-compat (Use the new API)"
- );
- return moduleGraph;
- },
- deprecateMessage + ": Use new ModuleGraph API",
- deprecationCode
- );
- deprecateMap.set(deprecateMessage, newFn);
- return newFn(module);
- }
-
-
- static setModuleGraphForModule(module, moduleGraph) {
- moduleGraphForModuleMap.set(module, moduleGraph);
- }
-
-
- static clearModuleGraphForModule(module) {
- moduleGraphForModuleMap.delete(module);
- }
- }
- const moduleGraphForModuleMap = new WeakMap();
- const deprecateMap = new Map();
- module.exports = ModuleGraph;
- module.exports.ModuleGraphConnection = ModuleGraphConnection;
|