UnFFI

Binding generation

Generate typed modules from C and C++ headers.

UnFFI can generate binding modules from project-local .h and .hpp files. The same generator core backs shipped OS bindings and the unffi/unplugin integration.

Bundler plugin

Use unffi/unplugin when a bundled app needs generated bindings as virtual modules.

vite.config.ts
import { vite as unffi } from 'unffi/unplugin'

export default {
  plugins: [
    unffi({
      entries: [{
        name: 'math',
        header: './native/math.h',
        libraryNames: ['./native/libmath'],
      }],
    }),
  ],
}
import { openMath } from 'virtual:unffi/bindings/math'

await using math = await openMath()

The plugin exports adapters for Vite, Rollup, Webpack, Rspack, esbuild, and Bun.

Entry options

OptionDescription
nameVirtual module name, used in virtual:unffi/bindings/<name>.
headerPath to the .h or .hpp file.
libraryNamesLibrary candidates passed to the generated open*() helper.
includeDirsExtra header include directories.
definesPreprocessor definitions.
platformOptional platform hint: macos, linux, windows, or portable.
envOptional UNFFI_*_PATH override name.

Parser model

The generator asks Clang for a JSON AST, maps supported C ABI types to t.* tokens, and emits normal TypeScript modules with schemas and open*() helpers.

Unsupported shapes are skipped instead of guessed: C++ classes, templates, Objective-C messaging, COM vtables, variadic functions, bitfields, and ownership-heavy structs need explicit wrappers.

Generated bindings are only as safe as the native symbols you choose. Start with scalar, cstring, and caller-owned buffer APIs.

On this page