Dune: how to depend and include a generated .js file into a .ml

Hi,

I have a web server library which should embed a js_of_ocaml-generated .js file. Embedding is done using ppx_blob. But I can’t find the way to:

  • tell dune that this library depends on this generated .js file,
  • refer to this file to pass it to ppx_blob.

My current dune file looks like

(library
 (name toto_server)
 (public_name toto_server)
 (wrapped true)
 (flags ...)
 (libraries ...)
 (modules ...)
 (preprocess (pps ... ppx_blob))
 (preprocessor_deps
   (file toto_server_client_js)
 )
)

(executable
  (modes js)
  (name toto_server_client_js)
  (flags ...)
  (libraries ...)
  (modules toto_server_client_js)
  (preprocess (pps js_of_ocaml-ppx))
)

By now, dune complains about the preprocess rule of the library with:

Error: No rule found for server/toto_server_client_js

And I guess that ppx_blob will not find the file if I simply put

let server_client_js = [%blob "toto_server_client.js"]

What’s the right way to achieve this ?

1 Like

Hi @Zoggy,

I think the only problem with your current setup is the name of the files. When dune creates a javascript file using (modes js) it gets the extension .bc.js which is what you need to depend on for the preprocessor i.e.

(library
 (name toto_server)
 (public_name toto_server)
 (wrapped true)
 (flags ...)
 (libraries ...)
 (modules ...)
 (preprocess (pps ... ppx_blob))
 (preprocessor_deps
   (file toto_server_client_js.bc.js)
 )
)

Then in the use of ppx_blob you can have:

let server_client_js = [%blob "toto_server_client_js.bc.js"]

That should work :))

1 Like

Of course ! Thanks :slight_smile:

2 Likes

ppx_blob is good for a single file, but I’ve found ocaml-crunch to be easier to work with, since it can scoop up a whole directory (e.g. a set of static web assets) and provide you with those files via a convenient lookup module. n.b. the dune example in its repo that shows how to set up a rule for regenerating the lookup module when the target directory’s contents change: ocaml-crunch/dune at master · mirage/ocaml-crunch · GitHub

3 Likes