mirror of
https://github.com/status-im/metro.git
synced 2025-01-29 12:24:47 +00:00
react-native: attachHMRServer: make it generic
Reviewed By: cpojer Differential Revision: D5172344 fbshipit-source-id: ab8b39e1924d66d37da9734455ed9a72cf59906e
This commit is contained in:
parent
de70d376ca
commit
3b705a1124
@ -67,7 +67,7 @@ class HMRBundle extends BundleBase {
|
|||||||
return (Promise.resolve(): any);
|
return (Promise.resolve(): any);
|
||||||
}
|
}
|
||||||
|
|
||||||
getModulesIdsAndCode() {
|
getModulesIdsAndCode(): Array<{id: string, code: string}> {
|
||||||
return this.__modules.map(module => {
|
return this.__modules.map(module => {
|
||||||
return {
|
return {
|
||||||
id: JSON.stringify(module.id),
|
id: JSON.stringify(module.id),
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
jest.dontMock('../getInverseDependencies');
|
|
||||||
|
|
||||||
const getInverseDependencies = require('../getInverseDependencies');
|
|
||||||
|
|
||||||
describe('getInverseDependencies', () => {
|
|
||||||
it('', () => {
|
|
||||||
const module1 = createModule('module1', ['module2', 'module3']);
|
|
||||||
const module2 = createModule('module2', ['module3', 'module4']);
|
|
||||||
const module3 = createModule('module3', ['module4']);
|
|
||||||
const module4 = createModule('module4', []);
|
|
||||||
|
|
||||||
const modulePairs = {
|
|
||||||
'module1': [['module2', module2], ['module3', module3]],
|
|
||||||
'module2': [['module3', module3], ['module4', module4]],
|
|
||||||
'module3': [['module4', module4]],
|
|
||||||
'module4': [],
|
|
||||||
};
|
|
||||||
|
|
||||||
const resolutionResponse = {
|
|
||||||
dependencies: [module1, module2, module3, module4],
|
|
||||||
getResolvedDependencyPairs: module => {
|
|
||||||
return modulePairs[module.hash()];
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const dependencies = getInverseDependencies(resolutionResponse);
|
|
||||||
const actual = // jest can't compare maps and sets
|
|
||||||
Array.from(dependencies.entries())
|
|
||||||
.map(([key, value]) => [key, Array.from(value)]);
|
|
||||||
|
|
||||||
expect(actual).toEqual([
|
|
||||||
[module2, [module1]],
|
|
||||||
[module3, [module1, module2]],
|
|
||||||
[module4, [module2, module3]],
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
function createModule(name, dependencies) {
|
|
||||||
return {
|
|
||||||
hash: () => name,
|
|
||||||
getName: () => Promise.resolve(name),
|
|
||||||
getDependencies: () => Promise.resolve(dependencies),
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* @flow
|
|
||||||
* @format
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
import type ResolutionResponse from '../DependencyGraph/ResolutionResponse';
|
|
||||||
|
|
||||||
function resolveModuleRequires<TModule: {hash(): string}, TOptions>(
|
|
||||||
resolutionResponse: ResolutionResponse<TModule, TOptions>,
|
|
||||||
module: TModule,
|
|
||||||
): Array<TModule> {
|
|
||||||
const pairs = resolutionResponse.getResolvedDependencyPairs(module);
|
|
||||||
return pairs ? pairs.map(([, dependencyModule]) => dependencyModule) : [];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getModuleDependents<TModule>(
|
|
||||||
cache: Map<TModule, Set<TModule>>,
|
|
||||||
module: TModule,
|
|
||||||
): Set<TModule> {
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
function getInverseDependencies<TModule: {hash(): string}, TOptions>(
|
|
||||||
resolutionResponse: ResolutionResponse<TModule, TOptions>,
|
|
||||||
): Map<TModule, Set<TModule>> {
|
|
||||||
const cache = new Map();
|
|
||||||
|
|
||||||
resolutionResponse.dependencies.forEach(module => {
|
|
||||||
resolveModuleRequires(resolutionResponse, module).forEach(dependency => {
|
|
||||||
getModuleDependents(cache, dependency).add(module);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = getInverseDependencies;
|
|
Loading…
x
Reference in New Issue
Block a user