Skip to content

Commit b79f5b2

Browse files
authored
Add documentation for importing export default (#2403)
Added some documentation to the `js_name` page about using it to import JavaScript modules that use `export default`.
1 parent d6825ad commit b79f5b2

File tree

1 file changed

+26
-0
lines changed
  • guide/src/reference/attributes/on-js-imports

1 file changed

+26
-0
lines changed

guide/src/reference/attributes/on-js-imports/js_name.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,30 @@ extern "C" {
5858
}
5959
```
6060

61+
The `js_name` attribute can also be used in situations where a JavaScript module uses
62+
`export default`. In this case, setting the `js_name` attribute to "default" on the
63+
`type` declaration, and the [`js_class` attribute][jsclass] to "default" on any methods
64+
on the exported object will generate the correct imports.
65+
66+
67+
For example, a module that would be imported directly in JavaScript:
68+
69+
```javascript
70+
import Foo from "bar";
71+
72+
let f = new Foo();
73+
```
74+
75+
Could be accessed using this definition in Rust:
76+
77+
```rust
78+
#[wasm_bindgen(module = "bar")]
79+
extern "C" {
80+
#[wasm_bindgen(js_name = default)
81+
type Foo;
82+
#[wasm_bindgen(constructor, js_class = default)]
83+
pub fn new() -> Foo;
84+
}
85+
```
86+
6187
[jsclass]: js_class.html

0 commit comments

Comments
 (0)