[Help] How to build to JS consumers and test

Hello,

hope everyone is having a glorious Sunday :smiley:

Following along in my journey of learning ReScript, I decided to rewrite a little helper I have written in plain JS for my blog and make a ReScript module out of it.
Nothing too fancy, just a function that given a text then it counts out the number of words and estimates the reading time.

So far so good, migrating to ReScript also allowed me to find some bugs here and there (mostly edge cases). Now I thought it would be nice to publish it as a JavaScript/TypeScript package and make it available for non-ReScript users.
The only problem I’m having is that, as you can see from the generated JavaScript file, var Caml_int32 = require("rescript/lib/js/caml_int32.js"); is included as an external resource and not bundled within the output file. This is not really convenient as it requires the user to have ReScript installed nonetheless; also I would like to output it as UMD as well as CJS and ESM. Is that possible without a bundler? If not, do you have anything light to suggest? I would like to not use webpack for such small project but I’m kinda outdated on this topic :sweat_smile:

As a side question, do you have any testing library to suggest? Would like to add some tests both for robustness and learning purposes. I read there was a plan of making an official testing library but didn’t managed to find that much.

Thanks!

If you don’t use any third-party dependencies ReScript has a standalone standard library which can be added as a dependency.

This is a topic dear to me[1], and I’d love to see some solution where you could easily create more complex libraries or applications without the need for specialised bundling.

  1. How to publish a rescript project for javascript users - #11 by lessp
1 Like

An article about testing in ReScript: Testing in ReScript - DEV Community

1 Like

As for building a lib for Js consumers you can use rollup with a simple config like this: Delete old rollup config · DZakh/rescript-struct@d4ab58d · GitHub
I’ll try to write a proper guide describing all the gotchas this week.

1 Like

@cometkim you’ve spent some time thinking about this too, right? Or was that more related to the emitted TS types?

1 Like

Nah, I think it’ll need a bundler anyway. Especially if it supports UMD.

Probably you interested in this

Feel free to leave feedback in there

5 Likes

Wow, I wasn’t aware that there are ongoing efforts to adress this. That’s amazing! I’ll try to describe some of the pain points we have had.

cc @jihchi

2 Likes

Hey, thanks for everyone jumping in! Lot of interesting stuff to read about and experiment with!

Best!

So, I did as suggested here and as pointed out by @lessp in previous comments.
I couldn’t make microbundle work as I wanted so I just switched to rollup and now it works like a charm.

I would probably write a guide about it, because why not!

Thanks everyone for helping!

1 Like