Deno Desktop apps
Deno Desktop apps
Deno Desktop 应用
deno desktop turns a Deno project (anything from a single TypeScript file to a Next.js app) into a self-contained desktop application. The output is a redistributable binary that bundles your code, the Deno runtime, and a web rendering engine into one bundle per platform.
deno desktop 可以将一个 Deno 项目(无论是单个 TypeScript 文件还是 Next.js 应用)转换为独立的桌面应用程序。其输出是一个可分发的二进制文件,它将你的代码、Deno 运行时以及 Web 渲染引擎打包在一起,每个平台生成一个独立的包。
Coming in Deno 2.9
即将于 Deno 2.9 发布
deno desktop ships in Deno v2.9.0 and is not in a stable release yet. To try it now, run deno upgrade canary to install the canary build. The command, configuration keys, and TypeScript APIs may still change before the feature is stable.
deno desktop 将随 Deno v2.9.0 发布,目前尚未进入稳定版本。如需立即尝试,请运行 deno upgrade canary 安装预览版(canary build)。在功能稳定之前,相关命令、配置键和 TypeScript API 仍可能发生变化。
Why deno desktop
为什么选择 deno desktop
Web technology is the most widely-known UI toolkit in the world. Desktop apps built on web stacks (Electron, Tauri, Electrobun) take advantage of that, but each has tradeoffs you have to live with: huge binaries, missing platform support, no JavaScript ecosystem, no built-in update story, no framework integration.
Web 技术是世界上最广为人知的 UI 工具包。基于 Web 技术栈(如 Electron、Tauri、Electrobun)构建的桌面应用利用了这一点,但每种方案都有你必须接受的权衡:巨大的二进制文件、缺失的平台支持、缺乏 JavaScript 生态系统、没有内置的更新机制,以及缺乏框架集成。
deno desktop is opinionated about those tradeoffs:
- Small by default, full Node compatibility. The default WebView backend uses the operating system’s own webview for small binaries, and you still have the entire npm ecosystem available through Deno’s Node compat layer. Opt into the bundled Chromium (CEF) backend when you need identical rendering across macOS, Windows, and Linux.
- Framework auto-detection. Point deno desktop at a Next.js, Astro, Fresh, Remix, Nuxt, SvelteKit, SolidStart, TanStack Start, or Vite SSR project and it runs: the production server in release mode, the dev server with hot reload under
--hmr. No code changes are required to take an existing web project to the desktop. - In-process bindings instead of IPC. Backend and UI communication goes through in-process channels, not socket-based IPC. Values are still encoded as they cross the call boundary, but there is no cross-process round-trip between your Deno code and the webview.
- Cross-compile from one machine. The same machine can build for macOS, Windows, and Linux. Backends are downloaded as needed, not built locally.
- Built-in binary-diff auto-update. Ship a single
latest.jsonmanifest andbsdiffpatches; the runtime polls, applies, and rolls back automatically on failed launches.
deno desktop 对这些权衡有明确的见解:
- 默认体积小,完全兼容 Node。 默认的 WebView 后端使用操作系统自带的 webview 以保持二进制文件体积小巧,同时你依然可以通过 Deno 的 Node 兼容层使用整个 npm 生态系统。当你需要在 macOS、Windows 和 Linux 上实现完全一致的渲染效果时,可以选择打包 Chromium (CEF) 后端。
- 框架自动检测。 将 deno desktop 指向 Next.js、Astro、Fresh、Remix、Nuxt、SvelteKit、SolidStart、TanStack Start 或 Vite SSR 项目,它即可运行:在发布模式下运行生产服务器,在
--hmr下运行带有热重载的开发服务器。无需修改代码即可将现有的 Web 项目迁移到桌面端。 - 进程内绑定而非 IPC。 后端与 UI 的通信通过进程内通道进行,而非基于 Socket 的 IPC。虽然数据在跨越调用边界时仍需编码,但你的 Deno 代码与 webview 之间不存在跨进程往返开销。
- 单机交叉编译。 同一台机器即可为 macOS、Windows 和 Linux 构建应用。后端按需下载,无需在本地构建。
- 内置二进制差异自动更新。 只需发布一个
latest.json清单和bsdiff补丁;运行时会自动轮询、应用更新,并在启动失败时自动回滚。
Hello, desktop
你好,桌面端
Create a one-file desktop app: main.ts
Deno.serve(() => new Response("<h1>Hello, desktop</h1>", { headers: { "content-type": "text/html" }, }) );
>_deno desktop main.ts
创建一个单文件桌面应用:main.ts
(代码见上文)
>_deno desktop main.ts
The compiled binary opens a window pointed at a local HTTP server bound to your Deno.serve() handler. Run it directly:
>_./main # macOS / Linux
>_.\main.exe # Windows
编译后的二进制文件会打开一个窗口,指向绑定到你 Deno.serve() 处理程序的本地 HTTP 服务器。直接运行它:
>_./main # macOS / Linux
>_.\main.exe # Windows
Deno.serve() automatically binds to the address the webview navigates to, so you do not need to pass a port or hostname. See HTTP serving for details.
Deno.serve() 会自动绑定到 webview 导航到的地址,因此你无需传递端口或主机名。详情请参阅 HTTP 服务部分。
What’s in this section
本节内容概览
-
Configuration: the
desktopblock indeno.json. -
Backends: CEF, webview, raw; how to choose.
-
HTTP serving:
Deno.serve()integration and the serving model. -
Frameworks: Next.js, Astro, Fresh, Remix, Nuxt, SvelteKit, and others.
-
Windows:
Deno.BrowserWindowlifecycle, multiple windows, events. -
Bindings: calling Deno code from the webview via
bindings.<name>(). -
Menus: application and context menus.
-
Tray and dock: system status icons and the macOS dock.
-
Dialogs:
prompt(),alert(),confirm()as native popups. -
Notifications: native OS notifications via the Web Notification API.
-
Hot module replacement:
--hmrfor framework and non-framework apps. -
DevTools: unified DevTools attached to both the Deno runtime and the webview.
-
Auto-update:
Deno.autoUpdate(), manifests,bsdiff, rollback. -
Error reporting: capturing uncaught exceptions and panics.
-
Distribution: cross-compilation, output formats, installers.
-
Comparison: how
deno desktoprelates to Electron, Tauri, Electrobun, Dioxus. -
deno desktop CLI reference: the command, its flags, and the
deno.jsondesktop schema. -
配置:
deno.json中的desktop代码块。 -
后端: CEF、webview、raw;如何选择。
-
HTTP 服务:
Deno.serve()集成与服务模型。 -
框架: Next.js、Astro、Fresh、Remix、Nuxt、SvelteKit 等。
-
窗口:
Deno.BrowserWindow生命周期、多窗口、事件。 -
绑定: 通过
bindings.<name>()从 webview 调用 Deno 代码。 -
菜单: 应用程序菜单和上下文菜单。
-
托盘与 Dock: 系统状态图标和 macOS Dock。
-
对话框: 原生弹窗形式的
prompt()、alert()、confirm()。 -
通知: 通过 Web Notification API 实现的原生系统通知。
-
热模块替换: 针对框架和非框架应用的
--hmr。 -
DevTools: 同时连接到 Deno 运行时和 webview 的统一开发工具。
-
自动更新:
Deno.autoUpdate()、清单文件、bsdiff、回滚。 -
错误报告: 捕获未捕获的异常和恐慌(panics)。
-
分发: 交叉编译、输出格式、安装程序。
-
对比:
deno desktop与 Electron、Tauri、Electrobun、Dioxus 的关系。 -
deno desktop CLI 参考: 命令、标志位以及
deno.json的 desktop 架构。