forked from facebook/metro
/
types.js
157 lines (133 loc) Β· 4.46 KB
/
types.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
* @oncall react_native
*/
'use strict';
export type Result<+TResolution, +TCandidates> =
| {+type: 'resolved', +resolution: TResolution}
| {+type: 'failed', +candidates: TCandidates};
export type Resolution = FileResolution | {+type: 'empty'};
export type AssetFileResolution = $ReadOnlyArray<string>;
export type FileResolution =
| {+type: 'sourceFile', +filePath: string}
| {+type: 'assetFiles', +filePaths: AssetFileResolution};
export type FileAndDirCandidates = {
+dir: FileCandidates,
+file: FileCandidates,
};
/**
* This is a way to describe what files we tried to look for when resolving
* a module name as file. This is mainly used for error reporting, so that
* we can explain why we cannot resolve a module.
*/
export type FileCandidates =
// We only tried to resolve a specific asset.
| {+type: 'asset', +name: string}
// We attempted to resolve a name as being a source file (ex. JavaScript,
// JSON...), in which case there can be several extensions we tried, for
// example `/js/foo.ios.js`, `/js/foo.js`, etc. for a single prefix '/js/foo'.
| {
+type: 'sourceFile',
filePathPrefix: string,
+candidateExts: $ReadOnlyArray<string>,
};
export type ExportMap = $ReadOnly<{
[subpathOrCondition: string]: ExportMap | string | null,
}>;
export type PackageJson = $ReadOnly<{
name?: string,
main?: string,
exports?: string | ExportMap,
...
}>;
export type PackageInfo = $ReadOnly<{
packageJson: PackageJson,
rootPath: string,
}>;
/**
* Check existence of a single file.
*/
export type DoesFileExist = (filePath: string) => boolean;
export type IsAssetFile = (fileName: string) => boolean;
export type RealPath = (path: string) => ?string;
/**
* Given a directory path and the base asset name, return a list of all the
* asset file names that match the given base name in that directory. Return
* null if there's no such named asset. `platform` is used to identify
* platform-specific assets, ex. `foo.ios.js` instead of a generic `foo.js`.
*/
export type ResolveAsset = (
dirPath: string,
assetName: string,
extension: string,
) => ?$ReadOnlyArray<string>;
export type ResolutionContext = $ReadOnly<{
allowHaste: boolean,
customResolverOptions: CustomResolverOptions,
disableHierarchicalLookup: boolean,
doesFileExist: DoesFileExist,
extraNodeModules: ?{[string]: string, ...},
/**
* Get the parsed contents of the specified `package.json` file.
*/
getPackage: (packageJsonPath: string) => ?PackageJson,
/**
* Get the package information and parsed `package.json` file for for a given
* module path, if it is contained within an npm package.
*/
getPackageForModule: (modulePath: string) => ?PackageInfo,
isAssetFile: IsAssetFile,
/**
* The ordered list of fields to read in `package.json` to resolve a main
* entry point based on the "browser" field spec.
*/
mainFields: $ReadOnlyArray<string>,
/**
* Full path of the module that is requiring or importing the module to be
* resolved.
*/
originModulePath: string,
nodeModulesPaths: $ReadOnlyArray<string>,
preferNativePlatform: boolean,
resolveAsset: ResolveAsset,
redirectModulePath: (modulePath: string) => string | false,
/**
* Given a name, this should return the full path to the file that provides
* a Haste module of that name. Ex. for `Foo` it may return `/smth/Foo.js`.
*/
resolveHasteModule: (name: string) => ?string,
/**
* Given a name, this should return the full path to the package manifest that
* provides a Haste package of that name. Ex. for `Foo` it may return
* `/smth/Foo/package.json`.
*/
resolveHastePackage: (name: string) => ?string,
resolveRequest?: ?CustomResolver,
sourceExts: $ReadOnlyArray<string>,
unstable_conditionNames: $ReadOnlyArray<string>,
unstable_conditionsByPlatform: $ReadOnly<{
[platform: string]: $ReadOnlyArray<string>,
}>,
unstable_enablePackageExports: boolean,
unstable_realPath?: ?RealPath,
}>;
export type CustomResolutionContext = $ReadOnly<{
...ResolutionContext,
resolveRequest: CustomResolver,
}>;
export type CustomResolver = (
context: CustomResolutionContext,
moduleName: string,
platform: string | null,
) => Resolution;
export type CustomResolverOptions = {
__proto__: null,
+[string]: mixed,
...
};