Skip to content

Commit

Permalink
feat: allow OOT platforms to use custom resolver.resolveRequest (#41697)
Browse files Browse the repository at this point in the history
Summary:
Currently, when we have an additional platform in `react-native.config.js`, users cannot use custom `resolver.resolveRequest` functions as they are overwritten by `reactNativePlatformResolver`. Goal of this PR is to allow OOT platforms to use additional custom resolvers besides remapping react native imports.

## Changelog:

[GENERAL] [FIXED] - Allow Out Of Tree platforms to pass custom resolvers

Pull Request resolved: #41697

Test Plan:
1. Add additional platform in `react-native.config.js`
2. Pass custom resolver to `metro.config.js`:

```js
resolveRequest: (context, moduleName, platform) => {
      console.log('resolveRequest', moduleName, platform);
      return context.resolveRequest(context, moduleName, platform);
 }
```
3. Check if user's `resolveRequest` function is called.

Reviewed By: huntie

Differential Revision: D51659721

Pulled By: robhogan

fbshipit-source-id: 952589b59a6fa34e9406d36c900be53a7c1a79c3
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Nov 29, 2023
1 parent bb075d7 commit 4ccd6e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 13 additions & 10 deletions packages/community-cli-plugin/src/utils/loadMetroConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export type ConfigLoadingContext = $ReadOnly<{
/**
* Get the config options to override based on RN CLI inputs.
*/
function getOverrideConfig(ctx: ConfigLoadingContext): InputConfigT {
function getOverrideConfig(
ctx: ConfigLoadingContext,
config: ConfigT,
): InputConfigT {
const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
platform => ctx.platforms[platform].npmPackageName,
);
Expand All @@ -46,6 +49,7 @@ function getOverrideConfig(ctx: ConfigLoadingContext): InputConfigT {
},
{},
),
config.resolver?.resolveRequest,
);
}

Expand Down Expand Up @@ -79,8 +83,6 @@ export default async function loadMetroConfig(
ctx: ConfigLoadingContext,
options: YargArguments = {},
): Promise<ConfigT> {
const overrideConfig = getOverrideConfig(ctx);

const cwd = ctx.root;
const projectConfig = await resolveConfig(options.config, cwd);

Expand All @@ -105,11 +107,12 @@ This warning will be removed in future (https://github.com/facebook/metro/issues
}
}

return mergeConfig(
await loadConfig({
cwd,
...options,
}),
overrideConfig,
);
const config = await loadConfig({
cwd,
...options,
});

const overrideConfig = getOverrideConfig(ctx, config);

return mergeConfig(config, overrideConfig);
}
12 changes: 9 additions & 3 deletions packages/community-cli-plugin/src/utils/metroPlatformResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import type {CustomResolver} from 'metro-resolver';
* macos: 'react-native-macos'
* }
*/
export function reactNativePlatformResolver(platformImplementations: {
[platform: string]: string,
}): CustomResolver {
export function reactNativePlatformResolver(
platformImplementations: {
[platform: string]: string,
},
customResolver: ?CustomResolver,
): CustomResolver {
return (context, moduleName, platform) => {
let modifiedModuleName = moduleName;
if (platform != null && platformImplementations[platform]) {
Expand All @@ -39,6 +42,9 @@ export function reactNativePlatformResolver(platformImplementations: {
}/${modifiedModuleName.slice('react-native/'.length)}`;
}
}
if (customResolver) {
return customResolver(context, modifiedModuleName, platform);
}
return context.resolveRequest(context, modifiedModuleName, platform);
};
}

0 comments on commit 4ccd6e1

Please sign in to comment.