From b79f5b2739885cc33d5b4a8ba5529e8ee8262fbc Mon Sep 17 00:00:00 2001 From: Jeremy Shoemaker Date: Mon, 4 Jan 2021 09:15:40 -0600 Subject: [PATCH] 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`. --- .../attributes/on-js-imports/js_name.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/guide/src/reference/attributes/on-js-imports/js_name.md b/guide/src/reference/attributes/on-js-imports/js_name.md index 69d89e5bc63..840fb4f57e5 100644 --- a/guide/src/reference/attributes/on-js-imports/js_name.md +++ b/guide/src/reference/attributes/on-js-imports/js_name.md @@ -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