2018-07-17 22:41:07 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-07-24 21:30:37 +08:00
|
|
|
const ModuleGraphConnection = require("./ModuleGraphConnection");
|
|
|
|
|
2018-08-07 20:20:53 +08:00
|
|
|
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
|
2018-07-24 21:30:37 +08:00
|
|
|
/** @typedef {import("./Dependency")} Dependency */
|
|
|
|
/** @typedef {import("./Module")} Module */
|
2018-08-16 19:55:41 +08:00
|
|
|
/** @typedef {import("./RequestShortener")} RequestShortener */
|
|
|
|
/** @template T @typedef {import("./util/SortableSet")<T>} SortableSet<T> */
|
|
|
|
|
|
|
|
/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
|
|
|
|
|
|
|
|
class ModuleGraphModule {
|
|
|
|
constructor() {
|
|
|
|
/** @type {Set<ModuleGraphConnection>} */
|
|
|
|
this.incomingConnections = new Set();
|
|
|
|
/** @type {Set<ModuleGraphConnection>} */
|
|
|
|
this.outgoingConnections = new Set();
|
|
|
|
/** @type {Module | null} */
|
|
|
|
this.issuer = null;
|
|
|
|
/** @type {(string | OptimizationBailoutFunction)[]} */
|
|
|
|
this.optimizationBailout = [];
|
|
|
|
/** @type {false | true | SortableSet<string> | null} */
|
|
|
|
this.usedExports = null;
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 21:30:37 +08:00
|
|
|
|
2018-08-16 20:04:37 +08:00
|
|
|
class ModuleGraphDependency {
|
|
|
|
constructor() {
|
|
|
|
/** @type {ModuleGraphConnection} */
|
|
|
|
this.connection = undefined;
|
|
|
|
/** @type {Module} */
|
|
|
|
this.parentModule = undefined;
|
|
|
|
/** @type {DependenciesBlock} */
|
|
|
|
this.parentBlock = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 22:41:07 +08:00
|
|
|
class ModuleGraph {
|
2018-07-24 21:30:37 +08:00
|
|
|
constructor() {
|
2018-08-16 20:04:37 +08:00
|
|
|
/** @type {Map<Dependency, ModuleGraphDependency>} */
|
2018-07-24 21:30:37 +08:00
|
|
|
this._dependencyMap = new Map();
|
2018-08-16 19:55:41 +08:00
|
|
|
/** @type {Map<Module, ModuleGraphModule>} */
|
2018-07-24 21:30:37 +08:00
|
|
|
this._moduleMap = new Map();
|
|
|
|
/** @type {Map<Module, Set<ModuleGraphConnection>>} */
|
|
|
|
this._originMap = new Map();
|
|
|
|
/** @type {Map<any, Object>} */
|
|
|
|
this._metaMap = new Map();
|
|
|
|
}
|
|
|
|
|
2018-08-16 19:55:41 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {ModuleGraphModule} the internal module
|
|
|
|
*/
|
|
|
|
_getModuleGraphModule(module) {
|
|
|
|
let mgm = this._moduleMap.get(module);
|
|
|
|
if (mgm === undefined) {
|
|
|
|
mgm = new ModuleGraphModule();
|
|
|
|
this._moduleMap.set(module, mgm);
|
2018-07-24 21:30:37 +08:00
|
|
|
}
|
2018-08-16 19:55:41 +08:00
|
|
|
return mgm;
|
2018-07-24 21:30:37 +08:00
|
|
|
}
|
|
|
|
|
2018-08-16 20:04:37 +08:00
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency
|
|
|
|
* @returns {ModuleGraphDependency} the internal dependency
|
|
|
|
*/
|
|
|
|
_getModuleGraphDependency(dependency) {
|
|
|
|
let mgd = this._dependencyMap.get(dependency);
|
|
|
|
if (mgd === undefined) {
|
|
|
|
mgd = new ModuleGraphDependency();
|
|
|
|
this._dependencyMap.set(dependency, mgd);
|
|
|
|
}
|
|
|
|
return mgd;
|
|
|
|
}
|
|
|
|
|
2018-08-07 20:20:53 +08:00
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency
|
|
|
|
* @param {DependenciesBlock} block parent block
|
|
|
|
* @param {Module} module parent module
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
setParents(dependency, block, module) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const mgd = this._getModuleGraphDependency(dependency);
|
|
|
|
mgd.parentBlock = block;
|
|
|
|
mgd.parentModule = module;
|
2018-08-07 20:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency
|
|
|
|
* @returns {Module} parent module
|
|
|
|
*/
|
|
|
|
getParentModule(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const mgd = this._getModuleGraphDependency(dependency);
|
|
|
|
return mgd.parentModule;
|
2018-08-07 20:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency
|
|
|
|
* @returns {DependenciesBlock} parent block
|
|
|
|
*/
|
|
|
|
getParentBlock(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const mgd = this._getModuleGraphDependency(dependency);
|
|
|
|
return mgd.parentBlock;
|
2018-08-07 20:20:53 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:30:37 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} originModule the referencing module
|
|
|
|
* @param {Dependency} dependency the referencing dependency
|
|
|
|
* @param {Module} module the referenced module
|
|
|
|
* @returns {void}}
|
|
|
|
*/
|
|
|
|
setResolvedModule(originModule, dependency, module) {
|
|
|
|
const connection = new ModuleGraphConnection(
|
|
|
|
originModule,
|
|
|
|
dependency,
|
|
|
|
module
|
|
|
|
);
|
2018-08-16 20:04:37 +08:00
|
|
|
const mgd = this._getModuleGraphDependency(dependency);
|
|
|
|
mgd.connection = connection;
|
2018-08-16 19:55:41 +08:00
|
|
|
const connections = this._getModuleGraphModule(module).incomingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
connections.add(connection);
|
2018-08-16 19:55:41 +08:00
|
|
|
const originConnections = this._getModuleGraphModule(originModule)
|
|
|
|
.outgoingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
originConnections.add(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the referencing dependency
|
|
|
|
* @param {Module} module the referenced module
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateModule(dependency, module) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
if (connection.module === module) return;
|
2018-08-16 19:55:41 +08:00
|
|
|
const oldMgm = this._getModuleGraphModule(connection.module);
|
|
|
|
oldMgm.incomingConnections.delete(connection);
|
2018-07-24 21:30:37 +08:00
|
|
|
connection.module = module;
|
2018-08-16 19:55:41 +08:00
|
|
|
const newMgm = this._getModuleGraphModule(module);
|
|
|
|
newMgm.incomingConnections.add(connection);
|
2018-07-24 21:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the referencing dependency
|
|
|
|
* @param {string} explanation an explanation
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
addExplanation(dependency, explanation) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
connection.addExplanation(explanation);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} oldModule the old referencing module
|
|
|
|
* @param {Module} newModule the new referencing module
|
2018-07-27 21:57:44 +08:00
|
|
|
* @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
|
2018-07-24 21:30:37 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-07-27 21:57:44 +08:00
|
|
|
replaceModule(oldModule, newModule, filterConnection) {
|
2018-07-24 21:30:37 +08:00
|
|
|
if (oldModule === newModule) return;
|
2018-08-16 19:55:41 +08:00
|
|
|
const oldMgm = this._getModuleGraphModule(oldModule);
|
|
|
|
const newMgm = this._getModuleGraphModule(newModule);
|
|
|
|
const oldConnections = oldMgm.outgoingConnections;
|
|
|
|
const newConnections = newMgm.outgoingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
for (const connection of oldConnections) {
|
2018-07-27 21:57:44 +08:00
|
|
|
if (filterConnection(connection)) {
|
|
|
|
connection.originModule = newModule;
|
|
|
|
newConnections.add(connection);
|
|
|
|
oldConnections.delete(connection);
|
|
|
|
}
|
2018-07-24 21:30:37 +08:00
|
|
|
}
|
2018-08-16 19:55:41 +08:00
|
|
|
const oldConnections2 = oldMgm.incomingConnections;
|
|
|
|
const newConnections2 = newMgm.incomingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
for (const connection of oldConnections2) {
|
2018-07-27 21:57:44 +08:00
|
|
|
if (filterConnection(connection)) {
|
|
|
|
connection.module = newModule;
|
|
|
|
newConnections2.add(connection);
|
|
|
|
oldConnections2.delete(connection);
|
|
|
|
}
|
2018-07-24 21:30:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the referenced module
|
|
|
|
* @param {string} explanation an explanation why it's referenced
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
addExtraReason(module, explanation) {
|
2018-08-16 19:55:41 +08:00
|
|
|
const connections = this._getModuleGraphModule(module).incomingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
connections.add(new ModuleGraphConnection(null, null, module, explanation));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency to look for a referenced module
|
|
|
|
* @returns {Module} the referenced module
|
|
|
|
*/
|
|
|
|
getResolvedModule(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
return connection !== undefined ? connection.resolvedModule : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency to look for a referenced module
|
|
|
|
* @returns {ModuleGraphConnection | undefined} the connection
|
|
|
|
*/
|
|
|
|
getConnection(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency to look for a referenced module
|
|
|
|
* @returns {Module} the referenced module
|
|
|
|
*/
|
|
|
|
getModule(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
return connection !== undefined ? connection.module : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency to look for a referencing module
|
|
|
|
* @returns {Module} the referencing module
|
|
|
|
*/
|
|
|
|
getOrigin(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
return connection !== undefined ? connection.originModule : null;
|
|
|
|
}
|
|
|
|
|
2018-08-07 20:20:53 +08:00
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency to look for a referencing module
|
|
|
|
* @returns {Module} the original referencing module
|
|
|
|
*/
|
|
|
|
getResolvedOrigin(dependency) {
|
2018-08-16 20:04:37 +08:00
|
|
|
const { connection } = this._getModuleGraphDependency(dependency);
|
2018-08-07 20:20:53 +08:00
|
|
|
return connection !== undefined ? connection.resolvedOriginModule : null;
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:30:37 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {ModuleGraphConnection[]} reasons why a module is included
|
|
|
|
*/
|
|
|
|
getIncomingConnections(module) {
|
2018-08-16 19:55:41 +08:00
|
|
|
const connections = this._getModuleGraphModule(module).incomingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
return Array.from(connections);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {ModuleGraphConnection[]} list of outgoing connections
|
|
|
|
*/
|
|
|
|
getOutgoingConnection(module) {
|
2018-08-16 19:55:41 +08:00
|
|
|
const connections = this._getModuleGraphModule(module).outgoingConnections;
|
2018-07-24 21:30:37 +08:00
|
|
|
return Array.from(connections);
|
|
|
|
}
|
|
|
|
|
2018-08-16 19:55:41 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {Module | null} the issuer module
|
|
|
|
*/
|
|
|
|
getIssuer(module) {
|
|
|
|
const mgm = this._getModuleGraphModule(module);
|
|
|
|
return mgm.issuer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @param {Module | null} issuer the issuer module
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
setIssuer(module, issuer) {
|
|
|
|
const mgm = this._getModuleGraphModule(module);
|
|
|
|
mgm.issuer = issuer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {(string | OptimizationBailoutFunction)[]} optimization bailouts
|
|
|
|
*/
|
|
|
|
getOptimizationBailout(module) {
|
|
|
|
const mgm = this._getModuleGraphModule(module);
|
|
|
|
return mgm.optimizationBailout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {false | true | SortableSet<string> | null} the used exports
|
|
|
|
* false: module is not used at all.
|
|
|
|
* true: the module namespace/object export is used.
|
|
|
|
* SortableSet<string>: these export names are used.
|
|
|
|
* empty SortableSet<string>: module is used but no export.
|
|
|
|
* null: unknown, worst case should be assumed.
|
|
|
|
*/
|
|
|
|
getUsedExports(module) {
|
|
|
|
const mgm = this._getModuleGraphModule(module);
|
|
|
|
return mgm.usedExports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @param {false | true | SortableSet<string>} usedExports the used exports
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
setUsedExports(module, usedExports) {
|
|
|
|
const mgm = this._getModuleGraphModule(module);
|
|
|
|
mgm.usedExports = usedExports;
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:30:37 +08:00
|
|
|
/**
|
|
|
|
* @param {any} thing any thing
|
|
|
|
* @returns {Object} metadata
|
|
|
|
*/
|
|
|
|
getMeta(thing) {
|
|
|
|
let meta = this._metaMap.get(thing);
|
|
|
|
if (meta === undefined) {
|
|
|
|
meta = Object.create(null);
|
|
|
|
this._metaMap.set(thing, meta);
|
|
|
|
}
|
|
|
|
return meta;
|
|
|
|
}
|
2018-07-17 22:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ModuleGraph;
|
2018-07-24 21:30:37 +08:00
|
|
|
module.exports.ModuleGraphConnection = ModuleGraphConnection;
|