Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions docs/Resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`
Expand Down Expand Up @@ -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` <div class="label deprecated">Deprecated</div>

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_
Expand All @@ -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` <div class="label deprecated">Deprecated</div>

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<string>`

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).
Expand Down
5 changes: 0 additions & 5 deletions packages/metro-resolver/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, extraPaths: ReadonlyArray<string>);
dirPaths: ReadonlyArray<string>;
Expand Down Expand Up @@ -65,7 +63,6 @@ export type ResolutionContext = Readonly<{
assetExts: ReadonlySet<string>;
customResolverOptions: CustomResolverOptions;
disableHierarchicalLookup: boolean;
doesFileExist: DoesFileExist;
extraNodeModules: null | undefined | {[packageName: string]: string};
dev: boolean;
getPackage: (packageJsonPath: string) => null | undefined | PackageJson;
Expand All @@ -78,7 +75,6 @@ export type ResolutionContext = Readonly<{
nodeModulesPaths: ReadonlyArray<string>;
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;
Expand All @@ -91,7 +87,6 @@ export type ResolutionContext = Readonly<{
}>;
unstable_enablePackageExports: boolean;
unstable_resolveExportsFromPackageRoot: boolean;
unstable_incrementalResolution: boolean;
unstable_logWarning: (message: string) => void;
}>;

Expand Down
4 changes: 3 additions & 1 deletion packages/metro-resolver/src/__tests__/assets-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 3 additions & 3 deletions packages/metro-resolver/src/__tests__/package-exports-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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,
};
Expand Down
8 changes: 0 additions & 8 deletions packages/metro-resolver/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -92,7 +85,6 @@ export function createResolutionContext(
web: ['browser'],
},
unstable_enablePackageExports: false,
unstable_incrementalResolution: false,
unstable_logWarning: () => {},
unstable_resolveExportsFromPackageRoot: false,
...createPackageAccessors(fileMap),
Expand Down
37 changes: 0 additions & 37 deletions packages/metro-resolver/src/createDefaultContext.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/metro-resolver/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type {
CustomResolutionContext,
CustomResolver,
CustomResolverOptions,
DoesFileExist,
FileAndDirCandidates,
FileCandidates,
FileResolution,
Expand Down
3 changes: 2 additions & 1 deletion packages/metro-resolver/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
13 changes: 0 additions & 13 deletions packages/metro-resolver/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -236,7 +225,6 @@ export type ResolutionContext = Readonly<{
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
Expand Down Expand Up @@ -285,7 +273,6 @@ export type ResolutionContext = Readonly<{
* closest to the requested subpath.
*/
unstable_resolveExportsFromPackageRoot: boolean,
unstable_incrementalResolution: boolean,
unstable_logWarning: (message: string) => void,
}>;

Expand Down
69 changes: 30 additions & 39 deletions packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {Reporter} from '../../lib/reporting';
import type {ResolverInputOptions} from '../../shared/types';
import type {
CustomResolver,
DoesFileExist,
FileCandidates,
FileSystemLookup,
Resolution,
Expand All @@ -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';
Expand All @@ -38,7 +36,6 @@ import util from 'node:util';
type Options = Readonly<{
assetExts: ReadonlySet<string>,
disableHierarchicalLookup: boolean,
doesFileExist: DoesFileExist,
emptyModulePath: string,
extraNodeModules: ?any,
fileSystemLookup: FileSystemLookup,
Expand Down Expand Up @@ -112,7 +109,6 @@ export class ModuleResolver {
const {
assetExts,
disableHierarchicalLookup,
doesFileExist,
extraNodeModules,
fileSystemLookup,
getPackage,
Expand All @@ -129,46 +125,41 @@ export class ModuleResolver {
unstable_conditionsByPlatform,
unstable_enablePackageExports,
unstable_resolveExportsFromPackageRoot,
unstable_incrementalResolution,
} = this._options;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit · unstable_incrementalResolution stays in ModuleResolver options with no reader

This destructure was the option's only read, and the PR removes it. Options still requires the field (ModuleResolution.js:63) and createModuleResolver.js:105-106 still passes it, so the value now goes nowhere. Remove both so the resolver stack no longer carries it, which matches the description's point that the resolver doesn't need to see it. The resolver.unstable_incrementalResolution config option and its transform and graph plumbing aren't affected.

Evidence: A search of the head checkout for unstable_incrementalResolution under node-haste/DependencyGraph finds only the type at ModuleResolution.js:63 and the assignment at createModuleResolver.js:105. this._options has no remaining read of it. · id 2d6ffa4f

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deliberate - #1949 reads this._options.unstable_incrementalResolution here, so it stays on the 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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading