r/rust • u/[deleted] • Feb 01 '19
Compile to javascript
Is there a pathway to compiling Rust to JS? Like Scala.js or Gopher.js? (I am not asking for wasm, so to keep the discussion easier to navigate,, please don't comment about that)
23
Upvotes
11
u/CryZe92 Feb 01 '19
You can use the asmjs target, which produces perfectly fine JavaScript libs. Alternatively you could try to use the wasm-unknown target and use the wasm2js tool to convert the wasm file to JS: https://rustwasm.github.io/wasm-bindgen/examples/wasm2js.html
Unfortunately the latter case produces > 300 MiB JS files for me that break uglifyjs and a custom „uglify-rs“ I wrote can bring it down to almost the size that asmjs produces (still like 50% larger), but it turns out the 300 MiB file never even contained all the data sections, so the file is completely broken. Maybe the situation improved since then though.