This might be mostly a webpack issue, rather than a wasm-bindgen issue, but I do have one wasm-bindgen specific question.
Running these commands generates my the .wasm and .js glue file as expected, which is great:
cargo +nightly build --target wasm32-unknown-unknown && wasm-bindgen --out-dir pkg ./target/wasm32-unknown-unknown/release/rust.wasm
The generated .js glue file (rust.js in my case) looks like this:
/* tslint:disable */
import * as wasm from './rust_bg';
const TextDecoder = typeof self === 'object' && self.TextDecoder
? self.TextDecoder
: require('util').TextDecoder;
...more glue...
/**
* @returns {void}
*/
export function greet() {
return wasm.greet();
}
The thing that is giving me issues is the second line, import * as wasm from './rust_bg';. Importing the greet function elsewhere in my otherwise all JS application and then running webpack (version 4.17.1) results in this error:
ERROR in ./rust/pkg/rust.js
Module not found: Error: Can't resolve './rust_bg' in '/Users/twilco/projects/proj-name/frontend/rust/pkg'
@ ./rust/pkg/rust.js 11:15-35
@ ./billing/status/status-view.jsx
@ ./billing/index.js
@ ./billing/bootstrap.js
When I change import * as wasm from './rust_bg'; to import * as wasm from './rust_bg.wasm';, the problem is fixed, and I'm able to use the imported wasm as expected - however, this manual edit is obviously overwritten every time I redo the bindgen. Is there a reason the .wasm file extension is left off the _bg file? There's probably a way to finagle Webpack into being able to resolve it without the file extension (although I don't know it!), but if there's no downside to adding the .wasm file extension to the end of that import statement then that would be nice to have.
Scratch this issue - was as simple as adding this to my webpack.config.js file 馃槼.
resolve: {
alias: {
...stuff...
},
extensions: ['.wasm']
}
Most helpful comment
Scratch this issue - was as simple as adding this to my
webpack.config.jsfile 馃槼.