How to start a minimal ReasonML project — without boilerplate

I was reading the quickstart guide and that is nice when you are reading about ReasonML for the first time or are starting from scratch, but what if you want to do it yourself or want to incoporate it into an existing project? This is a guide how to minimally get started with ReasonML without using a boilerplate.

The first thing we need is a bsconfig.json , that is needed for the Bucklescript compiler that turns the ReasonML into JavaScript.

It looks like this:

{
"name": "my-first-reasonml",
"version": "0.1.0",
"sources": {
"dir" : "src",
"subdirs" : true
},
"package-specs": {
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": [],
"warnings": {
"error" : "+101"
},
"refmt": 3
}

It is pretty simple, but let explain them:

  • name: your projects name or more used for if you make a library
  • version: What version your app is
  • sources: Configuration for Bucklescript where to look for the code
  • package-specs: In which format the code should be outputted
  • suffix: Which fileending the files should have after compilation
  • bs-dependencies: if you used any dependencies from redex.github.io
  • warnings: Here you can turn on/off errors
  • refmt: What version of the ReasonML formatter that is running

With that file out of the way, you need only to add bs-platform to your npm dependencies. If you don’t have a package.json, you can quickly add one with npm init .

npm i --only-dev bs-platform

and then you can add these commands to your package.json

“clean”: “bsb -clean-world”,
“build”: “bsb -make-world”,
“watch”: “bsb -make-world -w”,
  • clean removes any potentially left over files from previous build
  • build compiles all the code once
  • watch continuesly build whenever a change has happend in the source folder

When you installed bs-platform it added a lib folder. Add that folder to .gitignore .

To test this, add a file at ./src/main.re and put Js.log("Hello"); and run npm run watch . You should see a file called ./src/main.bs.js with

// Generated by BUCKLESCRIPT VERSION 4.0.3, PLEASE EDIT WITH CARE
'use strict';
console.log("Hi");
/*  Not a pure module */

Success

And you are done! This is what you need and now you know how to get started without a boilerplate