Menci / vite-plugin-wasm

Add WebAssembly ESM integration (aka. Webpack's `asyncWebAssembly`) to Vite and support `wasm-pack` generated modules.
MIT License
295 stars 17 forks source link

wasm-bindgen memory optimization seems to have broken the plugin #5

Closed Secretmapper closed 2 years ago

Secretmapper commented 2 years ago

Steps to reproduce:

  1. Do the wasm-pack tutorial
  2. Install Vite with this plugin
  3. Get error: Cannot access memory before initialization on the bg file. (Truncated below):
import * as wasm from '/-/hello-wasm/hello_wasm_bg.wasm?import';

let cachedUint8Memory0;
function getUint8Memory0() {
    if (cachedUint8Memory0.byteLength === 0) {
        cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
    }
    return cachedUint8Memory0;
}

cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); // Uncaught ReferenceError: Cannot access 'memory' before initialization

The memory checking seems to have been changed since this PR: https://github.com/rustwasm/wasm-bindgen/pull/2886 and I'm guessing the initialization logic is different from what's now needed since the wasm file is referenced in the top level.

Menci commented 2 years ago

Could you please test this WASM binding with Webpack5's asyncWebAssembly and check if Webpack 5 could handle this correctly?

Secretmapper commented 2 years ago

@Menci I've tried to look into it, and in the process it seems like I should have checked the related issues of the one I linked - the bug seems to have been fixed by this PR:

https://github.com/rustwasm/wasm-bindgen/pull/2965

Closing as it's a wasm-bindgen issue.

0x1f57 commented 2 years ago

Could you please test this WASM binding with Webpack5's asyncWebAssembly and check if Webpack 5 could handle this correctly?

I tested that everything was normal in webpack5

Menci commented 2 years ago

Yes, I also found that Webpack 5 could load buggy bindings successfully. But Node.js's native ESM WASM loader fails.