Skip to content

Commit 4ea0e8a

Browse files
committed
move implementation to user space
1 parent 797b688 commit 4ea0e8a

File tree

8 files changed

+33
-161
lines changed

8 files changed

+33
-161
lines changed

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

Lines changed: 0 additions & 110 deletions
This file was deleted.

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ export type ConfigLoadingContext = $ReadOnly<{
2929
/**
3030
* Get the config options to override based on RN CLI inputs.
3131
*/
32-
function getOverrideConfig(
33-
ctx: ConfigLoadingContext,
34-
projectConfig: {
35-
filepath: string,
36-
isEmpty: boolean,
37-
config: (ConfigT => Promise<ConfigT>) | (ConfigT => ConfigT) | InputConfigT,
38-
...
39-
},
40-
): InputConfigT {
32+
function getOverrideConfig(ctx: ConfigLoadingContext): InputConfigT {
4133
const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
4234
platform => ctx.platforms[platform].npmPackageName,
4335
);
@@ -55,16 +47,6 @@ function getOverrideConfig(
5547
{},
5648
),
5749
);
58-
59-
outOfTreePlatforms.forEach(platform => {
60-
if (typeof projectConfig.config !== 'function') {
61-
resolver.sourceExts =
62-
projectConfig.config.resolver?.sourceExts?.flatMap(ext => [
63-
`${platform}.${ext}`,
64-
ext,
65-
]);
66-
}
67-
});
6850
}
6951

7052
return {
@@ -97,9 +79,10 @@ export default async function loadMetroConfig(
9779
ctx: ConfigLoadingContext,
9880
options: YargArguments = {},
9981
): Promise<ConfigT> {
82+
const overrideConfig = getOverrideConfig(ctx);
83+
10084
const cwd = ctx.root;
10185
const projectConfig = await resolveConfig(options.config, cwd);
102-
const overrideConfig = getOverrideConfig(ctx, projectConfig);
10386

10487
if (projectConfig.isEmpty) {
10588
throw new CLIError(`No Metro config found in ${cwd}`);

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@ import type {CustomResolver} from 'metro-resolver';
1313

1414
/**
1515
* This is an implementation of a metro resolveRequest option which will remap react-native imports
16-
* to different npm packages based on the platform requested. This allows a single metro instance/config
16+
* to different npm packages based on the platform requested. This allows a single metro instance/config
1717
* to produce bundles for multiple out of tree platforms at a time.
1818
*
19-
* This resolver also supports custom `variant` option, which is intended to be used for resolving
20-
* different variants of the same platform, like visionOS or tvOS based on iOS.
21-
*
22-
* Ex:
23-
* - `?resolve.variant=visionos`
24-
* - `?resolve.variant=tvos`
25-
*
2619
* @param platformImplementations
2720
* A map of platform to npm package that implements that platform
2821
*
@@ -36,19 +29,13 @@ export function reactNativePlatformResolver(platformImplementations: {
3629
[platform: string]: string,
3730
}): CustomResolver {
3831
return (context, moduleName, platform) => {
39-
let platformOrVariant: string | null =
40-
// $FlowFixMe
41-
context.customResolverOptions.variant || platform;
4232
let modifiedModuleName = moduleName;
43-
if (
44-
platformOrVariant != null &&
45-
platformImplementations[platformOrVariant]
46-
) {
33+
if (platform != null && platformImplementations[platform]) {
4734
if (moduleName === 'react-native') {
48-
modifiedModuleName = platformImplementations[platformOrVariant];
35+
modifiedModuleName = platformImplementations[platform];
4936
} else if (moduleName.startsWith('react-native/')) {
5037
modifiedModuleName = `${
51-
platformImplementations[platformOrVariant]
38+
platformImplementations[platform]
5239
}/${modifiedModuleName.slice('react-native/'.length)}`;
5340
}
5441
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ + (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.variant" value:RCTPlatformVariant],
313-
#endif
314311
[[NSURLQueryItem alloc] initWithName:@"dev" value:enableDev ? @"true" : @"false"],
315312
[[NSURLQueryItem alloc] initWithName:@"lazy" value:lazy ? @"true" : @"false"],
316313
[[NSURLQueryItem alloc] initWithName:@"minify" value:enableMinification ? @"true" : @"false"],

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

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

1010
RCT_EXTERN NSString *const RCTPlatformName;
11-
#if TARGET_OS_VISION
12-
RCT_EXTERN NSString *const RCTPlatformVariant;
13-
#endif
1411

1512
RCT_EXTERN NSString *const RCTUserInterfaceStyleDidChangeNotification;
1613
RCT_EXTERN NSString *const RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey;

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

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

1010
NSString *const RCTPlatformName = @"ios";
11-
#if TARGET_OS_VISION
12-
NSString *const RCTPlatformVariant = @"visionos";
13-
#endif
1411

1512
NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification";
1613
NSString *const RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey = @"traitCollection";

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

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

packages/rn-tester/metro.config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ const {getDefaultConfig} = require('@react-native/metro-config');
1313
const {mergeConfig} = require('metro-config');
1414
const path = require('path');
1515

16+
// todo: extract to package
17+
const PlatformResolver = (config = {}) => {
18+
const platformExtension = config.platformExtension ?? 'visionos';
19+
const platformName =
20+
config.platformName ?? '@callstack/react-native-visionos';
21+
22+
return (context, moduleName, platform) => {
23+
let modifiedModuleName = moduleName;
24+
if (platformExtension != null && platformName) {
25+
if (moduleName === 'react-native') {
26+
modifiedModuleName = platformName;
27+
} else if (moduleName.startsWith('react-native/')) {
28+
modifiedModuleName = `${platformName}/${modifiedModuleName.slice(
29+
'react-native/'.length,
30+
)}`;
31+
}
32+
}
33+
34+
return context.resolveRequest(context, modifiedModuleName, platform);
35+
};
36+
};
37+
1638
/**
1739
* This cli config is needed for development purposes, e.g. for running
1840
* integration tests during local development or on CI services.
@@ -36,6 +58,10 @@ const config = {
3658
extraNodeModules: {
3759
'react-native': path.resolve(__dirname, '../react-native'),
3860
},
61+
resolveRequest: PlatformResolver(),
62+
sourceExts: getDefaultConfig(__dirname).resolver.sourceExts?.flatMap(
63+
ext => [`visionos.${ext}`, ext],
64+
),
3965
},
4066
};
4167

0 commit comments

Comments
 (0)