2016-07-29 18:00:08 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2017-05-12 17:26:48 +00:00
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
* @format
|
2016-07-29 18:00:08 +00:00
|
|
|
*/
|
2017-05-12 17:26:48 +00:00
|
|
|
|
2016-07-29 18:00:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-12 17:26:48 +00:00
|
|
|
import type ResolutionResponse from '../DependencyGraph/ResolutionResponse';
|
|
|
|
|
|
|
|
function resolveModuleRequires<TModule: {hash(): string}, TOptions>(
|
|
|
|
resolutionResponse: ResolutionResponse<TModule, TOptions>,
|
|
|
|
module: TModule,
|
2017-05-17 16:21:25 +00:00
|
|
|
): Array<TModule> {
|
2016-07-29 18:00:08 +00:00
|
|
|
const pairs = resolutionResponse.getResolvedDependencyPairs(module);
|
2017-05-12 17:26:48 +00:00
|
|
|
return pairs ? pairs.map(([, dependencyModule]) => dependencyModule) : [];
|
2016-07-29 18:00:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 17:26:48 +00:00
|
|
|
function getModuleDependents<TModule>(
|
|
|
|
cache: Map<TModule, Set<TModule>>,
|
|
|
|
module: TModule,
|
|
|
|
): Set<TModule> {
|
2016-07-29 18:00:08 +00:00
|
|
|
let dependents = cache.get(module);
|
|
|
|
if (!dependents) {
|
|
|
|
dependents = new Set();
|
|
|
|
cache.set(module, dependents);
|
|
|
|
}
|
|
|
|
return dependents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object that indicates in which module each module is required.
|
|
|
|
*/
|
2017-05-12 17:26:48 +00:00
|
|
|
function getInverseDependencies<TModule: {hash(): string}, TOptions>(
|
|
|
|
resolutionResponse: ResolutionResponse<TModule, TOptions>,
|
|
|
|
): Map<TModule, Set<TModule>> {
|
2016-07-29 18:00:08 +00:00
|
|
|
const cache = new Map();
|
|
|
|
|
|
|
|
resolutionResponse.dependencies.forEach(module => {
|
|
|
|
resolveModuleRequires(resolutionResponse, module).forEach(dependency => {
|
|
|
|
getModuleDependents(cache, dependency).add(module);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getInverseDependencies;
|