-
Create a directory called
hello
and createpackage.json
as below:package.json{ "dependencies": { "bs-platform": "0.9.3" // (1) }, "scripts" : { "build" : "bsc -c main_entry.ml" } }
-
Version should be updated accordingly
-
-
Create
main_entry.ml
as below:main_entry.mllet () = print_endline "hello world"
-
Build the app
npm run build
Now you should see a file called main_entry.js
generated as below:
main_entry.js
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE
'use strict';
console.log("hello world");
/* Not a pure module */ (1)
-
The compiler analyze this module is impure due to the side effect
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 created a filepackage.json
package.json{ "dependencies": { "bs-platform": "0.9.4" }, "scripts" : { "build" : "bsc -c -bs-main main_entry.ml" // (1) } }
-
here
-bs-main
option tells the compiler compilemain_entry
module and its dependency accordingly
-
-
Create file
fib.ml
and filemain_entry.ml
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
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 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