How to bind to local JS file when using `"in-source": false`

If I have the following structure:

- src/
  - Foo.res
  - module.js

and I bind to module.js using @module, when I go to run the outputted .bs.js file in lib/js/src/Foo.bs.js it won’t be able to find module.js

How should I handle this?

Hi!
If you using webpack as a bundler, you can define a resolve.alias. For example:

const path = require('path');

module.exports = {
  //...
  resolve: {
    alias: {
      src: path.resolve(__dirname, 'src'),
    },
  },
};

Now suppose you have a bar function in module.js, then in Foo.res you can write

@module("src/module.js")
external bar: int => int = "bar"

let result = bar(10)

And now webpack should build everything correctly and it should work.

Or maybe someone can suggest a better option :slight_smile:

2 Likes

If you do the out of source builds, currently you can leave your JS stubs in a separate package, then you can build it in a normal way.
The other option that we are trying to explore is to allow user extend the build system to copy your module.js to lib/js/src as well.
Curious why in source build is not an option for your libs?

1 Like

What I usually do for this scenarios is that I bind with a relative path to lib/js/src. Simple and it just works.
That’s also why I like to output JS close to the ReScript source.

1 Like

Ok good to know.

In source is an option, I would just prefer not to for aesthetic reasons.

I recently did a rescript init and the default template doesn’t have in-source and outputs to lib/js. May be worth revisiting if in-source is the less painful option for the default project.

1 Like