diff --git a/docs/Resolution.md b/docs/Resolution.md
index d7aadcb4a5..22e6e47c01 100644
--- a/docs/Resolution.md
+++ b/docs/Resolution.md
@@ -163,9 +163,9 @@ Applies [platform-specific extensions](https://reactnative.dev/docs/platform-spe
1. If the path refers to an [asset](#assetexts-readonlysetstring), then
1. Return [**RESOLVE_ASSET**](#resolve_asset)(_context_, _filePath_, _platform_).
-2. Otherwise, if the path [exists](#doesfileexist-string--boolean-deprecated) as a file, return a [source file resolution](#source-file) for it.
+2. Otherwise, if the path [exists](#filesystemlookup-string--exists-true-type-fd-realpath-string--exists-false) as a file, return a [source file resolution](#source-file) for it.
3. Otherwise:
- 1. Try all platform and extension variants in sequence. Return a [source file resolution](#source-file) for the first that [exists](#doesfileexist-string--boolean-deprecated) after applying [**BROWSER_SPEC_REDIRECTION**](#browser_spec_redirection). For example, if _platform_ is `android` and [`context.sourceExts`](#sourceexts-readonlyarraystring) is `['js', 'jsx']`, try:
+ 1. Try all platform and extension variants in sequence. Return a [source file resolution](#source-file) for the first that [exists](#filesystemlookup-string--exists-true-type-fd-realpath-string--exists-false) after applying [**BROWSER_SPEC_REDIRECTION**](#browser_spec_redirection). For example, if _platform_ is `android` and [`context.sourceExts`](#sourceexts-readonlyarraystring) is `['js', 'jsx']`, try:
1. _moduleName_ + `'.android.js'`
2. _moduleName_ + `'.native.js'` (if [`context.preferNativePlatform`](#prefernativeplatform-boolean) is `true`)
3. _moduleName_ + `'.js'`
@@ -230,10 +230,6 @@ The set of file extensions used to identify asset files. Defaults to [`resolver.
`true` for a development bundle, `false` otherwise.
-#### `doesFileExist: string => boolean`
Deprecated
-
-Returns whether the file at the given path exists. The default implementation wraps [`fileSystemLookup`](#filesystemlookup-string--exists-true-type-fd-realpath-string--exists-false); prefer using that directly.
-
#### `fileSystemLookup: string => {exists: true, type: 'f'|'d', realPath: string} | {exists: false}`
_Added in v0.81.0_
@@ -250,10 +246,6 @@ Paths to check for modules after looking through all `node_modules` directories.
If `true`, try `.native.${ext}` after `.${platform}.${ext}` and before `.${ext}`. Metro sets this to `true`, which is why it picks `Foo.native.js` when there is no `Foo.ios.js` (or other `Foo.${platform}.js`).
-#### `redirectModulePath: string => string | false` Deprecated
-
-Defaults to [**BROWSER_SPEC_REDIRECTION**](#browser_spec_redirection), which Metro's default resolver uses directly instead of calling this function. It is exposed for backwards-compatible use by custom resolvers, and will be removed in a future release.
-
#### `resolveAsset: (dirPath: string, assetName: string, extension: string) => ?$ReadOnlyArray`
Returns all asset files in `dirPath` matching `assetName` and `extension`, or `null` if there are none. The default implementation considers `${assetName}${extension}` and, for each of [`resolver.assetResolutions`](./Configuration.md#assetresolutions), `${assetName}@${resolution}x${extension}`. For example, `resolveAsset('/app/img', 'logo', '.png')` may return `['/app/img/logo.png', '/app/img/logo@2x.png', '/app/img/logo@3x.png']`. See also [Static Image Resources](https://reactnative.dev/docs/images#static-image-resources).
diff --git a/packages/metro-resolver/API.md b/packages/metro-resolver/API.md
index efd9739f0d..e5949819b1 100644
--- a/packages/metro-resolver/API.md
+++ b/packages/metro-resolver/API.md
@@ -14,8 +14,6 @@ export type CustomResolverOptions = {
readonly [key: string]: unknown;
};
-export type DoesFileExist = (filePath: string) => boolean;
-
export class FailedToResolveNameError extends Error {
constructor(dirPaths: ReadonlyArray, extraPaths: ReadonlyArray);
dirPaths: ReadonlyArray;
@@ -65,7 +63,6 @@ export type ResolutionContext = Readonly<{
assetExts: ReadonlySet;
customResolverOptions: CustomResolverOptions;
disableHierarchicalLookup: boolean;
- doesFileExist: DoesFileExist;
extraNodeModules: null | undefined | {[packageName: string]: string};
dev: boolean;
getPackage: (packageJsonPath: string) => null | undefined | PackageJson;
@@ -78,7 +75,6 @@ export type ResolutionContext = Readonly<{
nodeModulesPaths: ReadonlyArray;
preferNativePlatform: boolean;
resolveAsset: ResolveAsset;
- redirectModulePath: (modulePath: string) => string | false;
resolveHasteModule: (name: string) => null | undefined | string;
resolveHastePackage: (name: string) => null | undefined | string;
resolveRequest?: null | undefined | CustomResolver;
@@ -91,7 +87,6 @@ export type ResolutionContext = Readonly<{
}>;
unstable_enablePackageExports: boolean;
unstable_resolveExportsFromPackageRoot: boolean;
- unstable_incrementalResolution: boolean;
unstable_logWarning: (message: string) => void;
}>;
diff --git a/packages/metro-resolver/src/__tests__/assets-test.js b/packages/metro-resolver/src/__tests__/assets-test.js
index f31f502e6a..b67375df89 100644
--- a/packages/metro-resolver/src/__tests__/assets-test.js
+++ b/packages/metro-resolver/src/__tests__/assets-test.js
@@ -38,7 +38,9 @@ describe('asset resolutions', () => {
),
];
- assets = assets.filter(candidate => baseContext.doesFileExist(candidate));
+ assets = assets.filter(
+ candidate => baseContext.fileSystemLookup(candidate).exists,
+ );
return assets.length ? assets : null;
};
diff --git a/packages/metro-resolver/src/__tests__/package-exports-test.js b/packages/metro-resolver/src/__tests__/package-exports-test.js
index 2a12308e78..c4037ff191 100644
--- a/packages/metro-resolver/src/__tests__/package-exports-test.js
+++ b/packages/metro-resolver/src/__tests__/package-exports-test.js
@@ -1138,7 +1138,7 @@ describe('with package exports resolution enabled', () => {
...assetResolutions.map(
resolution => basePath + '@' + resolution + 'x' + extension,
),
- ].filter(candidate => baseContext.doesFileExist(candidate));
+ ].filter(candidate => baseContext.fileSystemLookup(candidate).exists);
return assets.length ? assets : null;
},
@@ -1178,8 +1178,8 @@ describe('with package exports resolution enabled', () => {
},
}),
resolveAsset: (dirPath: string, basename: string, extension: string) =>
- [dirPath + path.sep + basename + extension].filter(candidate =>
- baseContext.doesFileExist(candidate),
+ [dirPath + path.sep + basename + extension].filter(
+ candidate => baseContext.fileSystemLookup(candidate).exists,
),
unstable_applyBrowserSpecAfterExports: true,
};
diff --git a/packages/metro-resolver/src/__tests__/utils.js b/packages/metro-resolver/src/__tests__/utils.js
index 5615d24472..c58a7b35e5 100644
--- a/packages/metro-resolver/src/__tests__/utils.js
+++ b/packages/metro-resolver/src/__tests__/utils.js
@@ -47,12 +47,6 @@ export function createResolutionContext(
assetExts: new Set(['jpg', 'png']),
customResolverOptions: {},
disableHierarchicalLookup: false,
- doesFileExist: (filePath: string) =>
- // Should return false unless realpath(filePath) exists. We mock shallow
- // dereferencing.
- fileMap[filePath] != null &&
- (typeof fileMap[filePath] === 'string' ||
- typeof fileMap[filePath].realPath === 'string'),
extraNodeModules: null,
fileSystemLookup: inputPath => {
// Normalise and remove any trailing slash.
@@ -80,7 +74,6 @@ export function createResolutionContext(
mainFields: ['browser', 'main'],
nodeModulesPaths: [],
preferNativePlatform: false,
- redirectModulePath: (filePath: string) => filePath,
resolveAsset: (filePath: string) => null,
resolveHasteModule: (name: string) => null,
resolveHastePackage: (name: string) => null,
@@ -92,7 +85,6 @@ export function createResolutionContext(
web: ['browser'],
},
unstable_enablePackageExports: false,
- unstable_incrementalResolution: false,
unstable_logWarning: () => {},
unstable_resolveExportsFromPackageRoot: false,
...createPackageAccessors(fileMap),
diff --git a/packages/metro-resolver/src/createDefaultContext.js b/packages/metro-resolver/src/createDefaultContext.js
deleted file mode 100644
index c16e3c26bf..0000000000
--- a/packages/metro-resolver/src/createDefaultContext.js
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * 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-local
- * @format
- * @oncall react_native
- */
-
-import type {ResolutionContext} from './types';
-import type {TransformResultDependency} from 'metro/private/DeltaBundler/types';
-
-import {redirectModulePath} from './PackageResolve';
-
-type PartialContext = Readonly<{
- ...ResolutionContext,
- redirectModulePath?: ResolutionContext['redirectModulePath'],
-}>;
-
-/**
- * Helper used by the `metro` package to create the `ResolutionContext` object.
- * As context values can be overridden by callers, this occurs externally to
- * `resolve.js`.
- */
-export default function createDefaultContext(
- context: PartialContext,
- dependency: TransformResultDependency,
-): ResolutionContext {
- return {
- redirectModulePath: (modulePath: string) =>
- redirectModulePath(context, modulePath),
- dependency,
- ...context,
- };
-}
diff --git a/packages/metro-resolver/src/index.js b/packages/metro-resolver/src/index.js
index 13d239509e..28f9b6fbb9 100644
--- a/packages/metro-resolver/src/index.js
+++ b/packages/metro-resolver/src/index.js
@@ -14,7 +14,6 @@ export type {
CustomResolutionContext,
CustomResolver,
CustomResolverOptions,
- DoesFileExist,
FileAndDirCandidates,
FileCandidates,
FileResolution,
diff --git a/packages/metro-resolver/src/resolve.js b/packages/metro-resolver/src/resolve.js
index 9121ff9572..f60e1b8ce3 100644
--- a/packages/metro-resolver/src/resolve.js
+++ b/packages/metro-resolver/src/resolve.js
@@ -901,7 +901,8 @@ function resolvePackageEntryPoint(
const packageJsonPath = path.join(packagePath, 'package.json');
- if (!context.doesFileExist(packageJsonPath)) {
+ const packageJsonLookup = context.fileSystemLookup(packageJsonPath);
+ if (!packageJsonLookup.exists || packageJsonLookup.type !== 'f') {
return resolveFile(context, packagePath, 'index', platform);
}
diff --git a/packages/metro-resolver/src/types.js b/packages/metro-resolver/src/types.js
index 825e205078..933cedd0f9 100644
--- a/packages/metro-resolver/src/types.js
+++ b/packages/metro-resolver/src/types.js
@@ -126,11 +126,6 @@ export type PackageForModule = Readonly<{
packageRelativePath: string,
}>;
-/**
- * Check existence of a single file.
- */
-export type DoesFileExist = (filePath: string) => boolean;
-
/**
* Performs a lookup against an absolute or project-relative path to determine
* whether it exists as a file or directory. Follows any symlinks, and returns
@@ -158,12 +153,6 @@ export type ResolutionContext = Readonly<{
customResolverOptions: CustomResolverOptions,
disableHierarchicalLookup: boolean,
- /**
- * Determine whether a regular file exists at the given path.
- *
- * @deprecated, prefer `fileSystemLookup`
- */
- doesFileExist: DoesFileExist,
extraNodeModules: ?{[packageName: string]: string, ...},
/** Is resolving for a development bundle. */
@@ -236,7 +225,6 @@ export type ResolutionContext = Readonly<{
nodeModulesPaths: ReadonlyArray,
preferNativePlatform: boolean,
resolveAsset: ResolveAsset,
- redirectModulePath: (modulePath: string) => string | false,
/**
* Given a name, this should return the full path to the file that provides
@@ -285,7 +273,6 @@ export type ResolutionContext = Readonly<{
* closest to the requested subpath.
*/
unstable_resolveExportsFromPackageRoot: boolean,
- unstable_incrementalResolution: boolean,
unstable_logWarning: (message: string) => void,
}>;
diff --git a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js
index 7d211750df..56c8d6e6b6 100644
--- a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js
+++ b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js
@@ -17,7 +17,6 @@ import type {Reporter} from '../../lib/reporting';
import type {ResolverInputOptions} from '../../shared/types';
import type {
CustomResolver,
- DoesFileExist,
FileCandidates,
FileSystemLookup,
Resolution,
@@ -29,7 +28,6 @@ import {deriveVirtualModulePath} from '../../lib/modulePath';
import {codeFrameColumns} from '@babel/code-frame';
import invariant from 'invariant';
import * as Resolver from 'metro-resolver';
-import createDefaultContext from 'metro-resolver/private/createDefaultContext';
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
@@ -38,7 +36,6 @@ import util from 'node:util';
type Options = Readonly<{
assetExts: ReadonlySet,
disableHierarchicalLookup: boolean,
- doesFileExist: DoesFileExist,
emptyModulePath: string,
extraNodeModules: ?any,
fileSystemLookup: FileSystemLookup,
@@ -112,7 +109,6 @@ export class ModuleResolver {
const {
assetExts,
disableHierarchicalLookup,
- doesFileExist,
extraNodeModules,
fileSystemLookup,
getPackage,
@@ -129,46 +125,41 @@ export class ModuleResolver {
unstable_conditionsByPlatform,
unstable_enablePackageExports,
unstable_resolveExportsFromPackageRoot,
- unstable_incrementalResolution,
} = this._options;
try {
const result = Resolver.resolve(
- createDefaultContext(
- {
- allowHaste,
- assetExts,
- customResolverOptions: resolverOptions.customResolverOptions ?? {},
- dev: resolverOptions.dev,
- disableHierarchicalLookup,
- doesFileExist,
- extraNodeModules,
- fileSystemLookup,
- getPackage,
- getPackageForModule,
- isESMImport: dependency.data.isESMImport,
- mainFields,
- nodeModulesPaths,
- originModulePath,
- preferNativePlatform,
- resolveAsset,
- resolveHasteModule: (name: string) =>
- this._options.getHasteModulePath(name, platform),
- resolveHastePackage: (name: string) =>
- this._options.getHastePackagePath(name, platform),
- resolveRequest,
- schemeResolvers,
- sourceExts,
- unstable_applyBrowserSpecAfterExports,
- unstable_conditionNames,
- unstable_conditionsByPlatform,
- unstable_enablePackageExports,
- unstable_resolveExportsFromPackageRoot,
- unstable_incrementalResolution,
- unstable_logWarning: this._logWarning,
- },
+ {
+ allowHaste,
+ assetExts,
+ customResolverOptions: resolverOptions.customResolverOptions ?? {},
dependency,
- ),
+ dev: resolverOptions.dev,
+ disableHierarchicalLookup,
+ extraNodeModules,
+ fileSystemLookup,
+ getPackage,
+ getPackageForModule,
+ isESMImport: dependency.data.isESMImport,
+ mainFields,
+ nodeModulesPaths,
+ originModulePath,
+ preferNativePlatform,
+ resolveAsset,
+ resolveHasteModule: (name: string) =>
+ this._options.getHasteModulePath(name, platform),
+ resolveHastePackage: (name: string) =>
+ this._options.getHastePackagePath(name, platform),
+ resolveRequest,
+ schemeResolvers,
+ sourceExts,
+ unstable_applyBrowserSpecAfterExports,
+ unstable_conditionNames,
+ unstable_conditionsByPlatform,
+ unstable_enablePackageExports,
+ unstable_resolveExportsFromPackageRoot,
+ unstable_logWarning: this._logWarning,
+ },
dependency.name,
platform,
);
diff --git a/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js b/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js
index 0b7536566e..1d521aaf1f 100644
--- a/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js
+++ b/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js
@@ -46,7 +46,6 @@ export default function createModuleResolver({
return new ModuleResolver({
assetExts: new Set(config.resolver.assetExts),
disableHierarchicalLookup: config.resolver.disableHierarchicalLookup,
- doesFileExist: (filePath: string) => fileSystem.exists(filePath),
emptyModulePath: config.resolver.emptyModulePath,
extraNodeModules: config.resolver.extraNodeModules,
fileSystemLookup,