Skip to content

Commit cb01ec0

Browse files
huntiefacebook-github-bot
authored andcommitted
Filter options passed to ResolutionContext, convert to exact type
Summary: Update the context object passed to metro-resolver and custom resolvers to match the keys typed for metro-resolver and in our docs. The keys which are no longer exposed are: - `dirExists` - `emptyModulePath` - `mainFields` (will be re-added) - `moduleCache` - `platform` - `projectRoot` This is a breaking change that is summarised in the next commit. Reviewed By: motiz88 Differential Revision: D42546071 fbshipit-source-id: e50355da2edff864848a15c5aad3a98985e93bb6
1 parent c438dd0 commit cb01ec0

File tree

5 files changed

+49
-32
lines changed

5 files changed

+49
-32
lines changed

docs/Configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ Type: `Array<string>`
271271

272272
Additional platforms to resolve. Defaults to `['ios', 'android', 'windows', 'web']`.
273273

274-
For more information, see [Module Resolution](https://facebook.github.io/metro/docs/resolution) and [React Native's documentation for platform-specific extensions](https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions).
274+
For more information, see [Module Resolution](./Resolution.md) and [React Native's documentation for platform-specific extensions](https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions).
275275

276276
#### `requireCycleIgnorePatterns`
277277

packages/metro-resolver/src/__tests__/index-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,10 +724,10 @@ describe('resolveRequest', () => {
724724
);
725725
});
726726

727-
it('receives customTransformOptions', () => {
727+
it('receives customResolverOptions', () => {
728728
expect(
729729
Resolver.resolve(
730-
{...context, customTransformOptions: {key: 'value'}},
730+
{...context, customResolverOptions: {key: 'value'}},
731731
'/root/project/foo.js',
732732
'android',
733733
),
@@ -741,7 +741,7 @@ describe('resolveRequest', () => {
741741
{
742742
...context,
743743
resolveRequest: Resolver.resolve,
744-
customTransformOptions: {key: 'value'},
744+
customResolverOptions: {key: 'value'},
745745
},
746746
'/root/project/foo.js',
747747
'android',

packages/metro-resolver/src/resolve.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function resolve(
143143
* `/smth/lib/foobar/index.ios.js`.
144144
*/
145145
function resolveModulePath(
146-
context: ModulePathContext,
146+
context: $ReadOnly<{...ModulePathContext, ...}>,
147147
toModuleName: string,
148148
platform: string | null,
149149
): Resolution {
@@ -167,7 +167,7 @@ function resolveModulePath(
167167
* a Haste package, it could be `/smth/Foo/index.js`.
168168
*/
169169
function resolveHasteName(
170-
context: HasteContext,
170+
context: $ReadOnly<{...HasteContext, ...}>,
171171
moduleName: string,
172172
platform: string | null,
173173
): Result<Resolution, void> {
@@ -227,7 +227,7 @@ class MissingFileInHastePackageError extends Error {
227227
* even a package directory.
228228
*/
229229
function resolveFileOrDir(
230-
context: FileOrDirContext,
230+
context: $ReadOnly<{...FileOrDirContext, ...}>,
231231
potentialModulePath: string,
232232
platform: string | null,
233233
): Result<Resolution, FileAndDirCandidates> {
@@ -255,7 +255,7 @@ function resolveFileOrDir(
255255
* `bar` contains a package which entry point is `./lib/index` (or `./lib`).
256256
*/
257257
function resolveDir(
258-
context: FileOrDirContext,
258+
context: $ReadOnly<{...FileOrDirContext, ...}>,
259259
potentialDirPath: string,
260260
platform: string | null,
261261
): Result<Resolution, FileCandidates> {
@@ -275,7 +275,7 @@ function resolveDir(
275275
* resolution process altogether.
276276
*/
277277
function resolvePackage(
278-
context: FileOrDirContext,
278+
context: $ReadOnly<{...FileOrDirContext, ...}>,
279279
packageJsonPath: string,
280280
platform: string | null,
281281
): Resolution {
@@ -308,7 +308,7 @@ function resolvePackage(
308308
* `/js/boop/index.js` (see `_loadAsDir` for that).
309309
*/
310310
function resolveFile(
311-
context: FileContext,
311+
context: $ReadOnly<{...FileContext, ...}>,
312312
dirPath: string,
313313
fileName: string,
314314
platform: string | null,

packages/metro-resolver/src/types.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,13 @@ export type ResolveAsset = (
6363
) => ?$ReadOnlyArray<string>;
6464

6565
export type FileContext = $ReadOnly<{
66-
+doesFileExist: DoesFileExist,
67-
+isAssetFile: IsAssetFile,
68-
+nodeModulesPaths: $ReadOnlyArray<string>,
69-
+preferNativePlatform: boolean,
70-
+redirectModulePath: (modulePath: string) => string | false,
71-
+resolveAsset: ResolveAsset,
72-
+sourceExts: $ReadOnlyArray<string>,
73-
...
66+
doesFileExist: DoesFileExist,
67+
isAssetFile: IsAssetFile,
68+
nodeModulesPaths: $ReadOnlyArray<string>,
69+
preferNativePlatform: boolean,
70+
redirectModulePath: (modulePath: string) => string | false,
71+
resolveAsset: ResolveAsset,
72+
sourceExts: $ReadOnlyArray<string>,
7473
}>;
7574

7675
export type FileOrDirContext = $ReadOnly<{
@@ -84,8 +83,7 @@ export type FileOrDirContext = $ReadOnly<{
8483
* located in `node-haste/Package.js`, and fully duplicated in
8584
* `ModuleGraph/node-haste/Package.js` (!)
8685
*/
87-
+getPackageMainPath: (packageJsonPath: string) => string,
88-
...
86+
getPackageMainPath: (packageJsonPath: string) => string,
8987
}>;
9088

9189
export type HasteContext = $ReadOnly<{
@@ -94,14 +92,13 @@ export type HasteContext = $ReadOnly<{
9492
* Given a name, this should return the full path to the file that provides
9593
* a Haste module of that name. Ex. for `Foo` it may return `/smth/Foo.js`.
9694
*/
97-
+resolveHasteModule: (name: string) => ?string,
95+
resolveHasteModule: (name: string) => ?string,
9896
/**
9997
* Given a name, this should return the full path to the package manifest that
10098
* provides a Haste package of that name. Ex. for `Foo` it may return
10199
* `/smth/Foo/package.json`.
102100
*/
103-
+resolveHastePackage: (name: string) => ?string,
104-
...
101+
resolveHastePackage: (name: string) => ?string,
105102
}>;
106103

107104
export type ModulePathContext = $ReadOnly<{
@@ -110,8 +107,7 @@ export type ModulePathContext = $ReadOnly<{
110107
* Full path of the module that is requiring or importing the module to be
111108
* resolved.
112109
*/
113-
+originModulePath: string,
114-
...
110+
originModulePath: string,
115111
}>;
116112

117113
export type ResolutionContext = $ReadOnly<{
@@ -127,13 +123,11 @@ export type ResolutionContext = $ReadOnly<{
127123
unstable_enablePackageExports: boolean,
128124
resolveRequest?: ?CustomResolver,
129125
customResolverOptions: CustomResolverOptions,
130-
...
131126
}>;
132127

133128
export type CustomResolutionContext = $ReadOnly<{
134129
...ResolutionContext,
135130
resolveRequest: CustomResolver,
136-
...
137131
}>;
138132

139133
export type CustomResolver = (

packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,46 @@ class ModuleResolver<TPackage: Packageish> {
177177
platform: string | null,
178178
resolverOptions: ResolverInputOptions,
179179
): BundlerResolution {
180+
const {
181+
disableHierarchicalLookup,
182+
doesFileExist,
183+
extraNodeModules,
184+
isAssetFile,
185+
nodeModulesPaths,
186+
preferNativePlatform,
187+
resolveAsset,
188+
resolveRequest,
189+
sourceExts,
190+
unstable_conditionNames,
191+
unstable_conditionsByPlatform,
192+
unstable_enablePackageExports,
193+
} = this._options;
194+
180195
try {
181196
const result = Resolver.resolve(
182197
{
183-
...this._options,
198+
allowHaste,
199+
disableHierarchicalLookup,
200+
doesFileExist,
201+
extraNodeModules,
202+
isAssetFile,
203+
nodeModulesPaths,
204+
preferNativePlatform,
205+
resolveAsset,
206+
resolveRequest,
207+
sourceExts,
208+
unstable_conditionNames,
209+
unstable_conditionsByPlatform,
210+
unstable_enablePackageExports,
184211
customResolverOptions: resolverOptions.customResolverOptions ?? {},
185212
originModulePath: fromModule.path,
186213
redirectModulePath: (modulePath: string) =>
187214
this._redirectRequire(fromModule, modulePath),
188-
allowHaste,
189-
platform,
190215
resolveHasteModule: (name: string) =>
191216
this._options.getHasteModulePath(name, platform),
192217
resolveHastePackage: (name: string) =>
193218
this._options.getHastePackagePath(name, platform),
194219
getPackageMainPath: this._getPackageMainPath,
195-
unstable_enablePackageExports:
196-
this._options.unstable_enablePackageExports,
197220
},
198221
moduleName,
199222
platform,

0 commit comments

Comments
 (0)