ml-in-barcelona / jsoo-react

js_of_ocaml bindings for ReactJS. Based on ReasonReact.
https://ml-in-barcelona.github.io/jsoo-react
MIT License
138 stars 19 forks source link

Allow consumers to decide how React is provided #48

Closed jchavarri closed 3 years ago

jchavarri commented 3 years ago

Originated by #47.

There is no good reason why the require() calls are part of the library dune files. Consumers can decide how React will be provided, as long as it is on the globals that are expected to be read.

Webpack

Can add (javascript_files react.js react-dom.js) to their application dune files, and then require inside (or import, whatever they choose).

UMD

Can add <script> tags before loading the js file generated by jsoo (what this PR shows)

@tmattio Does this go in the right direction? If yes, I think I could just remove the __LIB__ prefixes (which never convinced me tbh 😄 ) so that there's no need to alias anything.

tmattio commented 3 years ago

Thanks a lot for looking into it @jchavarri!

That completely removes the overhead I was mentioning in the issue, so exactly what I thought 🎉

For reference, here's a dune configuration I tried that uses both the Webpack setup and the Jsoo-only setup:

(executable
 (name index)
 (modules (:standard \ index_dev))
 (modes js)
 (libraries jsoo-react.lib my_lib)
 (preprocess
  (pps jsoo-react.ppx)))

(executable
 (name index_dev)
 (modules (:standard \ index))
 (modes js)
 (js_of_ocaml
  (javascript_files react.js react-dom.js))
 (libraries jsoo-react.lib my_lib)
 (preprocess
  (pps jsoo-react.ppx)))
jchavarri commented 3 years ago

thanks for the examples @tmattio ! I added some instructions to set up the library with or without Webpack in the readme: https://github.com/jchavarri/jsoo-react/blob/bdecac1d9685afdc705fbd2c2c9d9e49a84cda48/README.md#existing-project

Gonna merge this now, but if you think there are better ways to solve this, please let me know :)