ocsigen / js_of_ocaml

Compiler from OCaml to Javascript.
http://ocsigen.org/js_of_ocaml/
Other
950 stars 185 forks source link

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

Closed xvw closed 1 year ago

xvw commented 1 year ago

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 1 year ago
xvw commented 1 year ago

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 1 year ago

Could you verify that https://github.com/ocsigen/js_of_ocaml/pull/1466 fixes your underlying issue ?

I could identify 3 issues:

xvw commented 1 year ago

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 1 year ago

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

hhugo commented 1 year ago

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

xvw commented 1 year ago

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