Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Platform.OS #20

Closed
okwasniewski opened this issue Oct 20, 2023 · 2 comments
Closed

Add support for Platform.OS #20

okwasniewski opened this issue Oct 20, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@okwasniewski
Copy link
Member

okwasniewski commented Oct 20, 2023

Description

The goal of this issue is to add support for Platform.OS = 'visionos'.

The work might require writing a custom metro resolver.

@okwasniewski okwasniewski added the enhancement New feature or request label Oct 20, 2023
@thymikee thymikee self-assigned this Oct 27, 2023
@thymikee
Copy link
Member

Update from my work in this area:
Managed to add support for visionos as a new platform (similar to macos). It revealed that we either need to convert/bridge existing .ios.js files to visionos.js or add a fallback platform. Tried the fallback platform solution by customizing reactNativePlatformResolver:

export function reactNativePlatformResolver(platformImplementations: {
[platform: string]: string,
}): 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)}`;
}
}
return context.resolveRequest(context, modifiedModuleName, platform);
};
}

to accept

platformImplementations: {
  [platform: string]: [string, string | undefined],
}

as an argument, where the tuple would read as [platform = "visionos", basePlatform = "ios"]. This way I tricked Metro into resolving ios extensions as fallbacks, however this revealed another issue with Platform.OS being visionos, while internally we have lots of Platform.OS === "ios" calls. Which we would either need to augment with visionos checks (as tried here by @okwasniewski: e5b83e0) or entirely replace. Which adds maintenance overhead for an out-of-tree platform like this. Even then, for React Native VisionOS, which largely shares the same code with React Native iOS, it would mean we wouldn't be able to use most of the community iOS native packages, unless contributing extra platform checks. That's not scalable.

As a potential followup, I'm going to explore extending Platform primitive with a new "variant" concept. Variant would serve as an extension for the base platform. visionos would be variant of ios. tvos would be variant of ios. Maybe even macos could be variant of ios, but that's debatable. Adding variant support would likely mean either hacking around Platform concept between Metro and RN for iOS. Or extending Metro with this concept.

@okwasniewski
Copy link
Member Author

This feature won't be supported out of the box. There is metro-resolver-platform-variants package created by @douglowder that allows to achieve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants