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

wasm-pack module import is undefined #21

Closed voxelstack closed 9 months ago

voxelstack commented 1 year ago

I had a NextJS project setup where I would build a rust library with wasm-pack, install it with file:wasm-bindgen-module, set asyncWebAssembly on the Webpack config and then import and use it like this:

import { f } from "wasm-bindgen-module";
f();

I am now moving over to SvelteKit and the "(aka. Webpack's asyncWebAssembly)" led me to believe I would be able to do the same but when I do onMount(() => f()), I get Cannot read properties of undefined (reading 'f').

Here's my vite.config.js:

const config = {
    plugins: [sveltekit(), wasm(), topLevelAwait()],
    optimizeDeps: {
        exclude: ["wasm-bindgen-module"],
    },
};

Am I missing anything?

Antonio-Bennett commented 1 year ago

Also experiencing this with Sveltekit

Menci commented 1 year ago

any reproduce project?

Menci commented 1 year ago

Please test it with the latest version of the plugin. See #23.

Menci commented 1 year ago

Closing since no response.

Antonio-Bennett commented 1 year ago

@Menci I gave up lol came back to it still getting this error. Here's a simple repo I just setup https://github.com/Antonio-Bennett/VisualizedAlgos image image

SKTT1Ryze commented 1 year ago

Also meet this issue, is there a solution now?

Menci commented 1 year ago

Sorry I'll take a look on this

Menci commented 1 year ago

Just checked your repo @Antonio-Bennett. It's the same as https://github.com/Menci/vite-plugin-wasm/issues/27, which is a bug of wasm-pack https://github.com/rustwasm/wasm-bindgen/commit/721c86c3467750bf2a46bf85941e6c211ae9e87a, not this plugin.

See https://github.com/rustwasm/wasm-pack/issues/1235 for the issue and https://github.com/rustwasm/wasm-pack/pull/1237 for a fix.

Menci commented 1 year ago

@SKTT1Ryze @voxelstack Please also check you projects. If there're different issues in your project, send to me to investigate.

Sytten commented 1 year ago

In the meantime you can disable manually treeshaking for the module:

export default defineConfig({
  plugins: [wasm(), topLevelAwait()],
  build: {
    rollupOptions: {
      plugins: [
        {
          name: "disable-treeshake",
          transform(code, id) {
            if (id.includes("MYMODULE")) {
              return { moduleSideEffects: "no-treeshake" };
            }
          },
        },
      ],
    },
  },
});
SKTT1Ryze commented 1 year ago

@Menci Solved in my case, i just didn't load the*_bg.wasm file with init function.

SKTT1Ryze commented 1 year ago

@Menci Solved in my case, i just didn't load the*_bg.wasm file with init function.

My project: https://github.com/DarkNeos/neos-ts