@bytecodealliance/jco-transpile

中文文档
0.23.0发布于 2026/10/31(Apache-2.0 WITH LLVM-exception)TypeScriptES Module构建来源已验证近 30 天下载 357.9万

WebAssembly Component transpilation functionality for Jco

原始使用文档
文档目录7 节
  1. Transpiling
  2. Transpilation example
  3. Generating guest (component) types
  4. Guest type generation example
  5. Generating host types
  6. Host type generation example
  7. Contribution

@bytecodealliance/jco-transpile

This @bytecodealliance/jco sub-project enables transpilation of WebAssembly Components into ES modules that can be run in Javascript environments like NodeJS and the browser.

@bytecodealliance/jco-transpile is used primarily when only transpilation functionality of jco is needed, and jco derives it's use of transpilation from this library.

Quickstart

To use @bytecodealliance/jco-transpile as a library in your own code, you can use some of the exported functions.

Check out our examples on GitHub.

Transpiling

To transpile an existing WebAssembly component (path on disk, Buffer):

import { transpile, writeFiles } from '@bytecodealliance/jco-transpile';

async function example() {
    // Transpile a given WebAssembly component into JS runnabe in NodeJS
    const { files, imports, exports } = await transpile('path/to/component.wasm', { outDir: 'path/to/output/dir' });
    // Write out the files that have been generated to disk
    await writeFiles(files);
}

[!NOTE] transpile takes many options -- consult transpile.d.ts for more detailed type information.

outDir controls the prefix of generated file paths, so it is specified, despite the fact that no files are actually written to disk.

Transpilation example

If you write a component with the given WIT:

package docs:[email protected];

interface add {
    add: func(x: u32, y: u32) -> u32;
}

world adder {
    export add;
}

After tranpsilation of that component, you should see output like the following:

dist
└── transpiled
    ├── add.core.wasm
    ├── add.d.ts
    ├── adder.core.wasm
    ├── adder.d.ts
    ├── adder.js
    ├── add.js
    ├── add.mjs
    └── interfaces
        └── docs-adder-add.d.ts

3 directories, 8 files

dist/transpiled/adder.js is the entrypoint that can be used from "host" code:

import { add } from './dist/transpiled/adder.js';

console.log('1 + 2 = ' + add.add(1, 2));

You can try this example for yourself in GitHub.

Generating guest (component) types

When writing components, you can generate the Typescript declarations that correspond to your WebAssembly Interface Types (WIT) imports and exports by generating guest tyeps:

import { generateGuestTypes, writeFiles } from '@bytecodealliance/jco-transpile';

async function example() {
    const files = await generateGuestTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });

    // NOTE: Files is a serialization of the files produced of the following type:
    // type FileBytes = { [filepath: string]: Uint8Array; };
    //
    // You can use the code below to write out all files to a given directory
    await Promise.all(
        Object.entries(files).map(async ([filePath, bytes]) => {
            await mkdir(dirname(filePath), { recursive: true });
            await writeFile(filePath, bytes);
        })
    );
}

Guest types are the implementations of WIT interfaces that are used by JS components -- i.e. JS code that is turned into a component (by jco componentize/componentize-js) and performing useful work.

For a given import in a WIT world, this type generation would enable calling/using the interface.

Guest type generation example

For example, given the following WIT interface:

package docs:[email protected];

interface add {
    add: func(x: u32, y: u32) -> u32;
}

world adder {
    export add;
}

The Typescript declaration produced is:

declare module 'docs:adder/[email protected]' {
  export function add(x: number, y: number): number;
}

Generating host types

When building platforms that run transpiled components, you can generate Typescript declarations that correspond to the WebAssembly Interface Types (WIT) imports (that the component requires) by generating host types:

import { generateHostTypes, writeFiles } from '@bytecodealliance/jco-transpile';

async function example() {
    // Generate types for a host binding (i.e. supplying imports to a transpiled component)
    const files = await generateHostTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });
    // Write out the files that have been generated to disk
    await writeFiles(files);
}

Host types are the implementations of WIT interfaces that are used by post-transpilation JS code, i.e. on the "host" (NodeJS + V8 or the browser), to make platform imports work.

For a given import in a WIT world, this type generation would enable implementation of the imported functionality interface.

Host type generation example

For example, given the following WIT interface:

package docs:[email protected];

interface add {
    add: func(x: u32, y: u32) -> u32;
}

world adder {
    export add;
}

The Typescript declaration produced is:

/** @module Interface docs:adder/[email protected] **/
export function add(x: number, y: number): number;

License

This project is licensed under the Apache 2.0 license with the LLVM exception. See LICENSE for more details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.

版本与发布记录

@bytecodealliance/jco-transpile 共发布 50 个版本,首次发布于 2025/06/26;过去 12 个月发布了 41 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.23.0latest2026/10/318 天前
0.22.02026/10/318 天前
0.21.02026/10/2811 天前
0.20.02026/10/1623 天前
0.19.02026/10/1425 天前
0.18.02026/10/061 个月前

其他发布通道:prerelease → 0.0.1,rc → 0.1.0-rc.0。

关于 @bytecodealliance/jco-transpile 的常见问题

@bytecodealliance/jco-transpile 是什么?

WebAssembly Component transpilation functionality for Jco

如何安装 @bytecodealliance/jco-transpile?

在项目目录运行 npm install @bytecodealliance/jco-transpile,或使用 pnpm add / yarn add / bun add。

@bytecodealliance/jco-transpile 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bytecodealliance/jco-transpile 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bytecodealliance/jco-transpile 可以商用吗?

@bytecodealliance/jco-transpile 使用 (Apache-2.0 WITH LLVM-exception) 许可。声明的许可为 (Apache-2.0 WITH LLVM-exception),请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@bytecodealliance/jco-transpile 的下载量有多少?

根据 npm 官方下载统计,@bytecodealliance/jco-transpile 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,578,759 次,日均约 119,292 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bytecodealliance/jco-transpile 还在维护吗?

最新版本 0.23.0 发布于 2026/10/31(8 天前);至今共发布 50 个版本,其中过去 12 个月发布了 41 个;首次发布于 2025/06/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。