Repository navigation
Fix platform inline transform for babel esmodules commonjs transform output - #800
janicduplessis wants to merge 1 commit into
Conversation
|
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 |
|
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 |
|
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. |
|
@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 |
|
At Meta, we've been using (cc @jacdebug for the spec compliance question, maybe?) |
|
Thanks for your efforts here - this issue has (just recently) been fixed by moving the inline plugin ahead of the CommonJS transform, and into That will be released in RN 0.88, so closing as superseded. |
Summary
I noticed that when using esmodule named import of platform dead code elimination of Platform.OS did not work.
Does not work
Source:
Transformed:
Works
Source:
Transformed:
The inline plugin works on transformed and bundled code. For the working
requirecase it works sincePlatformis assigned to a top level variable, which the plugin considers as valid for inlining. In the case ofimportbabel 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-nativebecomes_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 namereact-nativelike@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.OSthan should be inlined and made sure there was none.