Skip to content

Latest commit

 

History

History
151 lines (132 loc) · 2.36 KB

Get_started.adoc

File metadata and controls

151 lines (132 loc) · 2.36 KB

Get Started

First example

  • Create a directory called hello and create bsconfig.json and package.json as below:

    bsconfig.json
    {
        "name" : "hello",
        "sources" : { "dir" : "src"}
    }
    package.json
    {
        "dependencies": {
            "bs-platform": "1.3.3" // (1)
        },
        "scripts" : {
            "build" : "bsb",
            "watch" : "bsb -w" // (2)
        }
    }
    1. Version should be updated accordingly

    2. Watch mode when developing

  • Create src/main_entry.ml as below:

    src/main_entry.ml
    let () =
      print_endline "hello world"
  • Build the app

    npm run build

Now you should see a file called lib/js/src/main_entry.js generated as below:

lib/js/src/main_entry.js
// GENERATED CODE BY BUCKLESCRIPT VERSION 1.0.1 , PLEASE EDIT WITH CARE
'use strict';


console.log("hello world");

/*  Not a pure module */ // (1)
  1. The compiler detects this module is impure due to the side effect.

User can also run watch mode to see changes instantly while editing.

npm run watch
Tip
The working code is available here:

An example with multiple modules

Now we want to create two modules, one file called fib.ml which exports fib function, the other module called main_entry.ml which will call fib.

  • Create a directory fib and create files bsconfig.json and package.json

    bsconfig.json
    {
        "name" : "helo",
        "sources" : { "dir" : "src"}
    }
    package.json
    {
        "dependencies": {
            "bs-platform": "1.3.4"
        },
        "scripts" : {
            "build" : "bsb",
            "watch" : "bsb -w"
        }
    }
  • Create file src/fib.ml and file src/main_entry.ml

    src/fib.ml
    let fib n =
      let rec aux n a b =
        if n = 0 then a
        else
          aux (n - 1) b (a+b)
      in aux n 1 1
    src/main_entry.ml
    let () =
      for i = 0 to 10 do
        Js.log (Fib.fib i) (* <1> *)
      done
    1. Js module is a built-in module shipped with BuckleScript

  • Build the app

    npm install
    npm run build
    node lib/js/src/main_entry.js

If everything goes well, you should see the output as below:

1
1
2
3
5
8
13
21
34
55
89