Skip to content

Support react-native platform-specific extension #1436

Open

Description

@saschatimme

From the react-native docs:

For example, say you have the following files in your project:

BigButton.ios.js
BigButton.android.js
You can then require the component as follows:

const BigButton = require('./BigButton');

This works since react-native ships a custom javascript bundler by default.

It would be good to have the possibility to generate such file names with bucklescript. Probably some sort of name mangling would be necessary.

Activity

  1. bobzhang commented on Aug 28, 2017

    @bobzhang
    Member

    how is it different from ButtonIos ad ButtonAndroid?

  2. saschatimme commented on Aug 28, 2017

    @saschatimme
    ContributorAuthor

    The React Native packager (now extracted as metro-bundler) has as a hardcoded resolution rule to check for the name[.platform].js pattern:
    https://github.com/facebook/metro-bundler/blob/74e78ee43f73d0452dcce0db0887f055cf7abbee/packages/metro-bundler/src/node-haste/lib/parsePlatformFilePath.js#L30

  3. rafayepes commented on Aug 29, 2017

    @rafayepes

    how is it different from ButtonIos ad ButtonAndroid?

    It only bundles the one that matches your platform. It can be achieved for react-native-web with webpack as well. It a common system in the RN ecosystem to to build platform-specfic components.

    So the alternative I guess that would be to add static-if's that open the right module based on platform. But will be something very manual and cumbersome.

  4. bobzhang commented on Sep 25, 2017

    @bobzhang
    Member

    How do you achieve that producing Button.ios.js and Button.android.js at the same time?

  5. MoOx commented on Dec 14, 2017

    @MoOx
    Contributor

    I am facing the same issue. It's even blocking me with .web.js (RNW + webpack resolve option)

    (eg https://github.com/facebookincubator/create-react-app/blob/a1c5b8f7054ac0ac708a498c2a6308f3ed1cb8f0/packages/react-scripts/config/webpack.config.dev.js#L93)

    I saw that BS accept a option "suffix": ".whatever.js" to generate but that's clearly not possible if you have to deal with several suffix at the same time.
    I would like to have a single command that could compile my File.js and my File.platform.js to 2 files, not one (because as I am writing it's compiling each file, but is writting those at the same place).

  6. MoOx commented on Dec 14, 2017

    @MoOx
    Contributor

    Just an idea (I have in my case used a workaround using some webpack config since I have an issue with .web.js): using Platform.select might help to require the right file. See https://facebook.github.io/react-native/docs/next/platform-specific-code.html#docsNav for more info (Platform is available via bs-react-native)

  7. MoOx commented on Dec 16, 2018

    @MoOx
    Contributor

    How do you achieve that producing Button.ios.js and Button.android.js at the same time?

    Currently we have the following behavior

    • Something.re: module: Something, compiled Something.js
    • Something.else.re: module: Something, compiled Something.js
    • Something.el.se.re: module: Something, compiled Something.js

    @bobzhang How I see it what should happen is the following

    • Something.re: module: Something, compiled Something.js
    • Something.else.re: module: Something, compiled Something.else.js
    • Something.el.se.re: module: Something, compiled Something.el.se.js

    Maybe it's not the ultimate solution, but at least we are not completely dropping everything after the first dot, ignoring silently the first intention.
    At least if the solution needs more reflexion, we could throw an error if there is multiple dots in the filename?

  8. vknez commented on Nov 10, 2019

    @vknez

    I am experiencing similar challenges, and @MoOx's suggestions would help.

    I have a TypeScript + ReasonML React Native app that is compiled for 3 platforms: iOS, Android, and web. Quite often I need to make a distinction whether I'm requiring a component on web or native, and before introducing ReasonML I had the following approach (pretty common in React Native):

    I create two platform-specific files: MyComponent.native.tsx and MyComponent.web.tsx (native could be further split into ios|android). Then from the index.ts I do import { MyComponent} from "./MyComponent";. React Native correctly chooses which file to use for the import.

    My current alternative is to use JS (TS) wrapper components (with names mentioned above) on top of Reason files (MyComponentNative.re, MyComponentWeb.re), in order to be able to use those platform-specific extensions and import the specific bs.js component. This is not a big deal if those components are used only from the JavaScript world, but I need them in Reason as well (eg. a component that is the same on all platforms, but uses MyComponent).

  9. MoOx commented on Jan 22, 2020

    @MoOx
    Contributor

    Reminder for those looking for this, best quick workaround

    • Make your ThingPlatform.js for each platform
    • Make a Thing.{platform}.js which basically import/export ThingPlatform.bs.js for each platform
    • Make a Thing.re that is a binding of Thing.*.js

    Since reason-react 0.7 & zero cost bindings, the import/export step is not painful.

    But an official solution would be nice, but that could involve validating interfaces or all Thing.*.re & I understand that this requires work.

  10. ryyppy commented on Jan 12, 2021

    @ryyppy
    Member

    I think this is fixed now. Closed.

  11. 7 remaining items

  12. Freddy03h commented on May 21, 2023

    @Freddy03h

    😞

  13. fhammerschmidt commented on May 21, 2023

    @fhammerschmidt
    Member

    Yes, this is still an issue and should be reopened IMO.
    Except if we really won't fix it which I do not hope for.

    cc @cristianoc @ryyppy @cknitt

  14. reopened this on May 21, 2023
  15. removed
    staleOld issues that went stale
    on May 21, 2023
  16. Freddy03h commented on May 22, 2023

    @Freddy03h

    @fhammerschmidt thank you!

    The issue was very well explained by @MoOx, for now it's still unusable…

  17. github-actions commented on Aug 26, 2025

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  18. fhammerschmidt commented on Aug 26, 2025

    @fhammerschmidt
    Member

    Bad bot

  19. github-actions commented on Aug 23, 2026

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  20. Freddy03h commented on Aug 23, 2026

    @Freddy03h

    still

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions