BuckleScript provide some project templates, to help people get started on board as fast as possible.
npm install -g bs-platform && bsb -init hello && cd hello && npm run build
First, it installs bs-platform
globally, then we use the command line tool bsb
to set up a sample project named hello
, then we run the build.
The output project layout is similar as below
hello>tree -d .
.
|---.vscode
├── lib
│ ├── bs
│ │ └── src
│ └── js
│ └── src
├── node_modules
│ └── bs-platform -> ...
└── src
To enter watch mode, you can run npm run watch
,
then you can edit src/demo.ml
and see it compiled on the fly, and changes are updated on lib/js/src/demo.js
If you happen to use VSCode as editor, we provide .vscode/tasks.json
as well, type Tasks>build
, it will enter watch mode, the great thing is that VSCode comes with
a Problems
panel which has a much nicer UI.
Currently there are not too many project templates, you can contribute more project templates here here,
-
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.7.0" // (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.7.0" }, "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