mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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: https://github.com/facebook/react-native/pull/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
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
/**
|
|
* 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 {CustomResolver} from 'metro-resolver';
|
|
|
|
/**
|
|
* This is an implementation of a metro resolveRequest option which will remap react-native imports
|
|
* to different npm packages based on the platform requested. This allows a single metro instance/config
|
|
* to produce bundles for multiple out of tree platforms at a time.
|
|
*
|
|
* @param platformImplementations
|
|
* A map of platform to npm package that implements that platform
|
|
*
|
|
* Ex:
|
|
* {
|
|
* windows: 'react-native-windows'
|
|
* macos: 'react-native-macos'
|
|
* }
|
|
*/
|
|
export function reactNativePlatformResolver(
|
|
platformImplementations: {
|
|
[platform: string]: string,
|
|
},
|
|
customResolver: ?CustomResolver,
|
|
): CustomResolver {
|
|
return (context, moduleName, platform) => {
|
|
let modifiedModuleName = moduleName;
|
|
if (platform != null && platformImplementations[platform]) {
|
|
if (moduleName === 'react-native') {
|
|
modifiedModuleName = platformImplementations[platform];
|
|
} else if (moduleName.startsWith('react-native/')) {
|
|
modifiedModuleName = `${
|
|
platformImplementations[platform]
|
|
}/${modifiedModuleName.slice('react-native/'.length)}`;
|
|
}
|
|
}
|
|
if (customResolver) {
|
|
return customResolver(context, modifiedModuleName, platform);
|
|
}
|
|
return context.resolveRequest(context, modifiedModuleName, platform);
|
|
};
|
|
}
|