-
Create a directory called
hello
and createbsconfig.json
andpackage.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) } }
-
Version should be updated accordingly
-
Watch mode when developing
-
-
Create
src/main_entry.ml
as below:src/main_entry.mllet () = 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:
// GENERATED CODE BY BUCKLESCRIPT VERSION 1.0.1 , PLEASE EDIT WITH CARE
'use strict';
console.log("hello world");
/* Not a pure module */ // (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: |
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 filesbsconfig.json
andpackage.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 filesrc/main_entry.ml
src/fib.mllet 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.mllet () = for i = 0 to 10 do Js.log (Fib.fib i) (* <1> *) done
-
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