deno.com
本页内容

deno compile,独立可执行文件

命令行用法

deno compile [OPTIONS] [SCRIPT_ARG]...

将给定脚本编译为独立可执行文件。

deno compile --allow-read --allow-net jsr:@std/http/file-server
deno compile --output file_server jsr:@std/http/file-server

指定的任何影响运行时行为的标志都将应用于生成的二进制文件。

这允许将 Deno 应用程序分发到未安装 Deno 的系统。在底层,它将精简版的 Deno 运行时与您的 JavaScript 或 TypeScript 代码捆绑在一起。

使用 --target 标志支持交叉编译到不同的目标架构。首次调用 deno compile 时,Deno 将下载相关二进制文件并将其缓存到 $DENO_DIR 中。


类型检查选项 跳到标题

--check 跳到标题

设置类型检查行为。此子命令默认对本地模块进行类型检查,因此添加 --check 是多余的。如果提供“all”值,将包含远程模块。或者,可以使用“deno check”子命令。

--no-check 跳到标题

跳过类型检查。如果提供“remote”值,将忽略来自远程模块的诊断错误。

依赖管理选项 跳到标题

--cached-only 跳到标题

要求远程依赖项已缓存。

--frozen 跳到标题

如果锁定文件过期,则报错。

--import-map 跳到标题

从本地文件或远程 URL 加载导入映射文件。

--lock 跳到标题

检查指定的锁文件。(如果未提供值,则默认为“./deno.lock”)。

--no-lock 跳到标题

禁用锁定文件的自动发现。

--no-npm 跳到标题

不解析 npm 模块。

--no-remote 跳到标题

不解析远程模块。

--node-modules-dir 跳到标题

设置 npm 包的 node 模块管理模式。

--reload 跳到标题

短标记:-r

重新加载源代码缓存(重新编译 TypeScript)无值 重新加载所有内容 jsr:@std/http/file-server,jsr:@std/assert/assert-equals 重新加载特定模块 npm: 重新加载所有 npm 模块 npm:chalk 重新加载特定 npm 模块。

--vendor 跳到标题

切换远程模块的本地 vendor 文件夹使用和 npm 包的 node_modules 文件夹使用。

选项 跳到标题

--allow-scripts 跳到标题

允许为给定包运行 npm 生命周期脚本 注意:脚本只有在使用 node_modules 目录 (--node-modules-dir) 时才会执行。

--cert 跳到标题

从 PEM 编码文件中加载证书颁发机构。

--conditions 跳到标题

使用此参数为 npm 包导出指定自定义条件。您也可以使用 DENO_CONDITIONS 环境变量。

--config 跳到标题

短标记:-c

配置 Deno 的不同方面,包括 TypeScript、代码检查和代码格式化。通常,配置文件名为 deno.jsondeno.jsonc 并自动检测;在这种情况下,此标志不是必需的。

--env-file 跳到标题

从本地文件加载环境变量。仅使用具有给定键的第一个环境变量。现有的进程环境变量不会被覆盖,因此如果环境中已存在同名变量,则其值将保留。如果您的 .env 文件中存在相同环境变量的多个声明,则应用第一个遇到的声明。这由您作为参数传递的文件顺序决定。

--ext 跳到标题

设置所提供文件的内容类型。

--location 跳到标题

某些 Web API 使用的 `globalThis.location` 值。

--no-code-cache 跳到标题

禁用 V8 代码缓存功能。

--no-config 跳到标题

禁用自动加载配置文件。

--preload 跳到标题

在主模块之前执行的文件列表。

--seed 跳到标题

设置随机数生成器种子。

--v8-flags 跳到标题

要查看所有可用标志列表,请使用 --v8-flags=--help。标志也可以通过 DENO_V8_FLAGS 环境变量设置。使用此标志设置的任何标志都会追加到 DENO_V8_FLAGS 环境变量之后。

编译选项 跳到标题

--exclude 跳到标题

在编译后的可执行文件中排除文件/目录。使用此标志可排除包含文件中特定文件或目录。例如,排除捆绑的 node_modules 目录中的某个文件夹。

--icon 跳到标题

在 Windows 上设置可执行文件的图标 (.ico)。

--include 跳到标题

在编译后的可执行文件中包含额外的模块或文件/目录。如果动态导入模块或 Web Worker 主模块在可执行文件中加载失败,或者要在可执行文件中嵌入文件或目录,请使用此标志。此标志可以传递多次,以包含多个附加模块。

--no-terminal 跳到标题

在 Windows 上隐藏终端。

--output 跳到标题

短标记:-o

输出文件(默认为 $PWD/).

--target 跳到标题

目标操作系统架构。

标志 跳到标题

deno install 类似,用于执行脚本的运行时标志必须在编译时指定。这包括权限标志。

deno compile --allow-read --allow-net jsr:@std/http/file-server

脚本参数可以部分嵌入。

deno compile --allow-read --allow-net jsr:@std/http/file-server -p 8080

./file_server --help

交叉编译 跳到标题

您可以使用 --target 标志交叉编译其他平台的二进制文件。

# Cross compile for Apple Silicon
deno compile --target aarch64-apple-darwin main.ts

# Cross compile for Windows with an icon
deno compile --target x86_64-pc-windows-msvc --icon ./icon.ico main.ts

支持的目标 跳到标题

Deno 支持交叉编译到所有目标,无论主机平台如何。

操作系统 架构 目标
Windows x86_64 x86_64-pc-windows-msvc
macOS x86_64 x86_64-apple-darwin
macOS ARM64 aarch64-apple-darwin
Linux x86_64 x86_64-unknown-linux-gnu
Linux ARM64 aarch64-unknown-linux-gnu

图标 跳到标题

在面向 Windows 时,可以通过使用 --icon 标志为可执行文件添加图标。图标必须是 .ico 格式。

deno compile --icon icon.ico main.ts

# Cross compilation with icon
deno compile --target x86_64-pc-windows-msvc --icon ./icon.ico main.ts

动态导入 跳到标题

默认情况下,静态可分析的动态导入(在 import("...") 调用表达式中包含字符串字面量的导入)将包含在输出中。

// calculator.ts and its dependencies will be included in the binary
const calculator = await import("./calculator.ts");

但非静态可分析的动态导入不会被包含。

const specifier = condition ? "./calc.ts" : "./better_calc.ts";
const calculator = await import(specifier);

要包含非静态可分析的动态导入,请指定 --include <path> 标志。

deno compile --include calc.ts --include better_calc.ts main.ts

包含数据文件或目录 跳到标题

从 Deno 2.1 开始,您可以通过 --include <path> 标志将文件或目录包含到可执行文件中。

deno compile --include names.csv --include data main.ts

然后通过 import.meta.dirname 读取相对于当前模块目录路径的文件。

// main.ts
const names = Deno.readTextFileSync(import.meta.dirname + "/names.csv");
const dataFiles = Deno.readDirSync(import.meta.dirname + "/data");

// use names and dataFiles here

请注意,这目前仅适用于文件系统上的文件,而不适用于远程文件。

工作线程 跳到标题

与非静态可分析的动态导入类似,工作线程的代码默认不包含在编译后的可执行文件中。有两种方式可以包含工作线程:

  1. 使用 --include <path> 标志包含工作线程代码。
deno compile --include worker.ts main.ts
  1. 使用静态可分析的导入方式导入工作线程模块。
// main.ts
import "./worker.ts";
deno compile main.ts

代码签名 跳到标题

macOS 跳到标题

默认情况下,在 macOS 上,编译后的可执行文件将使用临时签名进行签名,这等同于运行 codesign -s -

$ deno compile -o main main.ts
$ codesign --verify -vv ./main

./main: valid on disk
./main: satisfies its Designated Requirement

您可以在对可执行文件进行代码签名时指定签名身份,就像对任何其他 macOS 可执行文件所做的那样

codesign -s "Developer ID Application: Your Name" ./main

有关 macOS 上代码签名和公证的更多信息,请参阅官方文档

Windows 跳到标题

在 Windows 上,编译后的可执行文件可以使用 SignTool.exe 工具进行签名。

$ deno compile -o main.exe main.ts
$ signtool sign /fd SHA256 main.exe

在可执行文件中不可用 跳到标题

您找到所需内容了吗?

隐私政策