From cb01ec09c6a8df51f39d6b1419cbf173fa5abf4a Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 19 Jan 2023 14:14:10 -0800 Subject: [PATCH] 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 --- docs/Configuration.md | 2 +- .../src/__tests__/index-test.js | 6 ++-- packages/metro-resolver/src/resolve.js | 12 +++---- packages/metro-resolver/src/types.js | 28 +++++++--------- .../DependencyGraph/ModuleResolution.js | 33 ++++++++++++++++--- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index cba9cbb3a7..14bd5dcadf 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -271,7 +271,7 @@ Type: `Array` 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` diff --git a/packages/metro-resolver/src/__tests__/index-test.js b/packages/metro-resolver/src/__tests__/index-test.js index 4e484bb571..1df292703a 100644 --- a/packages/metro-resolver/src/__tests__/index-test.js +++ b/packages/metro-resolver/src/__tests__/index-test.js @@ -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', ), @@ -741,7 +741,7 @@ describe('resolveRequest', () => { { ...context, resolveRequest: Resolver.resolve, - customTransformOptions: {key: 'value'}, + customResolverOptions: {key: 'value'}, }, '/root/project/foo.js', 'android', diff --git a/packages/metro-resolver/src/resolve.js b/packages/metro-resolver/src/resolve.js index 6de2011cc2..496d2d13c6 100644 --- a/packages/metro-resolver/src/resolve.js +++ b/packages/metro-resolver/src/resolve.js @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -275,7 +275,7 @@ function resolveDir( * resolution process altogether. */ function resolvePackage( - context: FileOrDirContext, + context: $ReadOnly<{...FileOrDirContext, ...}>, packageJsonPath: string, platform: string | null, ): Resolution { @@ -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, diff --git a/packages/metro-resolver/src/types.js b/packages/metro-resolver/src/types.js index f2196a4862..0478a6e90e 100644 --- a/packages/metro-resolver/src/types.js +++ b/packages/metro-resolver/src/types.js @@ -63,14 +63,13 @@ export type ResolveAsset = ( ) => ?$ReadOnlyArray; export type FileContext = $ReadOnly<{ - +doesFileExist: DoesFileExist, - +isAssetFile: IsAssetFile, - +nodeModulesPaths: $ReadOnlyArray, - +preferNativePlatform: boolean, - +redirectModulePath: (modulePath: string) => string | false, - +resolveAsset: ResolveAsset, - +sourceExts: $ReadOnlyArray, - ... + doesFileExist: DoesFileExist, + isAssetFile: IsAssetFile, + nodeModulesPaths: $ReadOnlyArray, + preferNativePlatform: boolean, + redirectModulePath: (modulePath: string) => string | false, + resolveAsset: ResolveAsset, + sourceExts: $ReadOnlyArray, }>; export type FileOrDirContext = $ReadOnly<{ @@ -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<{ @@ -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<{ @@ -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<{ @@ -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 = ( diff --git a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js index 24641a4321..ca00c72977 100644 --- a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js +++ b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js @@ -177,23 +177,46 @@ class ModuleResolver { 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,