wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Default imports

Open philip-peterson opened this issue 5 years ago • 6 comments

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.

philip-peterson avatar Jul 30 '20 03:07 philip-peterson

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

alexcrichton avatar Jul 30 '20 14:07 alexcrichton

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;
}

philip-peterson avatar Jul 31 '20 03:07 philip-peterson

Have you tried using static pi: f64 perhaps?

alexcrichton avatar Aug 03 '20 15:08 alexcrichton

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

djmaze avatar Aug 26 '24 16:08 djmaze

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;
}

djmaze avatar Aug 26 '24 16:08 djmaze

The workaround now seems to be broken in 0.2.100 by https://github.com/rustwasm/wasm-bindgen/pull/4329#issuecomment-2672840111

dylanowen avatar Feb 21 '25 01:02 dylanowen