Skip to content

Exports with #[wasm_bindgen(js_name = default)] gets exported as fn _default #3929

Closed
@nicklaswj

Description

@nicklaswj

Describe the Bug

I'm trying to create a plugin for the javascript project EventCatalog in Rust with wasm_bindgen.
Their plugin system is fairly simple and it works by:

  1. Adding your plugin as a nodejs package.
  2. Place the name of the added package in a config file.
  3. When the EventCatalog gets build, it require your package/plugin that's defined in the config file and calls the default exported function, i.e. whats exported with export default ... in the main module of the added nodejs package. For the curios you can find the code for the plugin loading code here.

I tried to create my plugin with the following code:

#[wasm_bindgen(js_name = default)]
pub fn init_my_plugin() {
    ...
}

Unfortunately the above function gets exported as _default instead of default and the generated JS code (with --target nodejs) looks like this:

module.exports._default = function() {
    wasm._default();
};

which means that EventCatalog fails to call my plugin. This issue is very much related to #3012 and is just the other side of the coin. I think it's reasonable that if you can import fn default, you should also be able to export fn default.

Expected Behavior

The following Rust function

#[wasm_bindgen(js_name = default)]
pub fn init_my_plugin() {
    ...
}

should be exported as defaultand not _defaultand the generated javascript code should be:

module.exports.default = function() {
    wasm.default();
};

Additional Context

I'll create a PR soon (PR created #3930)

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