rust-parcel-template icon indicating copy to clipboard operation
rust-parcel-template copied to clipboard

Can't figure out how to import JavaScript modules

Open SillyFreak opened this issue 6 years ago • 4 comments

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.

SillyFreak avatar Jan 01 '19 20:01 SillyFreak

If you have placed in the js/ folder

../../js/helper

sendilkumarn avatar Jan 02 '19 13:01 sendilkumarn

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

SillyFreak avatar Jan 02 '19 14:01 SillyFreak

Can you check with the latest version v0.0.2 ?

sendilkumarn avatar Jan 02 '19 14:01 sendilkumarn

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

SillyFreak avatar Jan 02 '19 15:01 SillyFreak