cargo-web icon indicating copy to clipboard operation
cargo-web copied to clipboard

Question: how to specify the wasm path in the generated js file?

Open slmjkdbtl opened this issue 7 years ago • 3 comments

In the generated js file there's

return fetch( "main.wasm", {credentials: "same-origin"} )

is there a way to customize the wasm path (like "wasm/main.wasm")?

also is there a way to remove the

console.log( "Finished loading Rust wasm module 'main'" );

part (or like omit it in like production mode)?

slmjkdbtl avatar Aug 19 '18 03:08 slmjkdbtl

Currently this is not customizable, however you can use --runtime=library-es6 argument to generate a .js file which you can load manually yourself. (It exports two things - imports and initialize. You need to load the WASM module yourself and pass the imports when instantiating it, and then use initialize on the instantiated module.)

koute avatar Aug 19 '18 21:08 koute

@slmjkdbtl To be more specific, you would do something like this:

import init from "./path/to/js";

const wasm = init();

WebAssembly.instantiateStreaming(fetch("path/to/wasm"), wasm.imports).then((result) => {
    const exports = wasm.initialize(result.instance);
});

Pauan avatar Aug 20 '18 20:08 Pauan

@koute @Pauan Thank you, very helpful! But will there be an option to specify such thing in the future?

slmjkdbtl avatar Aug 21 '18 04:08 slmjkdbtl