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

Improve bundle size by using arrays instead of objects for uppercase element props #91

Open jchavarri opened 2 years ago

jchavarri commented 2 years ago

Right now, we use a JavaScript object to convert from labelled arguments to a single value, so that the component implementation can receive them from ReactJS:

https://github.com/ml-in-barcelona/jsoo-react/blob/d7290d38fddfdfb0da41aa2e71f0974a83061ec7/ppx/ppx.ml#L563-L569

However, this approach leads to generated code like:

function make$2(className, onClick, disabled, children, key, param) {
  var
  _a_ = caml_call2(Stdlib_Option[7], caml_jsstring_of_string, key),
  _b_ =
  {
    "key": caml_call1(Js_of_ocaml_Js[6][9], _a_),
    "children": children,
    "disabled": disabled,
    "onClick": onClick,
    "className": className
  };
  return caml_call2(React[12], make$1, _b_)
}

This leads to larger bundle size as minifiers are not able to remove the string constants in the props object.

There might be a work around that involves passing an array instead of an object. The only constraint is that this array has to be written and read in a consistent order, but because ppx has full knowledge about this, it should be feasible.

jchavarri commented 2 years ago

One downside of the suggested improvement is that React Dev Tools won't be showing the props as an object as it does now. Maybe this could be an option that activates or not depending on the "developer" or "release" profile modes 🤔