With Vite 8.1 or later, how do `import { plan } from './planner.wasm'` and `./planner.wasm?init` differ, and when do you need `?url` instead?
answer
- who instantiates the module
- exports as named ES exports
- an async module needs top-level await
- an init function you call yourself
- the Module object needs a URL
basics
~20 sSince Vite 8.1 a direct .wasm import is instantiated by Vite and exposes the module's exports as named ES exports. ?init returns a function that instantiates on demand with your imports. Use ?url when you need the WebAssembly.Module itself.
solid answer
~40 sVite 8.1 added direct `.wasm` imports following the WebAssembly ES module integration proposal: Vite reads the binary's imports and exports, resolves its imports as JavaScript module specifiers relative to the `.wasm` file, instantiates it and re-exposes the exports, so `plan` is ready to call. Because instantiation is asynchronous, the importer becomes an async module and the target must support top-level `await`. `?init` keeps the older, manual route: the default export is a function taking an optional import object and returning a promise of the `WebAssembly.Instance`, so you choose when to pay the cost. When you need the compiled `WebAssembly.Module`, for example to instantiate it several times, import `?url` and call `WebAssembly.instantiateStreaming(fetch(url))` yourself.
code
typescript · 15 lines// Direct import (Vite 8.1+): instantiated before this module runs
import { plan } from './wasm/planner.wasm'
// Manual: nothing happens until init() is called
import initPlanner from './wasm/planner.wasm?init'
export function quickRoute(from: number, to: number) {
return plan(from, to)
}
export async function detailedRoute(from: number, to: number) {
const instance = await initPlanner() // optional import object
const run = instance.exports.plan as (a: number, b: number) => number
return run(from, to)
}go deeper
Recall that Vite can import a .wasm file and that ?init gives an initialization function. Know that WebAssembly must be instantiated before use.
Explain the three forms by who instantiates: Vite for a direct import, you via init() for ?init, and the WebAssembly API for ?url.
Show the production consequences: a direct import's async module delays every importer, ?init defers the cost, and SSR builds need a Node.js-compatible runtime for both.
Decide where WebAssembly cost belongs in the app's start-up budget and which form teams use by default, given top-level await targets and server runtimes.
## Three ways to load a `.wasm` file A WebAssembly binary must be **compiled** into a `WebAssembly.Module` and **instantiated** into a `WebAssembly.Instance`, with any functions it imports supplied, before its exports can be called. Vite offers three import forms that differ in who does that work: | Form | Default or named exports | Who instantiates | Typical use | |---|---|---|---| | `import { plan } from './planner.wasm'` | the module's exports, as named ES exports | Vite, before the importer runs | call exports like any module's | | `import init from './planner.wasm?init'` | an init function | you, when you call `init()` | defer the cost, pass your own imports | | `import url from './planner.wasm?url'` | a URL string | you, with the WebAssembly API | need the `Module`, or full control | ## Direct imports (Vite 8.1) Vite 8.1 added **direct `.wasm` imports**, following the WebAssembly ES Module Integration proposal: 1. Vite reads the binary's declared imports and exports. 2. Each import's module name is treated as an import specifier, resolved relative to the `.wasm` file, and the requested members are wired into the instance. 3. Vite instantiates the module and re-exposes its exports as named ES module exports. Because instantiation is asynchronous, a directly imported `.wasm` file behaves as an **async module**: every importer waits for it, and it requires top-level `await` support in the target. For TypeScript, enable `allowArbitraryExtensions` and add a declaration file next to the binary, `planner.d.wasm.ts`, that declares the exports; otherwise the compiler reports that the module has no exported member `plan`. ## Manual initialization with `?init` `?init` is the older, explicit route. Its default export is a function that takes an optional import object, passed to `WebAssembly.instantiate` as its second argument, and returns a promise of the `WebAssembly.Instance`: - Nothing is instantiated until you call it, so the route planner's cost is paid only when a visitor opens the planner. - You supply the imports, for example a logging callback, instead of having them resolved from JavaScript modules. - You read exports from `instance.exports`; there are no named ES exports. In the production build, a `.wasm` file used with `?init` that is smaller than `build.assetsInlineLimit` is inlined as a base64 string; a larger one is emitted as an asset and fetched when needed. ## When to reach for `?url` Neither form above hands you the compiled `WebAssembly.Module`. When you need it, for example to instantiate the planner several times, import the file with `?url` and use the API directly: - `WebAssembly.instantiateStreaming(fetch(url))` returns both `module` and `instance`. - The file is emitted with a hashed name, so it caches well. ## Server-side builds Both direct imports and `?init` load the binary through `node:fs` in server code, so in an SSR build they work only in Node.js-compatible runtimes. `?init` gained SSR support in Vite 8.0; direct imports arrived in 8.1. A server runtime without `node:fs` needs its own loading strategy. ## A worked choice for the route planner The volunteer area's route planner is a large binary used on one page. A direct import at the top of the app's entry would make the entry an async module and delay every module that imports it. Two better placements: 1. Put the direct import inside the module for the route-planner page, loaded with dynamic `import()`, so only that page waits. 2. Or import `?init` and call it when the visitor first asks for a route, which also lets you show progress while it instantiates. ## Pitfalls - Forgetting the `planner.d.wasm.ts` declaration and silencing the TypeScript error instead. - Expecting named ES exports from `?init`; you read `instance.exports` instead. - Trying to pass an import object to a direct import; its imports are resolved from JavaScript modules relative to the binary. ## Choosing - A small helper module whose exports the page calls at startup: a direct import reads most naturally. - A heavy module that only one feature uses: `?init`, called from that feature, keeps it off the start-up path. - Several instances from one compiled module: `?url` with the WebAssembly API.
- Why does a direct `.wasm` import make its importer an async module?WebAssembly instantiation is asynchronous, and the exports must exist before the importer runs. The module therefore waits for instantiation before its importers run, which is why Vite's docs say it requires top-level `await` support.
- How do you type `import { plan } from './planner.wasm'` in TypeScript?Enable `allowArbitraryExtensions` in `tsconfig.json` and add `planner.d.wasm.ts` next to the binary, declaring `export function plan(from: number, to: number): number`. TypeScript then finds that file when it resolves the `.wasm` import.
- A direct .wasm import has always been built into Vite.
- ?init returns the module's exports as named ES exports.
- A direct .wasm import loads synchronously, so no top-level await is needed.
- ?init gives you the compiled WebAssembly.Module to reuse.
- Direct and ?init imports work in any server runtime in an SSR build.
Assumes Vite 8.1 or later: 8.1.0 added direct .wasm imports (WebAssembly ESM integration) and 8.0.0 added SSR support for .wasm?init; before 8.1 only ?init and ?url were built in.