Hi, I’m working on a project with builds for Browser, React Native, and Electron. I’ve written a seperate IO module for each platform, each module has the same interface so they can be used interchangeably. I’d like to be able to switch module is being used at bundle time, while retaining type safety
I tried using Old Docs, but this can only target one platform at a time, and I’d like to develop on multiple platforms simultaniously.
Seems like a thing to be done with functors but I havnt seen it done that extensively.
Could you separate into different packages and include at the yaml/npm level?
I don’t see any reason why this wouldn’t work. E.g., say you have files arranged like:
src/io/IO__Target.ml
src/io/target/IO__Target__Browser.res
src/io/target/IO__Target__ReactNative.res
src/io/target/IO__Target__Electron.res
Then in src/io/IO__Target.ml you can define:
module Browser = IO__Target__Browser
module ReactNative = IO__Target__ReactNative
module Electron = IO__Target__Electron
module type S = sig ... end
module Make(IO : S) = struct ... end
module Platform = Make(
#if defined TARGET && TARGET = "browser" then
Browser
#else if defined TARGET && TARGET = "reactnative" then
ReactNative
#else if defined TARGET && TARGET = "electron" then
Electron
#end
)
You can also experiment with different file and folder layouts, but this should be a good starting point. To build for each platform just pass in the env variable for the platform, e.g. TARGET=browser rescript.