Rescript.json suffix confusion

so I’m revisiting this hellhole again trying to figure things out. because of node.js and esm, it’s turned into an absolute mess. with rescript on top of that, it’s even more confusing.

first off, if I provied a rescript library, it seems to be built with rescript using the suffix from the client rescript.json.

that means I can set type: module in the main repo and then use suffix .res.js, for example.

however, it will use the library’s package.json, which won’t have type: module set in it, which makes everything not run (early runtime failure)

alright, so I can set the suffix to .res.mjs and not have to care about setting type: module, and the library will be built with .res.mjs too. but now the rescript core library stops working:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘…/node_modules/@rescript/core/src/Core__Option.res.mjs’

because it doesn’t provide any .res.mjs files, only .mjs or .js it seems? so now I have to set my suffix to .mjs for it to work.

I mean, bottom line - why do I even get a pick? what am I misunderstanding? optimally I want to use .res.js and type module because I dislike using mjs and I want to have .res for clarity, but then nothing works lol

Maybe this will help, currently working on a project and these are the relevant settings and everything works perfectly

rescript.json

...
"package-specs": {
    "module": "es6",
    "in-source": true
	},
"suffix": ".bs.mjs",
...

I had type:module in package.json but just removed it, doesn’t seem to matter.

So now when it compiles I’ll have something like

Exec.res
Exec.bs.mjs

And I’ll run it with the line in package.json like

“backend:dev”: “dotenv -e .env.dev nodemon backend/src/exec.bs.mjs”,

what do you think?

That compiles for me, but it doesn’t run because @rescript/core doesn’t provide .bs.mjs-files:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘…/node_modules/@rescript/core/src/Core__Option.bs.mjs’ …

actually it doesn’t work if I set in-source to false either:

rror [ERR_MODULE_NOT_FOUND]: Cannot find module ‘…/node_modules/@rescript/core/lib/es6/src/Core__Option.mjs’ …

this is 11-rc7, is this a regression or something?

Try to clean and rebuild. Also, if you compile to esm, always use the .bs.mjs/.res.mjs extension

I did - but only .mjs works. Maybe I can come up with a small repro for this, but it just isn’t building. It seems to have to do with rescript libraries

I don’t know. It works for me

I have to admit I’ve seen problems like this in my own code and I’m not quite sure how I fixed it.

I did accept however that suffix: “bs.mjs” causes the least problem. The .mjs because node doesn’t need some config file to figure out how to run it, and .bs. because otherwise you might overwrite some javascript code you are binding too (yes I’ve done that)

I’ve had suffix problems but with only the Option core module

I’ve had problems because using vite will require type:“module” be set, but rescript doesn’t require it, so when you are working on the backend side you think nothing is wrong until you start up the front end.

it doesnt help that rescript init creates a config with “suffix”: “.bs.js”

and various bundlers like vite and rescript-starter-app will transpile things behind the scenes so you might not be actually using ES6 so you might be sheltered from these problems until you crank up node for something.

and the getting started docs tell you to do one thing, then the user discovers rescript init which creates a different config, and then the rescript react docs don’t even tell you how to get started. rust is more welcoming than rescript and that’s saying something.

if I knew what I was doing with rescript and could help I’d focus on two areas of improvement, get rescript init to be friendly to new and existing projects, remove the setup docs and tell the user to use rescript init, and get vite to add a rescript template that shows up like this

❯ npm create vite@latest
Need to install the following packages:
create-vite@5.1.0
Ok to proceed? (y) y
✔ Project name: … vite-project
? Select a framework: › - Use arrow-keys. Return to submit.
❯   Vanilla
    Vue
    React
    Preact
    Lit
    Svelte
    Solid
    Qwik
    Rescript-React <----------
    Others

It’s already like this in V11

2 Likes