Skip to content

Commit

Permalink
Filter options passed to ResolutionContext, convert to exact type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huntie authored and facebook-github-bot committed Jan 19, 2023
1 parent c438dd0 commit cb01ec0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Type: `Array<string>`

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

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).
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).

#### `requireCycleIgnorePatterns`

Expand Down
6 changes: 3 additions & 3 deletions packages/metro-resolver/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,10 +724,10 @@ describe('resolveRequest', () => {
);
});

it('receives customTransformOptions', () => {
it('receives customResolverOptions', () => {
expect(
Resolver.resolve(
{...context, customTransformOptions: {key: 'value'}},
{...context, customResolverOptions: {key: 'value'}},
'/root/project/foo.js',
'android',
),
Expand All @@ -741,7 +741,7 @@ describe('resolveRequest', () => {
{
...context,
resolveRequest: Resolver.resolve,
customTransformOptions: {key: 'value'},
customResolverOptions: {key: 'value'},
},
'/root/project/foo.js',
'android',
Expand Down
12 changes: 6 additions & 6 deletions packages/metro-resolver/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function resolve(
* `/smth/lib/foobar/index.ios.js`.
*/
function resolveModulePath(
context: ModulePathContext,
context: $ReadOnly<{...ModulePathContext, ...}>,
toModuleName: string,
platform: string | null,
): Resolution {
Expand All @@ -167,7 +167,7 @@ function resolveModulePath(
* a Haste package, it could be `/smth/Foo/index.js`.
*/
function resolveHasteName(
context: HasteContext,
context: $ReadOnly<{...HasteContext, ...}>,
moduleName: string,
platform: string | null,
): Result<Resolution, void> {
Expand Down Expand Up @@ -227,7 +227,7 @@ class MissingFileInHastePackageError extends Error {
* even a package directory.
*/
function resolveFileOrDir(
context: FileOrDirContext,
context: $ReadOnly<{...FileOrDirContext, ...}>,
potentialModulePath: string,
platform: string | null,
): Result<Resolution, FileAndDirCandidates> {
Expand Down Expand Up @@ -255,7 +255,7 @@ function resolveFileOrDir(
* `bar` contains a package which entry point is `./lib/index` (or `./lib`).
*/
function resolveDir(
context: FileOrDirContext,
context: $ReadOnly<{...FileOrDirContext, ...}>,
potentialDirPath: string,
platform: string | null,
): Result<Resolution, FileCandidates> {
Expand All @@ -275,7 +275,7 @@ function resolveDir(
* resolution process altogether.
*/
function resolvePackage(
context: FileOrDirContext,
context: $ReadOnly<{...FileOrDirContext, ...}>,
packageJsonPath: string,
platform: string | null,
): Resolution {
Expand Down Expand Up @@ -308,7 +308,7 @@ function resolvePackage(
* `/js/boop/index.js` (see `_loadAsDir` for that).
*/
function resolveFile(
context: FileContext,
context: $ReadOnly<{...FileContext, ...}>,
dirPath: string,
fileName: string,
platform: string | null,
Expand Down
28 changes: 11 additions & 17 deletions packages/metro-resolver/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ export type ResolveAsset = (
) => ?$ReadOnlyArray<string>;

export type FileContext = $ReadOnly<{
+doesFileExist: DoesFileExist,
+isAssetFile: IsAssetFile,
+nodeModulesPaths: $ReadOnlyArray<string>,
+preferNativePlatform: boolean,
+redirectModulePath: (modulePath: string) => string | false,
+resolveAsset: ResolveAsset,
+sourceExts: $ReadOnlyArray<string>,
...
doesFileExist: DoesFileExist,
isAssetFile: IsAssetFile,
nodeModulesPaths: $ReadOnlyArray<string>,
preferNativePlatform: boolean,
redirectModulePath: (modulePath: string) => string | false,
resolveAsset: ResolveAsset,
sourceExts: $ReadOnlyArray<string>,
}>;

export type FileOrDirContext = $ReadOnly<{
Expand All @@ -84,8 +83,7 @@ export type FileOrDirContext = $ReadOnly<{
* located in `node-haste/Package.js`, and fully duplicated in
* `ModuleGraph/node-haste/Package.js` (!)
*/
+getPackageMainPath: (packageJsonPath: string) => string,
...
getPackageMainPath: (packageJsonPath: string) => string,
}>;

export type HasteContext = $ReadOnly<{
Expand All @@ -94,14 +92,13 @@ export type HasteContext = $ReadOnly<{
* 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,
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,
...
resolveHastePackage: (name: string) => ?string,
}>;

export type ModulePathContext = $ReadOnly<{
Expand All @@ -110,8 +107,7 @@ export type ModulePathContext = $ReadOnly<{
* Full path of the module that is requiring or importing the module to be
* resolved.
*/
+originModulePath: string,
...
originModulePath: string,
}>;

export type ResolutionContext = $ReadOnly<{
Expand All @@ -127,13 +123,11 @@ export type ResolutionContext = $ReadOnly<{
unstable_enablePackageExports: boolean,
resolveRequest?: ?CustomResolver,
customResolverOptions: CustomResolverOptions,
...
}>;

export type CustomResolutionContext = $ReadOnly<{
...ResolutionContext,
resolveRequest: CustomResolver,
...
}>;

export type CustomResolver = (
Expand Down
33 changes: 28 additions & 5 deletions packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,46 @@ class ModuleResolver<TPackage: Packageish> {
platform: string | null,
resolverOptions: ResolverInputOptions,
): BundlerResolution {
const {
disableHierarchicalLookup,
doesFileExist,
extraNodeModules,
isAssetFile,
nodeModulesPaths,
preferNativePlatform,
resolveAsset,
resolveRequest,
sourceExts,
unstable_conditionNames,
unstable_conditionsByPlatform,
unstable_enablePackageExports,
} = this._options;

try {
const result = Resolver.resolve(
{
...this._options,
allowHaste,
disableHierarchicalLookup,
doesFileExist,
extraNodeModules,
isAssetFile,
nodeModulesPaths,
preferNativePlatform,
resolveAsset,
resolveRequest,
sourceExts,
unstable_conditionNames,
unstable_conditionsByPlatform,
unstable_enablePackageExports,
customResolverOptions: resolverOptions.customResolverOptions ?? {},
originModulePath: fromModule.path,
redirectModulePath: (modulePath: string) =>
this._redirectRequire(fromModule, modulePath),
allowHaste,
platform,
resolveHasteModule: (name: string) =>
this._options.getHasteModulePath(name, platform),
resolveHastePackage: (name: string) =>
this._options.getHastePackagePath(name, platform),
getPackageMainPath: this._getPackageMainPath,
unstable_enablePackageExports:
this._options.unstable_enablePackageExports,
},
moduleName,
platform,
Expand Down

0 comments on commit cb01ec0

Please sign in to comment.