Skip to content

Re-export imported JS modules, raw_modules, inline_js #1812

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

Closed
alexlapa opened this issue Oct 9, 2019 · 2 comments
Closed

Re-export imported JS modules, raw_modules, inline_js #1812

alexlapa opened this issue Oct 9, 2019 · 2 comments

Comments

@alexlapa
Copy link
Contributor

alexlapa commented Oct 9, 2019

Motivation

Use case:

lib.rs:

#[wasm_bindgen(module = "/src/error.js")]
extern "C" {
    #[wasm_bindgen(extends = Object, extends = Error)]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type MyError;

    #[wasm_bindgen(constructor)]
    fn new(message: &str) -> MyError;
}

#[wasm_bindgen]
pub fn get_my_err() -> MyError {
    MyError::new("msg")
}

/src/error.js:

export class MyError extends Error {
  constructor(message) {
    super(message);
    this.name = this.constructor.name;
  }
}

index.js

import {MyError} from "../../pkg/snippets/wasm-tests-2b0d4d4468a848de/src/error";

const rust = import('../../pkg');
rust
  .then(rust => {
        let foo = rust.get_my_err();
        console.log(foo instanceof MyError); // true
  });

It is not convenient to import MyError this way.

Proposed Solution

Reexport imported snippets. I.e.:
pkg/index.js:

import { MyError } from './snippets/wasm-tests-2b0d4d4468a848de/src/error.js';
import * as wasm from './index_bg.wasm';

export { MyError };
...
@alexcrichton
Copy link
Contributor

Thanks for the report! The purpose of the snippets feature, however, is for local private code to be imported into a project. If JS code needs to import JS code then you'll likely want to set up an NPM/JS package to share so you can import from that in both JS locations.

@alexlapa
Copy link
Contributor Author

@alexcrichton ,

Waiting for rustwasm/wasm-pack#606 then. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants