0

Following example https://github.com/rustwasm/wasm-bindgen/tree/master/examples/import_js works fine. Next step I try to import a js-module with 2 levels of modules. Let MyClass instead be referenced with mylib.part1.MyClass.

In rust wasm-bindgen file I try to import with:

#[wasm_bindgen(module = "/defined-in-js.js")]
extern "C" {

    type MyClass;

    #[wasm_bindgen(constructor, js_namespace = mylib.part1)]
    fn new() -> MyClass;
}

This gives build error:

error: expected `,`
  --> src/lib.rs:13:53
   |
13 |     #[wasm_bindgen(constructor, js_namespace = mylib.part1)]
   |                                                     ^

Is it possible to do the import in wasm-bindgen? Alternative some workaround solution with re-export in js without the 2 module levels (tried, but didn't manage).

Jonas Bojesen
  • 855
  • 1
  • 8
  • 22

1 Answers1

0

Actually documented here https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/on-js-imports/js_namespace.html

So becomes:

#[wasm_bindgen(module = "/defined-in-js.js")]
extern "C" {

    #[wasm_bindgen(js_namespace = ["mylib", "part1"], js_name = MyClass)]
    type MyClass;

    #[wasm_bindgen(constructor, js_namespace = ["mylib", "part1"], js_name = MyClass)]
    fn new() -> MyClass;
}
Jonas Bojesen
  • 855
  • 1
  • 8
  • 22