Seeking for better default vite:wasm
plugin
#7763
sapphi-red
started this conversation in
Ideas
Replies: 3 comments 1 reply
-
Seems it has already met Phase 3 requirements: WebAssembly/esm-integration#61 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Related discussions around esm integration for Node.js: nodejs/node#42309 (review) |
Beta Was this translation helpful? Give feedback.
0 replies
-
We discussed this issue in yesterday's team meeting. Our consensus was:
import init from 'foo.wasm?init'
const imports = {}
init(imports).then((instance) =>
// use instance here
) To use the exports: import init from 'foo.wasm?init'
const imports = {}
init(imports).then(({ exports }) =>
// use exports here
) What do you think? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Update: This proposal was implemented by #8219.
original content follwing
Current behavior
(a) Import without query (
import wasm from 'foo.wasm'
)(b) Import with
?url
(import wasmUrl from 'foo.wasm?url'
)(c) Using
import.meta.url
(new URL('foo.wasm', import.meta.url)
)Obtains URL to wasm file. This is not handled by
vite:wasm
plugin but I'm mentioning this since it is related.wasm-bindgen (with
--target web
) and AssemblyScript uses this.Usecases
1. Wants access to
WebAssembly.Instance
(#5190, #5615, #3847)instantiateWasm
property which requiresWebAssembly.Instance
.Webassembly.Module
, but it is actuallyWebAssembly.Instance
.Go.run
) requiresWebAssembly.Instance
.2. Wants access to
WebAssembly.Module
3. Wants ArrayBuffer of wasm
wasmBinary
property which requires ArrayBuffer.fetch
is enough.4. ES Module Integration Proposal for WebAssembly (#4551, #5755)
--target bundler
) uses this.experiments.asyncWebAssembly
(Example).--experimental-wasm-modules
flag. It is "Stability 1" and non-backward compatible changes may occur.Previously proposed approaches
vite-plugin-wasm
: Implements "Usecase 4" abovevite-plugin-top-level-await
even for modern browsers.getInstance
parameter forWebAssembly.Instance
My proposal
I believe that "ESM integration proposal for Wasm" should not be implemented at the current point. Because the spec is Stage 2 and it is experimental in Node.js which means it may change in future.
import wasm from 'foo.wasm'
Do not handle with
vite:wasm
.If it was not handled with any plugin, throw an error that "ESM integration proposal for Wasm" is not supported.
Maybe implement "ESM integration proposal for Wasm" with
vite:wasm
and only enable it behind experimental option. But it needs to be considered how to get along with top-level await.This makes users to be able to use other plugins (like
vite-plugin-wasm
) to handle wasm.import wasmUrl from 'foo.wasm?url'
Handle it same as currently.
import init from 'foo.wasm?helper'
Handle it same as the current
import init from 'foo.wasm'
does.The motivation of requiring query is to avoid future conflict with "ESM integration proposal for Wasm".
Also the return value of
init
is aligned to "ESM integration proposal for Wasm" to make it easy to migrate in future.import init from 'foo.wasm?instance'
This feature may not be needed. Instead adding an example like below to docs may be enough.
Beta Was this translation helpful? Give feedback.
All reactions