rustwasm / wasm-bindgen

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

Default imports #2263

Open philip-peterson opened 4 years ago

philip-peterson commented 4 years ago

Motivation

It would be nice if there was a syntax for doing default import of modules. Many npm modules are distributed with the only export being the default export.

Proposed Solution

#[wasm_bindgen(module = "pi")]
extern "C" {
    #[wasm_bindgen(default_symbol)]
    pi: f64;
}

Alternatives

There may be another way to import a library such as pi, but I am new to wasm-bindgen and couldn't find anything in the docs that provides instructions.

alexcrichton commented 4 years ago

I believe this should work with js_name = default, does that work for you?

philip-peterson commented 4 years ago

Sorry, what would that look like syntactically? I tried this but it led to a syntax error:

#[wasm_bindgen(module = "pi")]
extern "C" {
    #[wasm_bindgen(js_name = "default")]
    pi: f64;
}
alexcrichton commented 4 years ago

Have you tried using static pi: f64 perhaps?

djmaze commented 2 weeks ago

Same problem here, also when trying to import a class (via type).

djmaze commented 2 weeks ago

Okay, got this working for a class by adding js_class:

#[wasm_bindgen(module = "/mymodule.esm.js")]
extern "C" {
    type MyClass;

    #[wasm_bindgen(constructor, js_class = "default")]
    fn new() -> MyClass;
}