Skip to content

Conversation

jchavarri
Copy link
Contributor

This patch is the result of an exploration to bring back the load_modules API, which was recently disabled.

The proposed approach is to double down on the usage of js_of_ocaml pseudo file system to load these modules. As can be seen, the implementation of load_modules API for the playground is really simple, just a couple of calls to Js.create_file to load both cmi and cmj files into jsoo pseudo file system.

Advantages of using pseudo file system

  • Because we rely on js_of_ocaml on both "sides" —to consume the modules in the browser, and also to generate them as can be seen in section below— , it is highly unlikely this approach will break, as that would mean js_of_ocaml pseudo file system would be broken.
  • Using jsoo pseudo-file system removes the need to update references to compiler functions like Env.Persistent_signature.load or Js_cmj_load.load_unit, it's just files all the way down.
  • Future proof: From what I discussed with @bobzhang, it is very unlikely that bsb will remove support from reading module binaries from files in the future, regardless how the recently added in-memory loading evolves.
  • No more need for hacks to trick jsoo into skipping utf-16=>utf-8 conversions

Converting from traditional cmi/cmj files to browser-ready files

To complete the process of being able to compile code that references 3rd party BuckleScript libs on the browser, the plan is to have another small program that allows to create "browser-ready cmijs" from node_modules folder, a proof of concept of what this package could look like can be seen in https://github.com/jchavarri/bs-playground-bundler.

The generation of resulting scripts relies on js_of_ocaml pseudo file system to convert the binary representation of cmis into the utf-16 strings that will be loaded from the browser.

Remaining work

If this PR gets approved and merged, I will proceed then with other tasks to clean up / update other points:

  • Remove support for -playground flag in cmij_main
  • Port changes to jsoo_refmt_main

@jchavarri
Copy link
Contributor Author

jchavarri commented May 16, 2020

cc @hhugo In case you are interested, or have any pointers on how to do things better :)

Also, huge thanks for all the work you and all other js_of_ocaml contributors did on the pseudo file system. It's just amazing! 💙

@bobzhang bobzhang merged commit 5be1d05 into rescript-lang:master May 17, 2020
@jchavarri jchavarri deleted the load-modules-3 branch May 23, 2020 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants