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

Can't figure out how to import JavaScript modules #20

Open
SillyFreak opened this issue Jan 1, 2019 · 4 comments
Open

Can't figure out how to import JavaScript modules #20

SillyFreak opened this issue Jan 1, 2019 · 4 comments

Comments

@SillyFreak
Copy link

I've added the following code to lib.rs (see here):

#[wasm_bindgen(module = "./helper")]
extern "C" {
    pub fn foo();
}

(plus a call to foo in run()) and tried to figure out where to place helper.js, but everywhere I've tried (project root, js/, crate/, crate/src/, crate/pkg/) doesn't work. I have the feeling the file is ignored entirely; should this be working, and if so, how?

I'm on Linux and using npm run start for my tests.

@sendilkumarn
Copy link
Member

If you have placed in the js/ folder

../../js/helper

@SillyFreak
Copy link
Author

I recreated the project using these commands

npm init rust-parcel hello-web2
cd hello-web2
npm install

then applied the following changes (i.e. add the js/helper.js file, add the wasm_bindgen and a call to foo)

diff --git a/crate/src/lib.rs b/crate/src/lib.rs
index 6b121e9..391a973 100644
--- a/crate/src/lib.rs
+++ b/crate/src/lib.rs
@@ -24,9 +24,16 @@ cfg_if! {
     }
 }

+#[wasm_bindgen(module = "../../js/helper")]
+extern "C" {
+    pub fn foo();
+}
+
 // Called by our JS entry point to run the example
 #[wasm_bindgen]
 pub fn run() -> Result<(), JsValue> {
+    foo();
+
     // Use `web_sys`'s global `window` function to get a handle on the global
     // window object.
     let window = web_sys::window().expect("no global `window` exists");
diff --git a/js/helper.js b/js/helper.js
new file mode 100644
index 0000000..d8791a0
--- /dev/null
+++ b/js/helper.js
@@ -0,0 +1,3 @@
+export const foo = () => {
+    console.log("foo");
+};

and then started it using npm run start. The build is successful (as it was with my other tests), but it still gives me the same result. In Chrome:

localhost/:1 Uncaught (in promise) TypeError: WebAssembly Instantiation: Import #0 module="../../js/helper" error: module is not an object or function

@sendilkumarn
Copy link
Member

Can you check with the latest version v0.0.2 ?

@SillyFreak
Copy link
Author

Unfortunately still the same, both on master and 0.0.2. I take it it's not reproducible on your end?

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

No branches or pull requests

2 participants