Dynamicaly import user-provided ReScript module

I’m writing a CLI program in ReScript. I want to let users define a ReScript module that would change some behaviour of my program.

In JavaScript, I would let users set an option that defines a Node module, which I would then load with require() or dynamic import.

In ReScript I can load the code by writing custom bindings for require() / import() (if I figure out how to find/create the compiled .bs.js), but I wont be able to use it as a ReScript module.

Any ideas on how to make this work?

How about using a first-class module? You can dynamically switch behaviour at runtime by picking among different first-class modules.

1 Like

The problem is that the code of the module has to come from the user. From the user perspective it should look something like this:

$ node my-program.bs.js --config config.res


$ node my-program.bs.js --config CustomConfig

(Where CustomConfig is implemented in a file that was not available during compilation of my-program.bs.js)

I’ll probably go with config.js instead, but maybe there’s a way to make .res work?

Not that I can think of, since ReScript is statically compiled. You can of course bind to the import() function and load the module as a promise at runtime, provided you give it a fixed module type. In other words the config has to have a specific shape.