Skip to content

Commit

Permalink
fix(#48) local build of @callstack/out-of-tree-platforms (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Dec 12, 2023
1 parent b0deac7 commit f959837
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/out-of-tree-platforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"license": "MIT",
"exports": {
".": "./dist/index.js",
".": "./src/index.js",
"./package.json": "./package.json"
},
"files": [
Expand Down
43 changes: 43 additions & 0 deletions packages/out-of-tree-platforms/src/getPlatformResolver.js
Original file line number Diff line number Diff line change
@@ -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);
};
};
47 changes: 4 additions & 43 deletions packages/out-of-tree-platforms/src/index.js
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 2 additions & 1 deletion packages/rn-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f959837

Please sign in to comment.