ocsigen/js_of_ocaml

[FEATURE REQUEST] Allow JavaScript files to be linked without dead-code elimination

xvw opened this issue · 7 comments

xvw commented

Hello !
I'm perfectly aware that dead-code elimination is a desirable feature, but in some cases it can be problematic.
For example, as part of a project (Yourbones), we depend on libraries available via NPM, so we use Webpack to build a JavaScript file which exports the objects we need in our OCaml library, to apply the various polyfills so that in the end we only have a single file, in this case, beacon_stubs.js which we can link with the dune directive javascript_file ....
Unfortunately, the code produced by Webpack cannot be interpreted by JSOO's JavaScript parser.

/home/xvw/Projects/perso/yourbones/_opam/bin/js_of_ocaml: Error: cannot parse file "/home/xvw/Projects/perso/yourbones/_build/default/lib/beacon/stubs/stubs.js" (orig:"/home/xvw/Projects/perso/yourbones/_build/default/lib/beacon/stubs/stubs.js" from l:2, c:311932)

In the development context, it's enough to include the generated file in the HTML before including the file compiled by JSOO. However, in the context of distributing a library, I think it could be useful to be able to link Javascript files in the runtime, without trying to parse them and extract export information. This would make it possible to work relatively well with the JavaScript ecosystem, which requires files to be pre-bundled when targeting the browser.

hhugo commented
  • what is the js parser chocking on ? is it related to #1396 ?
xvw commented

what is the js parser chocking on ?

Hard to say, here's the error I get:

/home/xvw/Projects/perso/yourbones/_opam/bin/js_of_ocaml: Error: cannot parse file "/home/xvw/Projects/perso/yourbones/_build/default/lib/beacon/stubs/beacon_stubs.js" (orig:"" from l:0, c:0)

And here is the formatted JS file :
https://gist.github.com/xvw/1aab29f38b4accfc26018961b92e87c5

The file is created with webpack, and uses a very low preset (es5) so I don't think there are any imports/exports.

hhugo commented

Could you verify that #1466 fixes your underlying issue ?

I could identify 3 issues:

  • Error reporting was incorrect when caused by a dummy token (virtual_semicolon) because it has no location.
  • Logic for restricted token (return, continue, break, throw, yield) was wrong when used as identifier (e.g. x.return )
  • Renaming variable was incorrect in some case: method inside class, nested block defining/using the same variable name with let and const.
xvw commented

Thanks for the quick reply!
I have try to :

opam pin add js_of_ocaml https://github.com/ocsigen/js_of_ocaml.git#fix-for-x
opam install js_of_ocaml

Who displays me, if I do opam list | grep Js_of_ocaml:

js_of_ocaml             5.2.0        pinned to version 5.2.0 at git+https://github.com/ocsigen/js_of_ocaml.git#fix-for-x
js_of_ocaml-compiler    5.2.0        Compiler from OCaml bytecode to JavaScript
js_of_ocaml-lwt         5.2.0        Compiler from OCaml bytecode to JavaScript
js_of_ocaml-ppx         5.2.0        Compiler from OCaml bytecode to JavaScript

So I guess I'm in a good state. However, if I relaunch dune build:

/home/xvw/Projects/perso/yourbones/_opam/bin/js_of_ocaml: Error: cannot parse file "/home/xvw/Projects/perso/yourbones/_build/default/lib/beacon/stubs/beacon_stubs.js" (orig:"" from l:0, c:0)

Still the same issue using the formatted code.


EDIT:
using :

opam pin add js_of_ocaml-compiler https://github.com/ocsigen/js_of_ocaml.git#fix-for-x
opam install js_of_ocaml-compiler

It works! (With formatted code, when the code isn't formatted, it's minified, the error is the same, but that's a detail).

Thanks a lot!

xvw commented

Here is an example of a minified version of the stubs : https://gist.github.com/xvw/e8b29b71758e77223ec894cf31c70937

hhugo commented

I pushed 2 more fixes in #1466. Can you check again with the minified version ?

xvw commented

It seems working. Thanks a lot for the quick fix!