feat(wasm): support source phase imports and enable js-string builtins#22675
Open
guybedford wants to merge 1 commit into
Open
feat(wasm): support source phase imports and enable js-string builtins#22675guybedford wants to merge 1 commit into
guybedford wants to merge 1 commit into
Conversation
Add support for the source phase import proposal for `.wasm` files,
giving the compiled `WebAssembly.Module` so consumers own instantiation:
import source mod from './mod.wasm'
const instance = await WebAssembly.instantiate(mod, imports)
The dynamic `import.source('./mod.wasm')` form is supported too. This is
the standard alternative to `?init` for instantiation control.
Source phase imports are polyfilled on top of Vite's existing module
pipeline: `import source x from './m.wasm'` is rewritten to a plain import
of `./m.wasm?source`, whose module resolves to a compiled
`WebAssembly.Module`. The dynamic form additionally unwraps the namespace
default to match native semantics. This avoids relying on native
`import source` support in browsers or Node (SSR compiles via node:fs),
so it works across all supported runtimes.
Also enable the JS String Builtins and Imported String Constants proposals
on compile/instantiate, matching the WebAssembly/ES Module Integration
loader.
d8064ba to
384f348
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-on to the WASM ESM Integration work, adding support for the source phase import proposal for
.wasmfiles. A source phase import hands you the compiledWebAssembly.Modulewithout instantiating it, so you own instantiation entirely — the standard alternative to?initfor instantiation control:The dynamic
import.source('./mod.wasm')form resolves to theWebAssembly.Moduleas well.Rather than rely on native
import sourcesupport (which isn't available across all supported browsers or Node versions yet), this polyfills the proposal on top of Vite's existing module pipeline:import source x from './m.wasm'is rewritten to a plainimport x from './m.wasm?source', whose module resolves to a compiledWebAssembly.Module.import.source('./m.wasm')becomesimport('./m.wasm?source').then((m) => m.default), unwrapping the namespace default to match the native semantics where the promise resolves to the module source object.?sourcemodule compiles vianode:fs, so it works on every supported Node version regardless of whether the runtime understands the syntax. We can drop the polyfill and passimport sourcethrough natively once Node 22 reaches EOL (April 2027).This also enables the JS String Builtins and Imported String Constants proposals on compile/instantiate, matching the WebAssembly/ES Module Integration loader.
Tests cover static and dynamic source phase imports, and source phase imports instantiated with the wasm module's own imports, in both serve and build. Docs are updated with a new "Source Phase Imports" section.