Isomorphic Rescript to MoonBit

Isomorphic Rescript for backend and frontend and shared types is something I had been thinking for a while. Recently I started experimenting this idea again with MoonBit, and something clicked that I thought some fellow ReScript folks might find interesting.

The familiar idea

ReScript made it really nice to share code between frontend and backend:

  • shared types
  • shared validation
  • shared domain logic

But in practice the backend still ends up running on Node.

That works, but sometimes you’d prefer:

  • native binaries
  • predictable deployment
  • no JS runtime on the server

MoonBit’s take

MoonBit can compile the same code to:

  • JavaScript → for the browser
  • native backend → for servers

So the mental model becomes:

MoonBit
  ├─ JS → frontend
  └─ native → backend

instead of

ReScript
  ├─ JS → frontend
  └─ JS → Node backend

The isomorphic project explores exactly this idea: write logic once, run it in both places — but the server can be native, not Node. Plus derive(ToJson, FromJson), this makes the dev experience really nice.

It is still early days, but with the help of AI, I can whip quite a large app really quick(backend+frontend), so excited to share with you and hear your feedback!

3 Likes

Hello, thanks for brining this up here. I saw something on social media about this.
Would this fit into a React like model?
RSC but the server part is native?
I think Melange had some ideas like that.
Do you have an RPC style in mind?

I don’t follow - is this for doing ReScript ↔ MoonBit…? Or is this post just about MoonBit?

I believe this is more about Moonbit. But the angle is that this is something you can do today in ReScript, reuse types on both client/server. But that means the server is JS, while Moonbit could do native on the server. That is what I understood.

this is moonbit marketing :slight_smile: as much as i love rescript, this makes me jealous of moonbit haha. i do moonbit as well though, but not in production envs.

we just discussed internally two weeks back how “flimsy” it sometimes feel to deploy a nodejs bundle to backend for running on node. i wish we could compile it to a binary - in fact, i think nodejs should have some compile option to compile a js file to a binary, bundling it with only the needed parts of the runtime.

oh well, one can dream. best of luck with moonbit, absolutely awesome language (but get rid of the auto-inserted doc comments for every function :p)

Well you can do it with deno or bun. Both have a compile command that result in a self-contained binary which is roughly 60 megs for a hello world. So it’s not efficient at all compared to native languages but you can do it.

1 Like

This approach is ideal for developing agents with web embeded—it’s lightweight and has zero dependencies.

Apparently just Moonbit.

Yeah, with Bun single file executables and bytecode mode, you can get quite far. We still do have to solve going over the client/server boundary better and more ergonomically in the default case. That’s not ergonomic enough right now (unless you use something where it solves itself, like GraphQL).

This is one of the reasons I love using Deno and Deno deploy. Deno on its own is a better runtime than Node; better security, 2x requests per second than a Node server, and just a better overall story for what tools to use thanks to Deno having a strong built in server, a robust standard library, and Fresh is a very powerful front-end framework.

2 Likes