Git Product home page Git Product logo

commonjs_of_ocaml's Introduction

commonjs_of_ocaml

Import and export CommonJS modules in js_of_ocaml

Build Status

Working with Javascript CommonJS modules in js_of_ocaml can be difficult. commonjs_of_ocaml seeks to make things easier with a simple module and ppx syntax extension.

Installing / Getting started

You'll need opam to install this module.

opam install commonjs_of_ocaml

Alternatively, you can install from the repo itself

git clone https://github.com/AngryLawyer/commonjs_of_ocaml.git
cd commonjs_of_ocaml
opam pin add commonjs_of_ocaml .

Include commonjs and commonjs_ppx in your -packages switch with ocamlbuild and you're on your way.

You can require commonjs compliant Javascript packages into your OCaml files as follows:

let my_module = [%require "./my_module"]

Alternatively, if you're working in a situation where you want to provide a fallback if a commonjs loader is not available, or a module is not available:

let my_module = [%require_or_default "./my_module" my_fallback_variable]

This, internally, wraps a try block around the require statement, and is useful for libraries where users may be attaching their modules directly to the DOM window where they're not using a commonjs loader.

You can also expose your OCaml library to commonjs, for easy calling from native Javascript:

let () =
  CommonJS.export my_module

When you want to test your library, simply feed the js file produced by js_of_ocaml through a tool such as Browserify and your OCaml code should be bundled with its dependencies.

An example of importing and using the PPX is included in the tests directory.

Developing

The project uses oasis for building. You'll need js_of_ocaml installed to do this.

If you need to edit this project, clone the project:

git clone https://github.com/AngryLawyer/commonjs_of_ocaml.git
cd commonjs_of_ocaml
ocaml setup.ml -configure
ocaml setup.ml -build

To run the test suite:

ocaml setup.ml -configure --enable-tests
ocaml setup.ml -test

Why?

Packages such as Browserify work by looking for calls to require in the Javascript AST. The code js_of_ocaml produces is minified in such a way that it's difficult for tools to find dependencies it has.

By using a PPX, we can embed the raw string requiring the Javascript module.

Contributing

Fork the repo and make a pull request, and I'm more than happy to take a look!

Licensing

This project is MIT licensed.

commonjs_of_ocaml's People

Contributors

angrylawyer avatar gasche avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.