diff --git a/packages/out-of-tree-platforms/package.json b/packages/out-of-tree-platforms/package.json index 648226808be25f..b37d2b561ab1cd 100644 --- a/packages/out-of-tree-platforms/package.json +++ b/packages/out-of-tree-platforms/package.json @@ -12,7 +12,7 @@ }, "license": "MIT", "exports": { - ".": "./dist/index.js", + ".": "./src/index.js", "./package.json": "./package.json" }, "files": [ diff --git a/packages/out-of-tree-platforms/src/getPlatformResolver.js b/packages/out-of-tree-platforms/src/getPlatformResolver.js new file mode 100644 index 00000000000000..70e3acde5c1d78 --- /dev/null +++ b/packages/out-of-tree-platforms/src/getPlatformResolver.js @@ -0,0 +1,43 @@ +/** + * 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 + * @format + */ + +import type {CustomResolver} from 'metro-resolver'; + +type ResolverConfig = { + platformNameMap: {[platform: string]: string}, +}; + +/** + * Creates a custom Metro resolver that maps platform extensions to package names. + * To be used in app's `metro.config.js` as `resolver.resolveRequest`. + */ +export const getPlatformResolver = (config: ResolverConfig): CustomResolver => { + return (context, moduleName, platform) => { + // `customResolverOptions` is populated through `?resolver.platformExtension` query params + // in the jsBundleURLForBundleRoot method of the react-native/React/Base/RCTBundleURLProvider.mm + const platformExtension = context.customResolverOptions?.platformExtension; + let modifiedModuleName = moduleName; + if ( + typeof platformExtension === 'string' && + config.platformNameMap?.[platformExtension] + ) { + const packageName = config.platformNameMap[platformExtension]; + if (moduleName === 'react-native') { + modifiedModuleName = packageName; + } else if (moduleName.startsWith('react-native/')) { + modifiedModuleName = `${packageName}/${modifiedModuleName.slice( + 'react-native/'.length, + )}`; + } + } + + return context.resolveRequest(context, modifiedModuleName, platform); + }; +}; diff --git a/packages/out-of-tree-platforms/src/index.js b/packages/out-of-tree-platforms/src/index.js index a88b678821283b..2244b1ce77d29e 100644 --- a/packages/out-of-tree-platforms/src/index.js +++ b/packages/out-of-tree-platforms/src/index.js @@ -1,44 +1,5 @@ -/** - * 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 - * @format - */ +if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { + require('../../../scripts/build/babel-register').registerForMonorepo(); +} -import type {CustomResolver} from 'metro-resolver'; - -type ResolverConfig = { - platformNameMap: {[platform: string]: string}, -}; - -/** - * Creates a custom Metro resolver that maps platform extensions to package names. - * To be used in app's `metro.config.js` as `resolver.resolveRequest`. - */ -export const getPlatformResolver = (config: ResolverConfig): CustomResolver => { - return (context, moduleName, platform) => { - // `customResolverOptions` is populated through `?resolver.platformExtension` query params - // in the jsBundleURLForBundleRoot method of the react-native/React/Base/RCTBundleURLProvider.mm - const platformExtension = context.customResolverOptions?.platformExtension; - let modifiedModuleName = moduleName; - - if ( - typeof platformExtension === 'string' && - config.platformNameMap?.[platformExtension] - ) { - const packageName = config.platformNameMap[platformExtension]; - if (moduleName === 'react-native') { - modifiedModuleName = packageName; - } else if (moduleName.startsWith('react-native/')) { - modifiedModuleName = `${packageName}/${modifiedModuleName.slice( - 'react-native/'.length, - )}`; - } - } - - return context.resolveRequest(context, modifiedModuleName, platform); - }; -}; +export * from './getPlatformResolver'; diff --git a/packages/rn-tester/package.json b/packages/rn-tester/package.json index e40163d7742843..4967d2551cf21a 100644 --- a/packages/rn-tester/package.json +++ b/packages/rn-tester/package.json @@ -25,7 +25,8 @@ "dependencies": { "flow-enums-runtime": "^0.0.6", "invariant": "^2.2.4", - "nullthrows": "^1.1.1" + "nullthrows": "^1.1.1", + "@callstack/out-of-tree-platforms": "0.74.0" }, "peerDependencies": { "react": "18.2.0",