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

Compiling without Webpack #47

Closed tmattio closed 3 years ago

tmattio commented 3 years ago

Hi @jchavarri!

I've been setting up jsoo-react in my project and thought I'd give some feedback on the setup 🙂

To give you some context, I'm using ocaml-crunch to generate OCaml modules for my static content. It simplifies the deployment drastically: with this setup, I can just ship an executable and don't have to worry about storing static files. To serve the React app, I am embedding the compiled Js in an OCaml module and serve it from one of the endpoints of the server.

Now, all of this is pretty much automated with Dune configurations, however, the React app first needs to be processed by Webpack (or another bundler), because the code generated by Js_of_ocaml imports react.

This dependency on Webpack adds a lot of overhead to the setup, and I'm wondering if it would be possible to get rid of the imports in the generated Js code.

The HTML page could potentially get the react library from their CDN:

<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>

And the compiled Js could just assume that React is available.

I'm not very familiar with Js_of_ocaml, so I have no idea if that's feasible, but if it is, it would have some nice benefits:

What do you think?

jchavarri commented 3 years ago

I think it's a great idea! I wouldn't like to remove Webpack support, because it might be interesting in the future (for things like lazy loading) and I think some folks will appreciate that jsoo-react has some guidelines on how to be used with Webpack.

But otherwise, I love the simplification and build times improvements :) Your proposal made me realize we have no good reason to assume anything about how React gets provided to final bundles, we can just do nothing inside the library and resolve the globals in userland. I created a short PR in #48 to keep discussing over something tangible, let me know what you think!

jchavarri commented 3 years ago

Fixed in #48.