Refactor re-order dependencies: remove hybrid pseudo-module

Reviewed By: rafeca

Differential Revision: D7744414

fbshipit-source-id: 36995be791327e792c773482a331f00618cfb4ff
This commit is contained in:
Miguel Jimenez Esun 2018-04-25 05:22:17 -07:00 committed by Facebook Github Bot
parent 884d2ca248
commit 3bf01f272f

View File

@ -335,40 +335,47 @@ function resolveDependencies(
} }
/** /**
* Retraverse the dependency graph in DFS order to reorder the modules and * Re-traverse the dependency graph in DFS order to reorder the modules and
* guarantee the same order between runs. This method mutates the passed graph. * guarantee the same order between runs. This method mutates the passed graph.
*/ */
function reorderGraph(graph: Graph) { function reorderGraph(graph: Graph) {
const parent = { const orderedDependencies = new Map();
dependencies: new Map(graph.entryPoints.map(e => [e, e])),
};
const dependencies = reorderDependencies(parent, graph.dependencies); graph.entryPoints.forEach(entryPoint => {
const mainModule = graph.dependencies.get(entryPoint);
graph.dependencies = dependencies; if (!mainModule) {
throw new ReferenceError('Module not registered in graph: ' + entryPoint);
}
reorderDependencies(graph, mainModule, orderedDependencies);
});
graph.dependencies = orderedDependencies;
} }
function reorderDependencies( function reorderDependencies(
module: {|dependencies: Map<string, string>|} | Module, graph: Graph,
dependencies: Map<string, Module>, module: Module,
orderedDependencies?: Map<string, Module> = new Map(), orderedDependencies: Map<string, Module>,
): Map<string, Module> { ): void {
if (module.path) { if (module.path) {
if (orderedDependencies.has(module.path)) { if (orderedDependencies.has(module.path)) {
return orderedDependencies; return;
} }
orderedDependencies.set(module.path, module); orderedDependencies.set(module.path, module);
} }
module.dependencies.forEach(path => { module.dependencies.forEach(path => {
const dep = dependencies.get(path); const dependency = graph.dependencies.get(path);
if (dep) {
reorderDependencies(dep, dependencies, orderedDependencies);
}
});
return orderedDependencies; if (!dependency) {
throw new ReferenceError('Module not registered in graph: ' + path);
}
reorderDependencies(graph, dependency, orderedDependencies);
});
} }
module.exports = { module.exports = {