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

Failed to resolve import "wbg" from "node_modules/.../lib_bg.wasm" #9

Closed tedbyron closed 2 years ago

tedbyron commented 2 years ago

Using wasm-pack and a "file:path" dependency with yarn to my wasm package. I can use the init function from the package and I've verified that it is running the wasm binary, but when I try to import { memory } from the .wasm file I get this error:

[vite] Internal server error: Failed to resolve import "wbg" from "node_modules/.../lib_bg.wasm". Does the file exist?

The file does exist, so not quite sure what's happening here.

Full error
[vite] Internal server error: Failed to resolve import "wbg" from "node_modules/golem/lib_bg.wasm". Does the file exist?
  Plugin: vite:import-analysis
  File: /Users/ted/git/golem/node_modules/golem/lib_bg.wasm
  1  |
  2  |  import __vite__initWasm from "/__vite-plugin-wasm-helper"
  3  |  import { __wbg_new_693216e109162396 as __vite__wasmImport_0_0, __wbg_stack_0ddaca5d1abfb52f as __vite__wasmImport_0_1, __wbg_error_09919627ac0992f5 as __vite__wasmImport_0_2, __wbindgen_object_drop_ref as __vite__wasmImport_0_3, __wbg_self_ba1ddafe9ea7a3a2 as __vite__wasmImport_0_4, __wbg_window_be3cc430364fd32c as __vite__wasmImport_0_5, __wbg_globalThis_56d9c9f814daeeee as __vite__wasmImport_0_6, __wbg_global_8c35aeee4ac77f2b as __vite__wasmImport_0_7, __wbindgen_is_undefined as __vite__wasmImport_0_8, __wbg_newnoargs_fc5356289219b93b as __vite__wasmImport_0_9, __wbg_call_4573f605ca4b5f10 as __vite__wasmImport_0_10, __wbindgen_object_clone_ref as __vite__wasmImport_0_11, __wbg_process_e56fd54cf6319b6c as __vite__wasmImport_0_12, __wbindgen_is_object as __vite__wasmImport_0_13, __wbg_versions_77e21455908dad33 as __vite__wasmImport_0_14, __wbg_node_0dd25d832e4785d5 as __vite__wasmImport_0_15, __wbindgen_is_string as __vite__wasmImport_0_16, __wbg_static_accessor_NODE_MODULE_26b231378c1be7dd as __vite__wasmImport_0_17, __wbg_require_0db1598d9ccecb30 as __vite__wasmImport_0_18, __wbg_crypto_b95d7173266618a9 as __vite__wasmImport_0_19, __wbg_msCrypto_5a86d77a66230f81 as __vite__wasmImport_0_20, __wbg_newwithlength_e833b89f9db02732 as __vite__wasmImport_0_21, __wbg_randomFillSync_91e2b39becca6147 as __vite__wasmImport_0_22, __wbg_subarray_9482ae5cd5cd99d3 as __vite__wasmImport_0_23, __wbg_getRandomValues_b14734aa289bc356 as __vite__wasmImport_0_24, __wbg_length_e09c0b925ab8de5d as __vite__wasmImport_0_25, __wbindgen_memory as __vite__wasmImport_0_26, __wbg_buffer_de1150f91b23aa89 as __vite__wasmImport_0_27, __wbg_new_97cf52648830a70d as __vite__wasmImport_0_28, __wbg_set_a0172b213e2469e9 as __vite__wasmImport_0_29, __wbindgen_throw as __vite__wasmImport_0_30 } from "wbg";
     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
  4  |  const __vite__wasmModule = await __vite__initWasm({ "wbg": { __wbg_new_693216e109162396: __vite__wasmImport_0_0, __wbg_stack_0ddaca5d1abfb52f: __vite__wasmImport_0_1, __wbg_error_09919627ac0992f5: __vite__wasmImport_0_2, __wbindgen_object_drop_ref: __vite__wasmImport_0_3, __wbg_self_ba1ddafe9ea7a3a2: __vite__wasmImport_0_4, __wbg_window_be3cc430364fd32c: __vite__wasmImport_0_5, __wbg_globalThis_56d9c9f814daeeee: __vite__wasmImport_0_6, __wbg_global_8c35aeee4ac77f2b: __vite__wasmImport_0_7, __wbindgen_is_undefined: __vite__wasmImport_0_8, __wbg_newnoargs_fc5356289219b93b: __vite__wasmImport_0_9, __wbg_call_4573f605ca4b5f10: __vite__wasmImport_0_10, __wbindgen_object_clone_ref: __vite__wasmImport_0_11, __wbg_process_e56fd54cf6319b6c: __vite__wasmImport_0_12, __wbindgen_is_object: __vite__wasmImport_0_13, __wbg_versions_77e21455908dad33: __vite__wasmImport_0_14, __wbg_node_0dd25d832e4785d5: __vite__wasmImport_0_15, __wbindgen_is_string: __vite__wasmImport_0_16, __wbg_static_accessor_NODE_MODULE_26b231378c1be7dd: __vite__wasmImport_0_17, __wbg_require_0db1598d9ccecb30: __vite__wasmImport_0_18, __wbg_crypto_b95d7173266618a9: __vite__wasmImport_0_19, __wbg_msCrypto_5a86d77a66230f81: __vite__wasmImport_0_20, __wbg_newwithlength_e833b89f9db02732: __vite__wasmImport_0_21, __wbg_randomFillSync_91e2b39becca6147: __vite__wasmImport_0_22, __wbg_subarray_9482ae5cd5cd99d3: __vite__wasmImport_0_23, __wbg_getRandomValues_b14734aa289bc356: __vite__wasmImport_0_24, __wbg_length_e09c0b925ab8de5d: __vite__wasmImport_0_25, __wbindgen_memory: __vite__wasmImport_0_26, __wbg_buffer_de1150f91b23aa89: __vite__wasmImport_0_27, __wbg_new_97cf52648830a70d: __vite__wasmImport_0_28, __wbg_set_a0172b213e2469e9: __vite__wasmImport_0_29, __wbindgen_throw: __vite__wasmImport_0_30 } }, "/node_modules/golem/lib_bg.wasm?init");
  5  |  export const memory = __vite__wasmModule.memory;
      at formatError (file:///Users/ted/git/golem/node_modules/vite/dist/node/chunks/dep-1513d487.js:35028:46)
      at TransformContext.error (file:///Users/ted/git/golem/node_modules/vite/dist/node/chunks/dep-1513d487.js:35024:19)
      at normalizeUrl (file:///Users/ted/git/golem/node_modules/vite/dist/node/chunks/dep-1513d487.js:40145:33)
      at async TransformContext.transform (file:///Users/ted/git/golem/node_modules/vite/dist/node/chunks/dep-1513d487.js:40279:47)
      at async Object.transform (file:///Users/ted/git/golem/node_modules/vite/dist/node/chunks/dep-1513d487.js:35277:30)
      at async loadAndTransform (file:///Users/ted/git/golem/node_modules/vite/dist/node/chunks/dep-1513d487.js:39776:29)
Menci commented 2 years ago

Please provide a minimal reproducible example (zip or repo) for me to investigate this issue.

tedbyron commented 2 years ago

Nevermind, just figured it out. If using the --target web option, the init function will have the wasm memory buffer in its output. Thanks for the great vite plugins!