How to only expose one module from a lib

Hi,

I’m making a ReScript lib and I only want to expose one top module.

Like having multiple modules:

src/
src/A.res
src/B.res
src/MyLib.res
src/MyLib.resi

MyLib uses internally functions from A and B but from the user perspective I only want to be able to use what is exposed in MyLib.resi:

// Possible
let _ = MyLib.fun(...)

// Impossible
let _ = A.fun(..)

Any idea :innocent: ?

Thanks <3

1 Like

You can try the public option for sources in your bsconfig file to select which modules are exported.

Eg it may look something like this:

{
  "name": "the-lib",
  "sources": {
    "dir": "src",
    "public": ["Foo"]
  },
  ...

That would only export the Foo module from your lib. So then in an app consuming your lib, you would only have access to Foo.

Note that when I have tried this, it is a bit …wonky. For example, I would still get the auto-complete in vscode for the private modules, but when compiling the code, you get errors about missing modules. (So you could see them, but are not allowed to use them…that may well enough work for you.)

Here is another similar question.

1 Like

It’s probably just nicer to do something like rescript core and make your “internal” modules prefixed…Core__Blah, Core__Foo, etc. Then coordinate things from the “main” module:

module Blah = Core__Blah
module Foo = Core__Foo

Then just mention your naming strategy to your users about which are “private” or “internal”.

That it shows up in autocomplete sounds like a bug that should be fixed. Was there more wonkiness?

1 Like

I could open an issue later on at some point…would that be on the vscode plugin repository?

Off the top of my head…the public didn’t seem to have an effect when using multiple sources. Eg

"sources": [
  {"dir": "src_a", "public": ["Foo"]},
  {"dir": "src_b", ...}
]

In src_b all the modules in src_a would be visible, not just Foo. Though I suspect that is actually the intended behavior, as I interpret the public to only be affecting how the outside world interacts with the library. Could be something worth mentioning in the docs somewhere though.

2 Likes

Then we rely on the behavior of the programmer, I’m not fan of it :confused:

1 Like

Thanks, I’ll try it out !

Is there best practices about it?

The “non public modules still show in autocomplete” issue has been fixed and will soon be in the VSCode prerelease version: Account for "public" setting in bsconfig by zth · Pull Request #824 · rescript-lang/rescript-vscode · GitHub

6 Likes

I added the feature to the docs: Document 'public' feature by fhammerschmidt · Pull Request #721 · rescript-association/rescript-lang.org · GitHub

5 Likes