-
Notifications
You must be signed in to change notification settings - Fork 600
/
ResolutionRequest.js
119 lines (99 loc) 路 3.15 KB
/
ResolutionRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const path = require('path');
const {AmbiguousModuleResolutionError} = require('metro-core');
const {DuplicateHasteCandidatesError} = require('jest-haste-map').ModuleMap;
const {InvalidPackageError} = require('metro-resolver');
const {PackageResolutionError} = require('metro-core');
import type DependencyGraphHelpers from './DependencyGraphHelpers';
import type {ModuleResolver} from './ModuleResolution';
export type Packageish = {
path: string,
root: string,
redirectRequire(
toModuleName: string,
mainFields: $ReadOnlyArray<string>,
): string | false,
getMain(mainFields: $ReadOnlyArray<string>): string,
};
export type Moduleish = {
+path: string,
getPackage(): ?Packageish,
};
export type ModuleishCache<TModule, TPackage> = {
getPackage(
name: string,
platform?: string,
supportsNativePlatform?: boolean,
): TPackage,
getModule(path: string): TModule,
};
type Options<TModule, TPackage> = {|
+entryPath: string,
+helpers: DependencyGraphHelpers,
+moduleCache: ModuleishCache<TModule, TPackage>,
+moduleResolver: ModuleResolver<TModule, TPackage>,
+platform: string | null,
|};
class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
_immediateResolutionCache: {[key: string]: TModule, __proto__: null};
_options: Options<TModule, TPackage>;
constructor(options: Options<TModule, TPackage>) {
this._options = options;
this._resetResolutionCache();
}
resolveDependency(fromModule: TModule, toModuleName: string): TModule {
const resHash = getResolutionCacheKey(fromModule.path, toModuleName);
const immediateResolution = this._immediateResolutionCache[resHash];
if (immediateResolution) {
return immediateResolution;
}
const cacheResult = result => {
this._immediateResolutionCache[resHash] = result;
return result;
};
const resolver = this._options.moduleResolver;
const platform = this._options.platform;
const allowHaste = !this._options.helpers.isNodeModulesDir(fromModule.path);
try {
return cacheResult(
resolver.resolveDependency(
fromModule,
toModuleName,
allowHaste,
platform,
),
);
} catch (error) {
if (error instanceof DuplicateHasteCandidatesError) {
throw new AmbiguousModuleResolutionError(fromModule.path, error);
}
if (error instanceof InvalidPackageError) {
throw new PackageResolutionError({
packageError: error,
originModulePath: fromModule.path,
targetModuleName: toModuleName,
});
}
throw error;
}
}
_resetResolutionCache() {
this._immediateResolutionCache = Object.create(null);
}
getResolutionCache(): {[key: string]: TModule, __proto__: null} {
return this._immediateResolutionCache;
}
}
function getResolutionCacheKey(modulePath, depName) {
return `${path.resolve(modulePath)}:${depName}`;
}
module.exports = ResolutionRequest;