Skip to content
My custom sidebar

平台概述

Perry 将 TypeScript 编译为 7 个平台的原生可执行文件,从相同的源代码。

支持的平台

平台目标标志UI 工具包状态
macOS(默认)AppKit完整支持 (127/127 FFI 函数)
iOS--target ios / --target ios-simulatorUIKit完整支持 (127/127)
tvOS--target tvos / --target tvos-simulatorUIKit完整支持 (焦点引擎 + 游戏控制器)
watchOS--target watchos / --target watchos-simulatorSwiftUI (数据驱动)核心支持 (15 个小部件)
Android--target androidJNI/Android SDK完整支持 (112/112)
Windows--target windowsWin32完整支持 (112/112)
Linux--target linuxGTK4完整支持 (112/112)
Web / WebAssembly--target web (别名 --target wasm)通过 WASM 桥接的 DOM/CSS完整支持 (168 个小部件)

交叉编译

bash
# 默认: 为当前平台编译
perry app.ts -o app

# 为特定目标编译
perry app.ts -o app --target ios-simulator
perry app.ts -o app --target tvos-simulator
perry app.ts -o app --target watchos-simulator
perry app.ts -o app --target web   # 别名: --target wasm
perry app.ts -o app --target windows
perry app.ts -o app --target linux
perry app.ts -o app --target android

平台检测

使用 __platform__ 编译时常量按平台分支:

typescript
declare const __platform__: number;

// 平台常量:
// 0 = macOS
// 1 = iOS
// 2 = Android
// 3 = Windows
// 4 = Linux
// 5 = Web (浏览器, --target web / --target wasm)
// 6 = tvOS
// 7 = watchOS

if (__platform__ === 0) {
  console.log("Running on macOS");
} else if (__platform__ === 1) {
  console.log("Running on iOS");
} else if (__platform__ === 3) {
  console.log("Running on Windows");
}

__platform__ 在编译时解析。编译器常量折叠比较并消除死分支,所以平台特定代码具有零运行时成本。

平台功能矩阵

功能macOSiOStvOSwatchOSAndroidWindowsLinuxWeb (WASM)
CLI 程序
原生 UI (web 上 DOM)
游戏引擎通过 FFI
文件系统沙盒沙盒沙盒文件系统访问 API
网络fetch / WebSocket
系统 API部分部分最小部分部分
小部件 (WidgetKit)
线程原生原生原生原生原生原生原生Web Workers

下一步

MIT License.