Menci / vite-plugin-wasm

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

Add option to bundle .wasm file instead of inlining wasm binary as base64 #35

Closed ranile closed 9 months ago

ranile commented 1 year ago

There should be an option to bundle the WASM as .wasm file. At the moment, WASM is encoded as base64 and included in JS. This has several disadvantages, including but not limited to slower execution.

Please allow externalizing wasm files

Menci commented 1 year ago

How do you find it's encoded?. The WASM file is bundled.

ranile commented 1 year ago

Apologies, I should've mentioned this in the issue. I'm using library mode to build a library that exposes an API from the WASM file. The contents of the wasm file are encoded inside the final JS file

Yongle-Fu commented 1 year ago

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="env" error: module is not an object or function

my import files is xxx.js and xxx.wasm build from cpp project by emcc

Menci commented 1 year ago

@Yongle-Fu Please try Webpack's asyncWebAssembly. Does that works?

Yongle-Fu commented 1 year ago

webpack 5 works

const loadWasm = require('./oz.js'); liboz = await loadWasm()

Yongle-Fu commented 1 year ago
  import addWasmInit from './add.wasm?init'
  import loadWasmInit from './oz.wasm?init'
  import loadWasm from './oz.js'

  // code works
  addWasmInit().then((instance) => {
      console.log(instance);
      console.log(instance.exports.add(1, 2));
  });

  // code not works, Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="env" error: module is not an object or function
  loadWasmInit().then((instance) => {
      console.log(instance);
  });

  // code not works, another error
  const liboz = await loadWasm();
Menci commented 9 months ago

?init is not related to this plugin, closing.