Skip to content

Commit 2c903f6

Browse files
committed
create out-of-tree-platforms package and bring back resolver options
1 parent 3d26468 commit 2c903f6

File tree

8 files changed

+101
-20
lines changed

8 files changed

+101
-20
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @callstack/out-of-tree-platforms
2+
3+
[![Version][version-badge]][package]
4+
5+
Utilities for Out of Tree (OOT) platforms.
6+
7+
## `getPlatformResolver`
8+
9+
```js
10+
getPlatformResolver(options: ResolverConfig): CustomResolver
11+
```
12+
13+
### options
14+
15+
```js
16+
type ResolverConfig = {
17+
platformImplementations: {[platform: string]: string},
18+
};
19+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
import type {CustomResolver} from 'metro-resolver';
12+
13+
type ResolverConfig = {
14+
platformImplementations: {[platform: string]: string},
15+
};
16+
17+
const getPlatformResolver = (config: ResolverConfig): CustomResolver => {
18+
return (context, moduleName, platform) => {
19+
const platformExtension = context.customResolverOptions?.platformExtension;
20+
let modifiedModuleName = moduleName;
21+
22+
if (
23+
typeof platformExtension === 'string' &&
24+
config.platformImplementations?.[platformExtension]
25+
) {
26+
const packageName = config.platformImplementations[platformExtension];
27+
if (moduleName === 'react-native') {
28+
modifiedModuleName = packageName;
29+
} else if (moduleName.startsWith('react-native/')) {
30+
modifiedModuleName = `${packageName}/${modifiedModuleName.slice(
31+
'react-native/'.length,
32+
)}`;
33+
}
34+
}
35+
36+
return context.resolveRequest(context, modifiedModuleName, platform);
37+
};
38+
};
39+
40+
module.exports = {getPlatformResolver};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@callstack/out-of-tree-platforms",
3+
"version": "0.74.0",
4+
"description": "Utils for React Native out of tree platforms.",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/facebook/react-native.git",
9+
"directory": "packages/out-of-tree-platforms"
10+
},
11+
"homepage": "https://github.com/facebook/react-native/tree/HEAD/packages/out-of-tree-platforms#readme",
12+
"keywords": [
13+
"out-of-tree",
14+
"react-native"
15+
],
16+
"bugs": "https://github.com/facebook/react-native/issues",
17+
"engines": {
18+
"node": ">=18"
19+
},
20+
"exports": "./index.js",
21+
"devDependencies": {
22+
"metro-resolver": "^0.80.0"
23+
}
24+
}

packages/react-native/React/Base/RCTBundleURLProvider.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ + (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
308308
BOOL lazy = enableDev;
309309
NSArray<NSURLQueryItem *> *queryItems = @[
310310
[[NSURLQueryItem alloc] initWithName:@"platform" value:RCTPlatformName],
311+
#if TARGET_OS_VISION
312+
[[NSURLQueryItem alloc] initWithName:@"resolver.platformExtension" value:RCTPlatformExtension],
313+
#endif
311314
[[NSURLQueryItem alloc] initWithName:@"dev" value:enableDev ? @"true" : @"false"],
312315
[[NSURLQueryItem alloc] initWithName:@"lazy" value:lazy ? @"true" : @"false"],
313316
[[NSURLQueryItem alloc] initWithName:@"minify" value:enableMinification ? @"true" : @"false"],

packages/react-native/React/Base/RCTConstants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#import <React/RCTDefines.h>
99

1010
RCT_EXTERN NSString *const RCTPlatformName;
11+
#if TARGET_OS_VISION
12+
RCT_EXTERN NSString *const RCTPlatformExtension;
13+
#endif
1114

1215
RCT_EXTERN NSString *const RCTUserInterfaceStyleDidChangeNotification;
1316
RCT_EXTERN NSString *const RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey;

packages/react-native/React/Base/RCTConstants.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#import "RCTConstants.h"
99

1010
NSString *const RCTPlatformName = @"ios";
11+
#if TARGET_OS_VISION
12+
NSString *const RCTPlatformExtension = @"visionos";
13+
#endif
1114

1215
NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification";
1316
NSString *const RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey = @"traitCollection";

packages/react-native/react-native.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ module.exports = {
2626
startCommand,
2727
],
2828
platforms: {
29+
visionos: {
30+
npmPackageName: '@callstack/react-native-visionos',
31+
projectConfig: ios.projectConfig,
32+
dependencyConfig: ios.dependencyConfig,
33+
},
2934
ios: {
3035
projectConfig: ios.projectConfig,
3136
dependencyConfig: ios.dependencyConfig,

packages/rn-tester/metro.config.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,11 @@
99

1010
'use strict';
1111

12+
const {getPlatformResolver} = require('@callstack/out-of-tree-platforms');
1213
const {getDefaultConfig} = require('@react-native/metro-config');
1314
const {mergeConfig} = require('metro-config');
1415
const path = require('path');
1516

16-
// todo: extract to package
17-
const PlatformResolver = (config = {}) => {
18-
const platformName =
19-
config.platformName ?? '@callstack/react-native-visionos';
20-
21-
return (context, moduleName, platform) => {
22-
let modifiedModuleName = moduleName;
23-
if (moduleName === 'react-native') {
24-
modifiedModuleName = platformName;
25-
} else if (moduleName.startsWith('react-native/')) {
26-
modifiedModuleName = `${platformName}/${modifiedModuleName.slice(
27-
'react-native/'.length,
28-
)}`;
29-
}
30-
31-
return context.resolveRequest(context, modifiedModuleName, platform);
32-
};
33-
};
34-
3517
/**
3618
* This cli config is needed for development purposes, e.g. for running
3719
* integration tests during local development or on CI services.
@@ -55,7 +37,9 @@ const config = {
5537
extraNodeModules: {
5638
'react-native': path.resolve(__dirname, '../react-native'),
5739
},
58-
resolveRequest: PlatformResolver(),
40+
resolveRequest: getPlatformResolver({
41+
platformImplementations: [{visionos: '@callstack/react-native-vision'}],
42+
}),
5943
sourceExts: getDefaultConfig(__dirname).resolver.sourceExts?.flatMap(
6044
ext => [`visionos.${ext}`, ext],
6145
),

0 commit comments

Comments
 (0)