Skip to content

Fix platform inline transform for babel esmodules commonjs transform output - #800

Closed
janicduplessis wants to merge 1 commit into
react:mainfrom
janicduplessis:@janic/inline-platform
Closed

janicduplessis wants to merge 1 commit into
react:mainfrom
janicduplessis:@janic/inline-platform

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Apr 8, 2022 •

Copy link
Copy Markdown
Contributor

Summary

I noticed that when using esmodule named import of platform dead code elimination of Platform.OS did not work.

Does not work

Source:

import { Platform } from 'react-native';

if (Platform.OS === 'android') {
  // code
}

Transformed:

var _reactNative = _$$_REQUIRE(_dependencyMap[4]);

if (_reactNative.Platform.OS !== 'android') {
  // code
}

Works

Source:

const { Platform } = require('react-native');

if (Platform.OS === 'android') {
  // code
}

Transformed:

var _require = _$$_REQUIRE(_dependencyMap[4]),
Platform = _require.Platform;

if ('ios' !== 'android') {
  // code
}

The inline plugin works on transformed and bundled code. For the working require case it works since Platform is assigned to a top level variable, which the plugin considers as valid for inlining. In the case of import babel transforms it to a single top level require call (_reactNative), but the name doesn't match any of the allowed ones (ReactNative, React). This means it is not considered valid for inlining.

The name of the variable seems based on the name of the module imported, so react-native becomes _reactNative. I also noticed some cases in an app bundle where babel will add a number suffix to the variable like _reactNative2. This seems to be cause by other imports containing the name react-native like @sentry/react-native.

To support both cases I added support for passing a regex to the valid top level binding names. We can then use this to match _reactNative + optional number.

Test plan

Added a test for the case similar to how code looks in the transformed bundle before being passed to the inline plugin. Also tested this in an app bundle and looked for instances of Platform.OS than should be inlined and made sure there was none.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 8, 2022
@motiz88

motiz88 commented Apr 8, 2022 •

Copy link
Copy Markdown
Contributor

I agree that this needs to be fixed, but I don't think we should use a regex (or variable name matching in general) to do this as it's too brittle and unsound. We need to find a way to properly track/detect imported bindings in Metro's intermediate representation.

Notably I think Platform inlining does work with experimentalImportExportSupport: true, which we should probably document and turn on by default.

@motiz88
motiz88 self-requested a review April 8, 2022 04:38

@motiz88 motiz88 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment.

@motiz88

motiz88 commented Apr 8, 2022

Copy link
Copy Markdown
Contributor

One practical and safe way we can do this is by passing the dependency map to the plugin and using it to understand that the _$$_REQUIRE(_dependencyMap[4]) call evaluates to the react-native module.

@janicduplessis

Copy link
Copy Markdown
Contributor Author

Yea, the current implementation is brittle since it doesn't have any info about modules and just relies on variable names. Passing the dependency map seems like the proper solution. I'll see when I can find some time to work on this again.

@janicduplessis

Copy link
Copy Markdown
Contributor Author

@motiz88 Any change we can merge this as is to fix the issue? Sadly I don't think I will have time to implement a better solution.

If we don't want to merge this then how stable is experimentalImportExportSupport? Could it be turned on by default and considered not experimental anymore at the moment?

@motiz88

motiz88 commented Aug 18, 2022

Copy link
Copy Markdown
Contributor

At Meta, we've been using experimentalImportSupport in production for a few years. I think the main thing to consider at this point is how spec-compliant it is, and more to the point, whether it has any observable differences from the stock Babel transform in that regard. If we find that it is a drop-in replacement it would be an easy choice to default it to true.

(cc @jacdebug for the spec compliance question, maybe?)

@robhogan

robhogan commented Sep 9, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for your efforts here - this issue has (just recently) been fixed by moving the inline plugin ahead of the CommonJS transform, and into @react-native/babel-preset: react/react-native#57848. Metro's own plugin will be removed in future.

That will be released in RN 0.88, so closing as superseded.

@robhogan robhogan closed this Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants