Skip to content

Latest commit

 

History

History
67 lines (45 loc) · 2 KB

NPM-Support.adoc

File metadata and controls

67 lines (45 loc) · 2 KB

Built in NPM support

Build an OCaml library as a npm package

BuckleScript build system has built in support for NPM packages, please checkout the section about the bsb for NPM support.

Note

This section covers some basics of how NPM is supported internally, normal users are safe to skip this section.

BuckleScript extends the OCaml compiler options with several flags to provide a better experience for NPM users.

In general, you are expected to see two kinds of build artifacts, the generated JS files and metadata which your OCaml dependencies rely on.

Since CommonJS has no namespaces, to allow JS files to live in different directories, we have a flag

bsc.exe -bs-package-name $npm_package_name -bs-package-output modulesystem:path/to/your/js/dir -c a.ml

By passing this flag, bsc.exe will store your package_name and relative path to package.json in .cmj files. It will also generate JS files in the directory you specified. You can, and are encouraged to, store JavaScript files in a hierarchical directory.

For the binary artifacts (Note that this is not necessary if you only want your libraries to be consumed by JS developers, and it has benefit since end users don’t need these binary data any more), the convention is to store all *.cm data in a single directory package.json/lib/ocaml and Javascript files in a hierachical directory like package.json/lib/js

To use OCaml library as a npm package

If you follow the layout convention above, using an OCaml package is pretty straightforward:

bsc.exe -I path/to/ocaml/package/installed -c a.ml

Together

Your command line would be like this:

bsc.exe -I path/to/ocaml/package1/installed -I path/to/ocaml/package2/installed  -bs-package-name $npm_package_name -bs-package-output commonjs:path/to/lib/js/ -c a.ml

Examples

Please visit https://github.com/bucklescript/bucklescript-addons for more examples.