rustwasm / wasm-pack

📦✨ your favorite rust -> wasm workflow tool!
https://rustwasm.github.io/wasm-pack/
Apache License 2.0
6.27k stars 409 forks source link

importing local js files #240

Closed xtuc closed 6 years ago

xtuc commented 6 years ago

With wasm-bindgen we can depend on local JavaScript files, like the following example (from https://github.com/rustwasm/wasm-bindgen/tree/master/examples/import_js):

#[wasm_bindgen(module = "./defined-in-js")]
extern "C" {
     // ...
}

Actual

pkg
├── package.json
├── README.md
├── wasm_pack_test_bg.wasm
├── wasm_pack_test.d.ts
└── wasm_pack_test.js

Which results in Module not found: Error: Can't resolve './defined-in-js' in '/wasm-tool/rust-loader/example/pkg'.

Expected

defined-in-js.js should be moved to ./pkg.

pkg
├── package.json
├── README.md
├── defined-in-js.js
├── wasm_pack_test_bg.wasm
├── wasm_pack_test.d.ts
└── wasm_pack_test.js

Also note that other relative URLs needs to be rewritten?

ashleygwilliams commented 6 years ago

@alexcrichton last i checked we were not super amped about this interface? is this what is stable? if so i can build it into wasm-pack, but i had been holding off

alexcrichton commented 6 years ago

This is "stable" but only in a "that's how the attribute works" sense and not a "this is expected to work" sense. Imports from relative JS modules are basically not at all expected to work today, and if they do it's happenstance. In that sense I don't think this bug is actionable at the moment until there's an actual design for how relative paths and such are supposed to work.

Today the #[wasm_bindgen(module = "...")] is basically just configuring the import module of the wasm binary itself.

ashleygwilliams commented 6 years ago

gonna close this for the time being, based on @alexcrichton's comment above about the (lack of) functionality stability.

shinglyu commented 5 years ago

Sorry for commenting in a closed issue. I'm facing the same issue of needing to call JS function from Rust. Is there any plan for implementing a feature for this or is it ready somewhere? @alexcrichton ?

I tried to copy my js file into the pkg folder and it kinda worked locally. Not sure if this will work when deployed to a HTTP server.

ashleygwilliams commented 5 years ago

hi @shinglyu ! this is indeed a feature we intend to implement- probably in the next month or so. no worries about commenting on a closed issue!

shinglyu commented 5 years ago

@ashleygwilliams Thank you! If there is an issue/task for it please let me know. Maybe I can contribute to the discussion (or implementation).

wtfleming commented 4 years ago

@ashleygwilliams was this ever implemented, or is it still something that is intended to be implemented? I'm hoping to call into some custom JS functions from Rust and trying to figure out if I need to switch to using wasm-bindgen directly, or if I can continue using wasm-pack. thanks!

Pauan commented 4 years ago

@wtfleming I believe that importing local JS files should Just Work™ now (I use it all the time in my wasm-pack projects).

wtfleming commented 4 years ago

@Pauan thanks for letting me know, I seem to have it working now.

I think the problem may have been that running npm init rust-webpack my-app as documented at https://rustwasm.github.io/docs/wasm-pack/tutorials/hybrid-applications-with-webpack/getting-started.html installed "@wasm-tool/wasm-pack-plugin": "^0.4.2",

Updating the dependency on wasm-pack-plugin to 1.1.0 seems to have it working correctly.