Do not generate bindings for dependencies
I have two crates - reconfix and cdsl. The former one depends on the later one:
reconfix
|- cdsl
cdsl crate can be used as a Rust crate and it also contains bindings gated via cfg(target_arch = "wasm32"). NPM package with proper bindings can be generated.
Now I have a reconfix crate, which does use cdsl as a dependency. But when I generate bindings for the reconfix crate, all bindings (JS functions) from the cdsl crate are in the reconfix bindings as well.
Example:
cdslcontains JS functiongenerate_uireconfixhascdslas a dependencyreconfixdoes not contain JS functiongenerate_ui- create NPM package / generate bindings and
reconfixdoes containgenerate_uifunction, which is something I do not want, it behaves like implicitpub use ...reexports
Is there a way how to disable this behavior? For now, I introduced disable-wasm-bindings feature in the cdsl crate. But it's nothing I like or I consider as a good solution. What's the recommended way? Should I split cdsl in two crates, like cdsl (without wasm bindings) and cdsl-wasm crate, which will act as a bridge between Rust & JS only?
Thanks for the report!
This is somewhat nontrivial to do right now, as the transitive behavior of wasm-bindgen is actually intentional. I suspect, though, that like with normal Rust libraries compiled for C it's "best practice" to avoid #[wasm_bindgen] in dependencies like it's best practice to avoid #[no_mangle] unless it's for technical reasons.
We could explore an option like this perhaps, but the information isn't currently available to the CLI tool. Additionally it's not clear I think if we want this long-term because it may end up breaking intermediate dependencies accidentally in some cases.
Thanks for the reply. We have something like you described, but in the opposite way - enabled by default, feature can disable it. The reason is wasm-pack & this issue. Once 0.6.1 is released, we will modify it, so, by default #[wasm_bindgen] will be disabled and one will have to enable it with a feature (disable-wasm-bindings will become enable-wasm-bindings).
Ah ok makes sense! In that case I think it's probably best to fix it via that, but otherwise this seems reasonable to have as a feature request at least (even if it's not the default behavior) so let's leave it open
Hi, I have a related question. Is it possible to explicitly make bindings for something in a dependency crate from the dependent crate? It would be something like
#[wasm_bindgen]
pub use dep_crate::SomeStruct;
This way, the dependency doesn't need to know anything about wasm-bindgen, and the dependent crate is working like a facade. Now it's possible only by creating a wrapper struct with all required methods by hand. And it won't work for enums at all. Generally speaking, it would be a much better experience to have exports only in the main crate and only in the main module, as the splicing of exports is quite confusing. Is it theoretically possible to have something like this?
Unfortunately, no. The macro runs over the AST, so it can't resolve that and figure out how to generate bindings, it needs the source code itself.
I see, thanks for the clarification.
CARGO_CRATE_NAME and CARGO_PRIMARY_PACKAGE could be used to do this.
The question really is how to design an interface to control this. E.g. a CLI flag or something that allows more fine-grained control.
See also https://github.com/rust-lang/rust/issues/98449.