Create a reusable ES5 component from a ReasonML project


#1

Hi,

I am trying to create a React component/module based on ReasonML, which can be built / transpiled / published as an ES5 module, so that any JS-based project can import it and use it.

Ideally I would be able to do:

yarn build

…and get a “dist” folder with ES5 code inside.

What I have so far is here: https://github.com/dmaksimovic/reasonml-clock

Basically I’m trying to make it two-step, so that BuckleScript first converts Reason files into .bs.js files, then webpack bundling all that into a single output file. At least that’s the idea. But the end-product above doesn’t give me like “module.exports = Clock” or anything, instead all those “make”, “component”, etc. are exported and the target application throws errors, like “expected React element, but got object instead”.

Anybody knows what I need to do?


#2

If you’re creating a component that should be usable from JS, then use https://reasonml.github.io/reason-react/docs/en/interop.html#reactjs-using-reasonreact


#3

That’s exactly the piece I was missing, plus I also forgot to add libraryTarget: “umd” in webpack config. Thanks!


#4

Hopefully one day you won’t even need to bundle, if this task is implemented :slight_smile: