scriptc README

项目的 README,来自 github.com/vercel-labs/scriptc。合并为一个 markdown 文件:README.md。

scriptc

Vercel Labs Experiment npm version: scriptc License: Apache-2.0 npm downloads per month: scriptc

scriptc compiles TypeScript and JavaScript to native executables and WebAssembly. It uses the types checked by the TypeScript compiler to generate machine code, and the output runs without Node.js installed. npm packages your program imports compile along with it. Code scriptc can't compile is reported at build time with a diagnostic and, where available, a rewrite hint.

scriptc is experimental and supports a subset of JavaScript, TypeScript, and Node.js APIs. Run scriptc coverage on an existing program to see how much of it compiles, and see the limitations and Node.js compatibility reference for details.

Installation

Install with Node.js 24 or later and npm:

$ npm install -g scriptc

The installed compiler runs natively on macOS, Linux, and Windows. Node.js is needed only to install it and to run WASI modules with scriptc run. Standalone compiler archives are also available from GitHub Releases.

Building executables requires a linker and the platform SDK or sysroot. See the quickstart for prerequisites and platform support for host requirements, cross-compilation, and WebAssembly.

Build a program

Create hello.ts:

const who = process.argv.length > 2 ? process.argv[2] : "world";
console.log(`hello, ${who}`);

scriptc run compiles the file and runs the result:

$ scriptc run hello.ts
hello, world

scriptc build writes an executable:

$ scriptc build hello.ts -o hello >/dev/null
$ ./hello scriptc
hello, scriptc

These examples use POSIX shell syntax. On Windows, build hello.exe and run it with .\hello.exe.

Use scriptc coverage hello.ts to check what compiles before you build. See coverage reports for details.

Use Node APIs

Supported Node APIs compile to the native runtime. For example, server.ts:

import { createServer } from "node:http";

const server = createServer((req, res) => {
  res.setHeader("content-type", "application/json");
  res.end(JSON.stringify({ path: req.url }));
});

server.listen(8080, () => {
  console.log("listening on http://localhost:8080");
});
$ scriptc build server.ts -o server >/dev/null
$ ./server
listening on http://localhost:8080

Use npm packages

npm packages compile into the executable with your code, so it doesn't read node_modules at runtime.

Create cli.ts:

import pc from "picocolors";

console.log(pc.green("hello from scriptc"));
$ npm install picocolors
$ scriptc build cli.ts -o cli >/dev/null
$ ./cli
hello from scriptc

Package support is partial. See npm dependencies for which packages compile and how to check them.

Documentation

Contributing

See CONTRIBUTING.md for source setup, testing, native artifact builds, and docs development.

scriptc (vercel-labs/scriptc)

正在搜索数据库…

这是什么?scriptc 是一个实验性的编译器,可以把 TypeScript 和 JavaScript 转换为带类型的 IR(中间表示)、可读的 C 代码、LLVM IR、原生汇编或目标文件、原生可执行文件,或 WebAssembly 模块。生成的独立二进制文件运行时不需要 Node.js。
它是像 tsc 那样的转译器,还是真的能生成原生二进制文件?它不止于转译成 JS。scriptc 只借用 TypeScript 编译器做解析和类型检查,然后生成原生可执行文件,并附带一个小型内置运行时(无需 Node 或 JS 引擎),或者通过 WASI 生成 WebAssembly 模块。
我该如何用它编译程序?编写一个 .ts 文件,然后运行 scriptc,并使用 --emit=ir|c|llvm|asm|obj 获取中间产物,或者直接构建独立可执行文件;`scriptc build --lib --profile ...` 会创建一个自包含的归档文件。
除了 Node 之外,我还需要安装什么?所有功能都必须先安装 Node.js 24+。--emit=ir|c|llvm 只需要 Node;--emit=asm|obj 使用 scriptc 内置的平台辅助工具,无需额外的编译器;完整的可执行文件需要平台链接器驱动程序或 SDK(通过 SCRIPTCLINKER 设置);WebAssembly 和 WASI 构建还需要 Zig。
它开箱即用就支持所有 TypeScript 或 JavaScript 特性吗?不支持。无法静态编译的代码会被标记为诊断信息;带有 any 类型的代码或 npm 包需要使用 --dynamic 标志,它会嵌入 quickjs-ng 引擎。WASI 构建还会拒绝任何需要套接字、子进程、操作系统信号或文件系统监视的代码。
它支持哪些平台和目标?scriptc 支持为 macOS、Linux 和 Windows 进行原生构建,并通过 WASI Preview 1 生成 WebAssembly。内置的 LLVM 级别辅助工具目前只能在 macOS 15+ arm64 上运行。
--dynamic 标志有什么作用?它会在输出中嵌入 quickjs-ng JavaScript 引擎,这样编译器就可以在原生二进制文件内部回退到解释执行带有 any 类型的代码,或 npm 包的 JavaScript 代码,而运行时无需读取 node_modules。
如何检查我的代码有多少部分可以静态编译?运行 scriptc coverage。它会报告程序中有多少部分可以静态编译,并为每个动态或不支持的调用点输出一条带编号的诊断信息。
--print=native-link-info 是做什么用的?它会输出编译后的目标文件,并打印一份带版本号的 JSON 配方,描述目标平台、主入口、确切的运行时包版本、所需的系统库、FFI 输入和 ABI 标记,适用于自定义链接配置。
为什么 WebAssembly 输出需要 Zig,而不是用内置辅助工具?跨目标的 WASI 和 WebAssembly 构建通过 Zig 内置的 WASI libc 和 LLVM 后端,生成可移植的 WASI Preview 1 模块。这与原生 macOS、Linux、Windows 辅助工具所用的工具链路径不同。
有哪些类似的项目?emscripten, compiler, perry, clice, tsrs, blaze, tsx, goose
大小
305.3 MB
星标
6.6K
时长
4个月
提交 · 90 天