Skip to content

Platform.os binding prevents inlining/optimization #239

@grabbou

Description

@grabbou

Metro bundler inlines (ReactNative.)Platform.OS and (ReactNative.)Platform.select, so that when packaging for one platform, the code for the other platform is optimized away.

So if I had

[@bs.module "react-native"] [@bs.scope "Platform"] external os : string = "OS";

if (os === "ios") {
  Js.log("ios");
} else {
  Js.log("android");
};

this would get compiled by BuckleScript to

if (ReactNative.Platform.OS === "ios") {
  console.log("ios");
} else {
  console.log("android");
}

and Metro Bundler would inline ReactNative.Platform.OS and subsequently optimize the Android branch away for an iOS build.

However, with the current Rebolt bindings, I have

switch (Platform.os()) {
| IOS(_) => Js.log("ios")
| Android => Js.log("android")
};

which gets compiled by BuckleScript to

var match = Platform$Rebolt.os(/* () */0);
if (match) {
  console.log("ios");
  return /* () */0;
} else {
  console.log("android");
  return /* () */0;
}

This prevents the inlining/optimization from working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions