mcpField
D
dart-compilation-deployment

dart-compilation-deployment

github.com/dart-lang开发53260 次安装2 周前更新
代码仓库

关于 dart-compilation-deployment

面向 Dart 开发者的编译与部署指南,帮助根据目标环境选择原生、AOT 或 Web 输出格式。内容涵盖命令示例、优化选项及验证流程。

主要功能

  1. 01选择编译目标
  2. 02构建独立可执行文件
  3. 03生成并运行 AOT 快照
  4. 04编译为 JavaScript 或 WebAssembly
  5. 05优化与交叉编译指南

适用场景

  1. 01将 Dart 命令行应用打包为原生二进制文件
  2. 02在已安装 Dart 运行时的环境部署 AOT 快照
  3. 03构建用于 Web 部署的 Dart 应用

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dart-lang/skills --skill dart-compilation-deployment

安装后在 Claude Code 中输入/dart-compilation-deployment确认已加载。

详细介绍

编译 Dart 应用程序

目录

编译目标与条件逻辑

根据部署目标和限制,选择适当的 dart compile 子命令:

  • 如果要在 Windows/macOS/Linux 上部署独立的原生二进制文件: 使用 exe。它会打包机器码和精简的 Dart 运行时。
  • 如果要部署到资源受限的环境,或分发多个命令行应用: 使用 aot-snapshot。它会生成不含运行时的特定于架构的模块。使用 dartaotruntime 执行该模块。
  • 如果要部署到 Web 目标: 使用 js 或 wasm。(参见相关技能:dart-web-development)。
  • 如果要使用训练数据优化启动速度: 使用 jit-snapshot。
  • 如果需要跨所有操作系统/CPU 架构移植的模块: 使用 kernel。请注意,其启动速度比 AOT 格式慢。

注意:exe 和 aot-snapshot 命令不支持 dart:mirrors 或 dart:developer。它们也不会运行构建钩子;如果存在钩子,请使用 dart build。

工作流:编译原生可执行文件

按照此工作流生成独立的可执行文件。

任务进度检查清单:

  • 确认应用不依赖 dart:mirrors 或 dart:developer。
  • 确定目标操作系统和架构。
  • 使用适当的标志执行 dart compile exe 命令。
  • 运行验证器 -> 在目标环境中执行二进制文件 -> 检查错误 -> 修复。

实施步骤:

  1. 编译源文件,并使用 -o 指定输出路径:
    dart compile exe bin/myapp.dart -o bin/myapp
    
  2. 交叉编译(仅限 Linux 目标): 如果要在 64 位主机上为不同架构进行编译,请指定 --target-os 和 --target-arch(arm、arm64、riscv64、x64):
    dart compile exe --target-os=linux --target-arch=arm64 bin/myapp.dart -o bin/myapp_arm64
    

工作流:编译 AOT 快照

如果目标环境已安装 Dart 运行时,请使用 AOT 快照来提升性能并减少磁盘空间占用。

任务进度检查清单:

  • 将 Dart 代码编译为 AOT 快照。
  • 将 .aot 文件部署到目标环境。
  • 确保目标环境的 PATH 中有 dartaotruntime。
  • 使用 dartaotruntime 执行快照。

实施步骤:

  1. 生成 AOT 快照:
    dart compile aot-snapshot bin/myapp.dart -o bin/myapp.aot
    
  2. 使用独立的 AOT 运行时运行快照:
    dartaotruntime bin/myapp.aot
    

工作流:编译 Web 应用

将 Dart 代码编译为可部署的 JavaScript 或 WebAssembly。

任务进度检查清单:

  • 选择目标格式(js 或 wasm)。
  • 选择优化级别(-O1 到 -O4)。
  • 编译应用。
  • 运行验证器 -> 测试用户输入的边界情况 -> 检查运行时类型错误 -> 必要时降低优化级别 -> 修复。

实施步骤:

  1. 使用 -O2 优化级别编译为 JavaScript(安全的默认值):
    dart compile js -O2 -o build/main.js web/main.dart
    
  2. 优化级别:
    • -O1:默认优化。
    • -O2:增加安全的代码缩减。(推荐的基线级别)。
    • -O3:省略隐式类型检查。警告:先使用 -O2 进行充分测试,确保不会抛出 TypeError 异常。
    • -O4:激进优化。警告:容易受到输入数据变化的影响。请严格测试边界情况。
  3. 环境声明: 使用 -D<flag>=<value> 传递环境变量:
    dart compile js -DAPI_URL=https://api.example.com -O2 -o build/main.js web/main.dart
    

示例

示例 1:为 Linux ARM64 交叉编译原生可执行文件
# Input
dart compile exe --target-os=linux --target-arch=arm64 bin/server.dart -o build/server_linux_arm64

# Output
# Generates a standalone binary at build/server_linux_arm64 containing the compiled machine code and Dart runtime.
示例 2:生成并运行 AOT 快照
# Input: Compilation
dart compile aot-snapshot bin/cli.dart -o build/cli.aot

# Input: Execution
dartaotruntime build/cli.aot

# Output
# Executes the pre-compiled AOT snapshot without the overhead of the full Dart SDK.
示例 3:使用严格优化编译 Web 应用
# Input
dart compile js -O3 --no-source-maps -DENVIRONMENT=production -o deploy/app.js web/app.dart

# Output
# Generates a highly optimized, minified JavaScript file (deploy/app.js) with type checks omitted and no source maps.

评论

支持 Markdown