Private modules in bsconfig

According to the documentation it’s possible to achieve private modules by configuring sources (you have to press the + to see the full definition)

I have a lib with this in my bsconfig:

...
"sources": [
    {
      "dir": "src",
      "subdirs": true,
      "public": [
        "Components",
      ]
    }
  ]

The intention is to only have the Components module accessible at Library.Components, however I can’t seem to figure out how to make this work. With this current config, all modules are still visible, ie Lib.Components.Foo

Has anyone got a working example of this? I’ve looked at plenty of rescript libs but haven’t stumbled across any examples.

2 Likes

I found an example: tablecloth-rescript/bsconfig.json at main · darklang/tablecloth-rescript · GitHub

I tried it in a monorepo. The compiler complains about modules it cannot find when I refer to them from another package, so that works.

However, rescript-vscode happily autocompletes modules that are not in the public array so there is some discrepancy there.

3 Likes

Ahh interesting - so it looks like the issue comes from subdirs rules taking precedence. I guess a flat folder structure is the only way around this!

Good to know.

Maybe you can still use subdirs for public code if you use two sources?

something like

...
  "sources": [
    {
      "dir": "src",
      "subdirs": false,
      "public": [
        "Components"
      ]
    },
    {
      "dir": "src-public",
      "subdirs": true
    }
  ]
2 Likes

This is actually exactly what I ended up doing! One private src, one public directory

1 Like