Skip to content

Breaking: Remove Platform inlining, now done by React Native - #2195

Open
robhogan wants to merge 2 commits into
mainfrom
pr2195
Open

robhogan wants to merge 2 commits into
mainfrom
pr2195

Conversation

@robhogan

@robhogan robhogan commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

Since React Native 0.88, @react-native/babel-preset inlines Platform.OS and Platform.select itself (react/react-native#57848), whenever Metro sets caller.inlinePlatform:

https://github.com/react/react-native/blob/v0.89.0-rc.0/packages/react-native-babel-preset/src/configs/main.js#L165-L167

Metro's own implementation in inline-plugin runs after ESM has been lowered to CommonJS, so it can only match on names. It inlines .OS on any top-level binding called Platform, whether or not it's React Native's, and misses some genuine imports. RN's plugin tracks bindings back to React Native's Platform module, so Metro's has been redundant since 0.88, and occasionally wrong.

This removes platform inlining from inline-plugin, which still inlines __DEV__ and process.env.NODE_ENV, and narrows its options to {dev}. Metro still sets the inlinePlatform transform option and passes it through to Babel - that's what enables RN's plugin.

Projects on React Native < 0.88 with this Metro will lose platform inlining, so this is breaking. RN and Expo both pin Metro, so in practice that only affects projects overriding Metro's version. Expo's fork of the transform worker already skips inlinePlugin.

Changelog:

 - **[Breaking]**: Remove Metro's `Platform.OS` / `Platform.select` inlining, which `@react-native/babel-preset` performs since React Native 0.88

Test plan:
Built a React Native 0.89.0-rc.0 app (@react-native/metro-config and @react-native/babel-preset from npm) with this branch's transform worker, for iOS and Android, dev and prod, unminified, and compared against main. The entry point reads Platform.OS through a named import, a namespace import, a deep import of Libraries/Utilities/Platform and require('react-native').Platform, and calls Platform.select. All are inlined on both.

The bundles, including all of React Native's own Platform uses, are byte-identical except for one line in a local module that declares an unrelated const Platform = {OS: ...}, which main wrongly inlined:

-  var os = exports.os = "ios";
+  var os = exports.os = Platform.OS;

Since React Native 0.88, `@react-native/babel-preset` inlines `Platform.OS` and `Platform.select` itself (react/react-native#57848), whenever Metro sets `caller.inlinePlatform`:

https://github.com/react/react-native/blob/v0.89.0-rc.0/packages/react-native-babel-preset/src/configs/main.js#L165-L167

Metro's own implementation in `inline-plugin` runs after ESM has been lowered to CommonJS, so it can only match on names. It inlines `.OS` on any top-level binding called `Platform`, whether or not it's React Native's, and misses some genuine imports. RN's plugin tracks bindings back to React Native's `Platform` module, so Metro's has been redundant since 0.88, and occasionally wrong.

This removes platform inlining from `inline-plugin`, which still inlines `__DEV__` and `process.env.NODE_ENV`, and narrows its options to `{dev}`. Metro still sets the `inlinePlatform` transform option and passes it through to Babel - that's what enables RN's plugin.

Projects on React Native < 0.88 with this Metro will lose platform inlining, so this is breaking. RN and Expo both pin Metro, so in practice that only affects projects overriding Metro's version. Expo's fork of the transform worker already skips `inlinePlugin`.

Changelog:
```
 - **[Breaking]**: Remove Metro's `Platform.OS` / `Platform.select` inlining, which `@react-native/babel-preset` performs since React Native 0.88
```

Test plan:
Built a React Native 0.89.0-rc.0 app (`@react-native/metro-config` and `@react-native/babel-preset` from npm) with this branch's transform worker, for iOS and Android, dev and prod, unminified, and compared against `main`. The entry point reads `Platform.OS` through a named import, a namespace import, a deep import of `Libraries/Utilities/Platform` and `require('react-native').Platform`, and calls `Platform.select`. All are inlined on both.

The bundles, including all of React Native's own `Platform` uses, are byte-identical except for one line in a local module that declares an unrelated `const Platform = {OS: ...}`, which `main` wrongly inlined:

```diff
-  var os = exports.os = "ios";
+  var os = exports.os = Platform.OS;
```
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Metro review: Looks good

Risk: medium · Commit: e7cc249 · 5 commands run

This removes Platform.OS and Platform.select inlining from inline-plugin, since @react-native/babel-preset now does it, and narrows the plugin's options to {dev}. Metro still passes inlinePlatform through to Babel. Since my last review, the new commit only simplifies the plugin (no behavior change) and makes the test that pins the removal pass inlinePlatform: true, platform: 'ios', so it now checks something real. Tests, Flow, lint and API snapshots pass, and I found nothing to change. The break is intended and is labeled [Breaking] in the changelog.

No findings.

Verified
  • ✅ yarn jest --no-watchman packages/metro-transform-plugins packages/metro-transform-worker — 9 suites, 197 tests, 70 snapshots
  • ✅ yarn typecheck
  • ✅ yarn lint packages/metro-transform-plugins/src packages/metro-transform-worker/src
  • ✅ yarn verify-api-snapshots — 18 snapshots verified. The metro-transform-plugins API report doesn't expose the plugin's Options shape, so no snapshot change is needed.
  • ✅ git diff dca390b15b6e HEAD — Checked the changes since the previous review: a refactor that keeps behavior the same, plus the stronger removal test.

Not run: I didn't run the head's inline-plugin test against the base plugin, because the session denied the commands needed for it. From the code, the base plugin with inlinePlatform: true would rewrite Platform.OS in that fixture, so the test should fail there. I didn't rebuild the React Native 0.89 app comparison from the test plan. No benchmark: the change only removes visitor work from the per-file plugin, so it can't make it slower. Nothing here is Windows-specific.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

@robhogan robhogan added the Auto review: Good The Metro review bot found nothing to change at this head label Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Auto review: Good The Metro review bot found nothing to change at this head CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant