First steps using Reason with BuckleScript

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet

At it's core BuckelScript is a backend for the OCaml compiler to emits JavaScript. Luckily it support not only OCaml, but also ReasonML syntax out of the box. In addition BuckleScript comes with a whole toolchain to make our lifes easier.

In this lesson we will setup the tooling to create and build a new project. We explore it's structure and add a new module.

Instructor: [00:02] We install BuckleScript using npm install. Reason can compile to native binaries like code and JavaScript.

[00:08] In this example, we quickly run through how to set up your first Reason JavaScript project using the BuckleScript platform. BuckleScript ships with the CLI tool BSB.

[00:18] It allows us to create a new project, and ships with a couple of themes. We're going to choose the basic Reason theme.

[00:26] The project contains a package.json file and a node models directory, pretty standard for a JavaScript project. What's special, though, is a dsconfig.json file, which allows you to configure BuckleScript specifically to this project.

[00:42] Going through all the possible options and configurations is probably best to be explained in a separate course. We're instead going to focus on getting our first output with this default configuration.

[00:53] This example comes with only one demo.re file. We delete it and create a main.re file instead. Inside the file, we get started with printing a Hello World. Now we run npm run build.

[01:08] This will compile each Reason file to a matching JavaScript file, which means we can run it with Node or in the browser. As you can see, printing worked as expected.

[01:20] BuckleScript ships with a JS library that has a couple of utilities like Log. Log is quite useful, as it allows us to print larger structures and debug them.

[01:30] As a next step, we have the module math.re with an add function. In main, we can use math then. If you build again and run main, you can see that using the math module worked out of the box.

[01:49] By the way in case you don't want to build manually all the time, you can use npm start, which will watch for file changes and compile accordingly. Now you're ready to get started with your first Reason project.