rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.82k stars 1.08k forks source link

JS snippets for nodejs target #1525

Open ibaryshnikov opened 5 years ago

ibaryshnikov commented 5 years ago

Initially nodejs target was not supported when js snippets were implemented. Is it the time to add support for nodejs?

There is already code like this in js-sys tests:

#[wasm_bindgen(module = "tests/wasm/Iterator.js")]

https://github.com/rustwasm/wasm-bindgen/blob/master/crates/js-sys/tests/wasm/Iterator.rs#L5

And it somehow works. But when I tried to compile my own module, it compiled and failed in runtime because the .js file was not copied to the pkg folder. How did you make the tests work?

fitzgen commented 5 years ago

IIRC, because node only has experimental support for modules that you have to enable with a special flag at startup, we don't support modules + js snippets on node unless you set the WBINDGEN_I_PROMISE_JS_SYNTAX_WORKS_IN_NODE env var, which is just for our own internal testing really.

To properly support js snippets on node, we want to rewrite the exports to common js.

ibaryshnikov commented 5 years ago

@fitzgen nice, thank you for the response! so it's already on your roadmap?

fitzgen commented 5 years ago

I would say we know what needs to be done, but have not prioritized it or come up with any target release dates or anything like that. If you want to talk about it next WG meeting, feel free to add to the schedule!

kellytk commented 5 years ago

What would help move this issue forward, at least for the no-modules target?

alexcrichton commented 5 years ago

The original RFC has a bit more information, but there's not much more other than "this needs implementing" for the node/no-modules targets. The "this" in terms of what needs to happen is specified in the RFC.

kellytk commented 5 years ago

I made https://github.com/yewstack/yew-wasm-pack-minimal and used Rollup with the intention of removing it when wasm-pack can be used exclusively. If it could be modified in any way to assist with implementation please feel welcome to open an issue.

PhilippGackstatter commented 3 years ago

Is there an update on snippets for nodejs?

Does this require "only" implementation and if so, is there a plan for how to do so? Specifically, it seems like there hasn't been a decision on which JS parser to use. I could imagine implementing this, but it depends on how much time it would take.

We currently want to use websocket support from rust-libp2p that imports a JS snippet to implement it. It seems to be fixed by removing the export keyword from the websocket_transport function and adding the line

exports.websocket_transport = websocket_transport;

I assume, however, that a more universal solution would not be as straightforward.