WASM export a function from Rust

Good day,
Currently I'm writing rust+wgpu module for plotting graphs in html and have a fundamental problem: I cannot use my module. All works fine using #[wasm_bindgen(start)], but I cannot use the other #[wasm_bindgen] functions. I see a result of start() but get nothing from the other functions. What's happens? I don't understand the problem and I spend a full day to find any working sample.

  1. I use trunk serve but I also try custom actix server
  2. Console msg:
The `integrity` attribute is currently ignored for preload destinations that do not support subresource integrity. See https://crbug.com/981419 for more information
localhost./:6 Unchecked runtime.lastError: The message port closed before a response was received.
wasm.js:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
  1. I tryed #[no_mangle] pub extern "C" fn
  2. I tryed crate-type: cdylib, staticlib, rlib
  3. Even a giude example doesn't work.
    This already feels like a curse...

How properly create and use wasm module into HTML and use like a common .js package?

I don’t know what the specific cause of your problem is, but this seems like the most important problem to dig into. Your web server should not be returning "text/html" responses in response to requests for JS (or WASM). Perhaps you are loading the wrong path? Use the dev tools to determine what the response is — is it actually JS with the wrong content-type, or is it perhaps a HTML error page.

1 Like

Best guess is the server is set up for SPA by default, and the path is just wrong so it serves your index.html as a fallback.

Odd that the WASM start function runs, though; the standard setup is you import the JS wrapper that imports the WASM module, so you shouldn't see the latter without the former.