@cem2ran/ppx_style

A tiny ppx for learning purposes

Usage no npm install needed!

<script type="module">
  import cem2ranPpxStyle from 'https://cdn.skypack.dev/@cem2ran/ppx_style';
</script>

README

style-ppx

A project that includes the minimum configuration for a ppx called ppx_style, a project that uses Reason and Esy.

ppx_style implements a ppx that applies the record or object to the make function defined in the Style module.

So, the code:


module Style = {
    let make = (~backgroundColor, ~width, ~height, ()) = ...;
};

// Record style
Style({backgroundColor: "papayawhip", width: 42.->dp, height: 42.->dp});
// Object style
Style({"backgroundColor": "papayawhip", "width": 42.->dp, "height": 42.->dp});
// dp is also now the default for float values for attributes of "size" type
Style({
  backgroundColor: "papayawhip",
  width: 42., // <-- look ma no dp!
  height: 42.->dp,
  flex: 1., // <-- ppx does not touch this because it isn't a "size" attribute
});

Is transformed into:

make(~backgroundColor="papayawhip", ~width=42.->dp, ~height=42.->dp, ());

Files and folders

The example contains a couple of different targets that will be handled by dune (an OCaml build system) to use the ppx in different projects:

  • The library: located under lib folder. It is used directly by native projects, and indirectly by BuckleScript projects
  • The standalone binary: BuckleScript does not provide a way to compose multiple ppxs together, so each ppx gets called individually, getting a serialized version of the AST, using the -ppx compiler flag behind the scenes. This can be configured in BuckleScript projects by using the ppx-flags key in bsconfig.json (see "Examples" section below).

For this reason, ppx_style exposes an executable that can be consumed by BuckleScript projects.

Examples

The repo contains a couple of demo examples that show one way to consume the ppx from both BuckleScript and native environments.

Check the readmes on each example folder: