在当前页面
deno compile
,独立可执行文件
命令行用法
o compile [选项] [脚本参数]...
将给定的脚本编译成一个独立的可执行文件。
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 将下载适当的二进制文件并将其缓存到 $DENO_DIR
中。
了解更多: https://docs.deno.com/go/compile
不稳定选项 Jump to heading
--unstable
Jump to heading
--unstable
标志已被弃用。请改用细粒度的 --unstable-*
标志.
--unstable-bare-node-builtins
Jump to heading
启用不稳定的裸 Node 内置功能.
--unstable-broadcast-channel
Jump to heading
启用不稳定的 BroadcastChannel
API.
--unstable-cron
Jump to heading
启用不稳定的 Deno.cron API.
--unstable-detect-cjs
Jump to heading
在更多情况下将模棱两可的 .js、.jsx、.ts、.tsx 文件视为 CommonJS 模块.
--unstable-kv
Jump to heading
启用不稳定的键值存储 API.
--unstable-net
Jump to heading
启用不稳定的网络 API.
--unstable-node-globals
Jump to heading
在所有地方暴露 Node 全局变量.
--unstable-sloppy-imports
Jump to heading
启用不稳定的通过扩展探测、.js 到 .ts 以及目录探测来解析模块的功能.
--unstable-temporal
Jump to heading
启用不稳定的 Temporal API.
--unstable-unsafe-proto
Jump to heading
启用不安全的 proto 支持。这是一个安全风险。.
--unstable-webgpu
Jump to heading
启用不稳定的 WebGPU
API.
--unstable-worker-options
Jump to heading
启用不稳定的 Web Worker API.
类型检查选项 Jump to heading
--check
Jump to heading
设置类型检查行为。此子命令默认会检查本地模块,因此添加 --check
是多余的
如果提供了 "all" 值,远程模块将被包括在内。
或者,可以使用 'deno check' 子命令.
--no-check
Jump to heading
跳过类型检查。如果提供了 "remote" 值,远程模块的诊断错误将被忽略.
依赖管理选项 Jump to heading
--cached-only
Jump to heading
要求远程依赖已经缓存.
--frozen
Jump to heading
如果锁文件过期则报错.
--import-map
Jump to heading
从本地文件或远程 URL 加载导入映射文件 文档:https://docs.deno.com/runtime/manual/basics/import_maps.
--lock
Jump to heading
检查指定的锁文件。(如果未提供值,默认为 "./deno.lock").
--no-lock
Jump to heading
禁用自动发现锁文件.
--no-npm
Jump to heading
不解析 npm 模块.
--no-remote
Jump to heading
不解析远程模块.
--node-modules-dir
Jump to heading
设置 npm 包的 node 模块管理模式.
--reload
Jump to heading
Short flag: -r
重新加载源代码缓存(重新编译 TypeScript) 无值 重新加载所有内容 jsr:@std/http/file-server,jsr:@std/assert/assert-equals 重新加载特定模块 npm: 重新加载所有 npm 模块 npm:chalk 重新加载特定 npm 模块.
--vendor
Jump to heading
切换本地 vendor 文件夹用于远程模块和 node_modules 文件夹用于 npm 包.
Options Jump to heading
--allow-scripts
Jump to heading
允许为给定的包运行 npm 生命周期脚本
注意:只有在使用 node_modules 目录时才会执行脚本(--node-modules-dir
).
--cert
Jump to heading
从 PEM 编码文件加载证书颁发机构.
--config
Jump to heading
Short flag: -c
配置 deno 的不同方面,包括 TypeScript、代码格式化和代码格式化
通常配置文件将被称为 deno.json
或 deno.jsonc
并自动检测;在这种情况下,此标志不是必需的。
文档:https://docs.deno.com/go/config.
--env-file
Jump to heading
从本地文件加载环境变量 只有第一个具有给定键的环境变量会被使用。 现有的进程环境变量不会被覆盖,因此如果环境中已经存在同名的变量,它们的值将被保留。 在您的 .env 文件中存在多个相同环境变量的声明时,将应用第一个遇到的声明。这由您作为参数传递的文件的顺序决定。.
--ext
Jump to heading
设置提供文件的内容类型.
--location
Jump to heading
某些 web API 使用的 globalThis.location 的值.
--no-code-cache
Jump to heading
禁用 V8 代码缓存功能.
--no-config
Jump to heading
禁用自动加载配置文件.
--seed
Jump to heading
设置随机数生成器种子.
--v8-flags
Jump to heading
要查看所有可用标志的列表,请使用 --v8-flags=--help
标志也可以通过 DENO_V8_FLAGS 环境变量设置。
使用此标志设置的任何标志都会附加在 DENO_V8_FLAGS 环境变量之后.
编译选项 Jump to heading
--icon
Jump to heading
在 Windows 上设置可执行文件的图标 (.ico).
--include
Jump to heading
在编译的可执行文件中包含额外的模块或文件/目录。 如果动态导入的模块或 Web Worker 主模块在可执行文件中无法加载,或者要在可执行文件中嵌入文件或目录,请使用此标志。 此标志可以多次传递,以包含多个额外的模块。.
--no-terminal
Jump to heading
在 Windows 上隐藏终端.
--output
Jump to heading
Short flag: -o
输出文件 (默认为 $PWD/<推断名称>).
--target
Jump to heading
目标操作系统架构.
标志 Jump to heading
与 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
交叉编译 Jump to heading
你可以使用 --target
标志为其他平台交叉编译二进制文件。
# 为 Apple Silicon 交叉编译
deno compile --target aarch64-apple-darwin main.ts
# 为 Windows 交叉编译并添加图标
deno compile --target x86_64-pc-windows-msvc --icon ./icon.ico main.ts
支持的目标 Jump to heading
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 |
图标 Jump to heading
在针对 Windows 时,可以使用 --icon
标志为可执行文件添加图标。图标必须是 .ico
格式。
deno compile --icon icon.ico main.ts
# 交叉编译并添加图标
deno compile --target x86_64-pc-windows-msvc --icon ./icon.ico main.ts
动态导入 Jump to heading
默认情况下,可静态分析的动态导入(即在 import("...")
调用表达式中包含字符串字面量的导入)将包含在输出中。
// calculator.ts 及其依赖项将包含在二进制文件中
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
包含数据文件或目录 Jump to heading
从 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");
// 在这里使用 names 和 dataFiles
请注意,目前这仅适用于文件系统上的文件,而不适用于远程文件。
工作线程 Jump to heading
与不可静态分析的动态导入类似,工作线程 的代码默认不会包含在编译后的可执行文件中。有两种方法可以包含工作线程:
- 使用
--include <path>
标志包含工作线程代码。
deno compile --include worker.ts main.ts
- 使用可静态分析的导入语句导入工作线程模块。
// main.ts
import "./worker.ts";
deno compile main.ts
代码签名 Jump to heading
macOS Jump to heading
默认情况下,在 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 Jump to heading
在 Windows 上,可以使用 SignTool.exe
工具为编译后的可执行文件签名。
$ deno compile -o main.exe main.ts
$ signtool sign /fd SHA256 main.exe