Skip to content

Imports with #[wasm_bindgen(js_name = default)] from a local module with a default export break when compiling to a library  #3012

Closed
@expenses

Description

@expenses

Describe the Bug

I'm trying to bind with a local module like so:

#[wasm_bindgen(module = "/module.js")]
extern "C" {
    #[wasm_bindgen(js_name = default)]
    fn init() {
        ...    
    }
}

where module.js looks like this:

function init() {...}
export default init;

Unfortunately, the resulting generated js bindings file for the rust library has an import that looks like this:

import { _default } from './snippets/module.js';
...
export default init;

Because module.js has a default export, { _default } doesn't match with anything and is broken.

Expected Behavior

What should be happening is this:

import { default as _default } from './snippets/module.js';

I've written a small patch that fixes this issue:

diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs
index 247792239..09d2f4e35 100644
--- a/crates/cli-support/src/js/mod.rs
+++ b/crates/cli-support/src/js/mod.rs
@@ -2223,6 +2223,12 @@ impl<'a> Context<'a> {
     }
 
     fn add_module_import(&mut self, module: String, name: &str, actual: &str) {
+        let mut name = name.to_string();
+
+        if name == "_default" {
+            name = "default".to_string();
+        }
+
         let rename = if name == actual {
             None
         } else {

However this is probably the wrong way to handle this edgecase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions