Setting the file. One moment.
Subchapter 8.37
references/recipe-wasm.mdMarkdown2 KBView on GitHub
tsdown supports WASM through rolldown-plugin-wasm (opens in a new tab), enabling direct .wasm imports with synchronous and asynchronous instantiation.
pnpm add -D rolldown-plugin-wasmimport { wasm } from 'rolldown-plugin-wasm'
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['./src/index.ts'],
plugins: [wasm()],
})Add type declarations to tsconfig.json:
{
"compilerOptions": {
"types": ["rolldown-plugin-wasm/types"]
}
}import { add } from './add.wasm'
add(1, 2)Use ?init query for async initialization:
import init from './add.wasm?init'
const instance = await init(imports) // imports optional
instance.exports.add(1, 2)Use ?init&sync query for synchronous initialization:
import initSync from './add.wasm?init&sync'
const instance = initSync(imports) // imports optional
instance.exports.add(1, 2)import { add } from 'some-pkg'
add(1, 2)import { readFile } from 'node:fs/promises'
import init, { add } from 'some-pkg'
import wasmUrl from 'some-pkg/add_bg.wasm?url'
await init({
module_or_path: readFile(new URL(wasmUrl, import.meta.url)),
})
add(1, 2)import init, { add } from 'some-pkg/add.js'
import wasmUrl from 'some-pkg/add_bg.wasm?url'
await init({ module_or_path: wasmUrl })
add(1, 2)nodejs and no-modules wasm-bindgen targets are not supported.
wasm({
maxFileSize: 14 * 1024, // Max size for inline (default: 14KB)
fileName: '[hash][extname]', // Output file name pattern
targetEnv: 'auto', // 'auto' | 'auto-inline' | 'browser' | 'node'
})| Option | Default | Description |
|---|---|---|
maxFileSize | 14 * 1024 | Max file size for inlining. Set to 0 to always copy. |
fileName | '[hash][extname]' | Pattern for emitted WASM files |
targetEnv | 'auto' | 'auto' detects at runtime; 'browser' omits Node builtins; 'node' omits fetch |