Skip to content

Commit 3829549

Browse files
okwasniewskiOthinn
authored andcommitted
feat: allow OOT platforms to use custom resolver.resolveRequest (facebook#41697)
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: facebook#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
1 parent 9484171 commit 3829549

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

packages/community-cli-plugin/src/utils/loadMetroConfig.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export type ConfigLoadingContext = $ReadOnly<{
2929
/**
3030
* Get the config options to override based on RN CLI inputs.
3131
*/
32-
function getOverrideConfig(ctx: ConfigLoadingContext): InputConfigT {
32+
function getOverrideConfig(
33+
ctx: ConfigLoadingContext,
34+
config: ConfigT,
35+
): InputConfigT {
3336
const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
3437
platform => ctx.platforms[platform].npmPackageName,
3538
);
@@ -46,6 +49,7 @@ function getOverrideConfig(ctx: ConfigLoadingContext): InputConfigT {
4649
},
4750
{},
4851
),
52+
config.resolver?.resolveRequest,
4953
);
5054
}
5155

@@ -79,8 +83,6 @@ export default async function loadMetroConfig(
7983
ctx: ConfigLoadingContext,
8084
options: YargArguments = {},
8185
): Promise<ConfigT> {
82-
const overrideConfig = getOverrideConfig(ctx);
83-
8486
const cwd = ctx.root;
8587
const projectConfig = await resolveConfig(options.config, cwd);
8688

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

108-
return mergeConfig(
109-
await loadConfig({
110-
cwd,
111-
...options,
112-
}),
113-
overrideConfig,
114-
);
110+
const config = await loadConfig({
111+
cwd,
112+
...options,
113+
});
114+
115+
const overrideConfig = getOverrideConfig(ctx, config);
116+
117+
return mergeConfig(config, overrideConfig);
115118
}

packages/community-cli-plugin/src/utils/metroPlatformResolver.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ import type {CustomResolver} from 'metro-resolver';
2525
* macos: 'react-native-macos'
2626
* }
2727
*/
28-
export function reactNativePlatformResolver(platformImplementations: {
29-
[platform: string]: string,
30-
}): CustomResolver {
28+
export function reactNativePlatformResolver(
29+
platformImplementations: {
30+
[platform: string]: string,
31+
},
32+
customResolver: ?CustomResolver,
33+
): CustomResolver {
3134
return (context, moduleName, platform) => {
3235
let modifiedModuleName = moduleName;
3336
if (platform != null && platformImplementations[platform]) {
@@ -39,6 +42,9 @@ export function reactNativePlatformResolver(platformImplementations: {
3942
}/${modifiedModuleName.slice('react-native/'.length)}`;
4043
}
4144
}
45+
if (customResolver) {
46+
return customResolver(context, modifiedModuleName, platform);
47+
}
4248
return context.resolveRequest(context, modifiedModuleName, platform);
4349
};
4450
}

0 commit comments

Comments
 (0)