Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xvw opened this issue Jun 1, 2023 · 7 comments

Comments

@xvw
Copy link
Contributor

xvw commented Jun 1, 2023

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.

@xvw xvw added the enhancement label Jun 1, 2023
@xvw xvw changed the title [FEATURE REQUEST] [FEATURE REQUEST] Allow JavaScript files to be linked without dead-code elimination Jun 1, 2023
@hhugo
Copy link
Member

hhugo commented Jun 5, 2023

@xvw
Copy link
Contributor Author

xvw commented Jun 5, 2023

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
Copy link
Member

hhugo commented Jun 5, 2023

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
Copy link
Contributor Author

xvw commented Jun 5, 2023

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
Copy link
Contributor Author

xvw commented Jun 5, 2023

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

@hhugo
Copy link
Member

hhugo commented Jun 6, 2023

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

@xvw
Copy link
Contributor Author

xvw commented Jun 7, 2023

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

@xvw xvw closed this as completed Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants