Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for importing export default #2403

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions guide/src/reference/attributes/on-js-imports/js_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,30 @@ extern "C" {
}
```

The `js_name` attribute can also be used in situations where a JavaScript module uses
`export default`. In this case, setting the `js_name` attribute to "default" on the
`type` declaration, and the [`js_class` attribute][jsclass] to "default" on any methods
on the exported object will generate the correct imports.


For example, a module that would be imported directly in JavaScript:

```javascript
import Foo from "bar";

let f = new Foo();
```

Could be accessed using this definition in Rust:

```rust
#[wasm_bindgen(module = "bar")]
extern "C" {
#[wasm_bindgen(js_name = default)
type Foo;
#[wasm_bindgen(constructor, js_class = default)]
pub fn new() -> Foo;
}
```

[jsclass]: js_class.html