@bytecodealliance/jco-transpile

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

WebAssembly Jco 组件转接功能

中文使用文档
文档目录7 节
  1. 传输
  2. 转换示例
  3. 生成来宾( 组件) 类型
  4. 来宾类型生成实例
  5. 生成主机类型
  6. 主机类型生成示例
  7. 捐款

@bytecodealliance/jco-transpile

这个 @bytecodealliance/jco 子项目允许转录 Web大会组件 输入ES模块 可以在像NodeJS和浏览器这样的Javascript环境中运行.

@bytecodealliance/jco-transpile ,主要用于仅用于转接功能 jco 需要帮助, 财务报告和审定财务报表 jco 来源于这个图书馆的转录.

快速启动

用于 @bytecodealliance/jco-transpile 作为您自己的代码中的库,您可以使用一些导出函数.

看看我们 gitHub 上的例子。 。 。 .

传输

要传送现有的WebAssembly组件(磁盘路径, 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);
}

[注] transpile 选择很多选项 -- -- 咨询 transpile.d.ts 用于更详细的类型信息.

outDir 控制生成文件路径的前缀, 所以指定它, 尽管没有文件 实际写入磁盘.

转换示例

如果您使用给定的 WIT 写入组件 :

package docs:[email protected];

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

world adder {
    export add;
}

在该组件转接后,您应该看到输出如下:

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 是可以从“ host” 代码中使用的切入点 :

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

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

你可以自己试试这个例子 以 GitHub 为单位。 。 。 .

生成来宾( 组件) 类型

写入组件时,您可以生成与您相应的打字声明 Web Assembly 接口类型 产生客体的进出口:

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);
        })
    );
}

来宾类型是JS组件使用的WIT接口的执行 -- -- 即JS代码 变成一个组件 jco componentize编号 :componentize-js进行有益的工作.

对于WIT世界中指定的导入,该类型生成将启用 电话编号 :使用 界面.

来宾类型生成实例

例如,考虑到以下WIT接口:

package docs:[email protected];

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

world adder {
    export add;
}

制作的打字声明如下:

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

生成主机类型

在构建运行转录组件的平台时,可以生成 Typescript 与《公约》相对应的声明 Web Assembly 接口类型 (构成部分需要) 生成主机类型 :

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);
}

主机类型是WIT接口的实现,用于 翻译后 JS代码, 即"主机"(NodeJS + V8 或浏览器)上,以制作平台 进口 工作时.

对于WIT世界中指定的导入,该类型生成将启用 执行 进口数量 功能界面.

主机类型生成示例

例如,考虑到以下WIT接口:

package docs:[email protected];

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

world adder {
    export add;
}

制作的打字声明如下:

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

许可证

这个项目根据Apache 2.0许可证获得许可,LLVM例外. 见 自由主义 更多细节.

捐款

除非您另有明确声明,否则有意提交的任何捐款 根据Apache-2.0许可证的定义, 应照此办理,不附加任何条件.

版本与发布记录

@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 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 和提交记录判断。