Close module or re-open an "original" (eg: Js)

Quick question for a given use case:

I use “rescript-js” using bsc-flags

  "bsc-flags": [
    "-open Belt",
    "-open ReScriptJs"
  ],

This is totally fine for me until I started to use rescript-relay which need at some point Js.Exn (currently missing from rescript-js).

So I was wondering: can I keep this global open ReScriptJs and “close” it somehow in a given file?
Or reopen the “original Js” module ?

Thanks for your tips, have a nice day :slight_smile:

3 Likes

I think you you can override ReScriptJs module to work it around. Put somewhere in your codebase file ReScriptJs.res

module Js = {
   include  ReScriptJs__Js
   module Exn = Js_exn
}
2 Likes

Thanks for the trick !

Main idea here is that since Js module is shadowed there is no way to get access to original Js module. But you can get submodules like Js_exn and use it one way or another.

This is working well but has a limitation: in case of ReScriptJs, Promise has been overwritten by @ryyppy/rescript-promise and this is causing trouble as generated code by rescript-relay is looking for the original one.
I have to not open ReScriptJs via bcc-flags and keep local open :frowning:

How about doing something like this?

module Js = {
   include  ReScriptJs__Js
   module Exn = Js_exn
   module Promise = Js_promise
   module Promise2 = ReScriptJs.Js.Promise
}
1 Like

I never use -open, I made my own mini stdlib file that I manually open at the top of every file. This “mini stdlib” uses a combination of module X = Belt.X and include to bring together everything I use regularly.

When done this way, the original Js reference can be stored before open ReScriptJs.

Is it possible to override an open like Belt? I like to just use belt but include my own modules, and creating a file named Belt in my own repo doesn’t seem to work.

It’s impossible, so I’ve written a tool to solve the problem: https://github.com/DZakh/rescript-stdlib-cli.
It misses some features for gradual adoption, but the main functionality works properly. We use it in production without a problem.

Also, here’s a good post from @Ryan explaining how to vendor a standard library: RFC: The ultimate answer to Belt vs Js in ReScript - #7 by Ryan

2 Likes

Those are very valuable resources! Thank you very much!