dart-compilation-deployment
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesedart-compilation
dart-compilation
Goal
目标
Compiles Dart source code into optimized, target-specific formats including self-contained native executables, ahead-of-time (AOT) snapshots, just-in-time (JIT) modules, and web-deployable JavaScript or WebAssembly. Assumes the Dart SDK is installed, the target environment is configured, and the source code does not rely on unsupported libraries (e.g., ) for native compilation.
dart:mirrors将Dart源代码编译为针对特定目标的优化格式,包括独立原生可执行文件、提前编译(AOT)快照、即时编译(JIT)模块、可部署到Web的JavaScript或WebAssembly。本指南默认你已安装Dart SDK、完成目标环境配置,且源代码不依赖原生编译不支持的库(例如)。
dart:mirrorsDecision Logic
决策逻辑
Use the following logic to determine the appropriate compilation target:
- Is the target a Web environment?
- Yes -> Use (or
dart compile js). See Related Skill:dart compile wasm.dart-web-development
- Yes -> Use
- Is the target a Native OS (Windows, macOS, Linux)?
- Does the deployment require a single, self-contained binary?
- Yes -> Use .
dart compile exe
- Yes -> Use
- Is the deployment environment resource-constrained (e.g., embedded systems, containers) where multiple apps share a runtime?
- Yes -> Use and execute with
dart compile aot-snapshot.dartaotruntime
- Yes -> Use
- Does the application benefit from profile-guided optimization via a training run?
- Yes -> Use .
dart compile jit-snapshot
- Yes -> Use
- Does the deployment require a single, self-contained binary?
- Is the target platform-agnostic?
- Yes -> Use to generate a portable
dart compile kernelfile..dill
- Yes -> Use
使用以下逻辑确定合适的编译目标:
- 目标是否为Web环境?
- 是 -> 使用(或
dart compile js)。相关技能:dart compile wasm。dart-web-development
- 是 -> 使用
- 目标是否为原生操作系统(Windows、macOS、Linux)?
- 部署是否需要单个独立的二进制文件?
- 是 -> 使用。
dart compile exe
- 是 -> 使用
- 部署环境是否为资源受限环境(例如嵌入式系统、容器),且多个应用共享运行时?
- 是 -> 使用,并通过
dart compile aot-snapshot执行。dartaotruntime
- 是 -> 使用
- 应用是否能通过训练运行实现配置引导优化?
- 是 -> 使用。
dart compile jit-snapshot
- 是 -> 使用
- 部署是否需要单个独立的二进制文件?
- 目标是否需要跨平台兼容?
- 是 -> 使用生成可移植的
dart compile kernel文件。.dill
- 是 -> 使用
Instructions
使用指南
-
Determine Compilation Target and Parameters Evaluate the project requirements against the Decision Logic. STOP AND ASK THE USER: "Which target platform and architecture are you compiling for? Do you require cross-compilation (e.g., compiling for Linux ARM64 from macOS)?"
-
Compile to Self-Contained Executable (Native) For standard native binaries, use thesubcommand. This bundles the machine code and a minimal Dart runtime.
exebashdart compile exe bin/main.dart -o build/app.exeCross-Compilation (Linux targets only):bashdart compile exe \ --target-os=linux \ --target-arch=arm64 \ bin/main.dart -o build/app_linux_arm64 -
Compile to AOT Snapshot (Resource-Constrained) For environments where disk space is limited and a shared runtime is preferred, generate an AOT snapshot.bash
dart compile aot-snapshot bin/main.dart -o build/app.aotExecution:bashdartaotruntime build/app.aot -
Compile to Web Targets (JS / Wasm) For web deployments, compile to optimized JavaScript or WebAssembly.bash
# Compile to JS with aggressive optimizations (O2 is safe, O3/O4 omit type checks) dart compile js -O2 -o build/app.js web/main.dart # Compile to WebAssembly dart compile wasm web/main.dart -o build/app.wasm -
Compile to JIT or Kernel Modules (Specialized) JIT Snapshot (requires a training run):bash
dart compile jit-snapshot bin/main.dart -o build/app.jit dart run build/app.jitPortable Kernel:bashdart compile kernel bin/main.dart -o build/app.dill dart run build/app.dill -
Validate-and-Fix Loop After executing the compilation command, verify the output file exists and test its execution.bash
# Verify file generation ls -la build/ # Test execution (example for exe) ./build/app.exeError Handling: If compilation fails due to build hooks, fallback to. If type errors occur in JS compilation atdart buildor-O3, downgrade to-O4and recompile.-O2
-
确定编译目标和参数 结合决策逻辑评估项目需求。 请停止并询问用户: "你编译的目标平台和架构是什么?是否需要交叉编译(例如从macOS编译Linux ARM64版本)?"
-
编译为独立可执行文件(原生) 对于标准原生二进制文件,请使用子命令。该命令会打包机器码和最小化的Dart runtime。
exebashdart compile exe bin/main.dart -o build/app.exe交叉编译(仅支持Linux目标):bashdart compile exe \ --target-os=linux \ --target-arch=arm64 \ bin/main.dart -o build/app_linux_arm64 -
编译为AOT快照(资源受限场景) 对于磁盘空间有限、优先使用共享运行时的环境,请生成AOT快照。bash
dart compile aot-snapshot bin/main.dart -o build/app.aot执行方式:bashdartaotruntime build/app.aot -
编译为Web目标(JS / Wasm) 对于Web部署场景,编译为优化后的JavaScript或WebAssembly。bash
# 编译为高优化等级JS(O2为安全等级,O3/O4会省略类型检查) dart compile js -O2 -o build/app.js web/main.dart # 编译为WebAssembly dart compile wasm web/main.dart -o build/app.wasm -
编译为JIT或Kernel模块(特殊场景) JIT快照(需要训练运行):bash
dart compile jit-snapshot bin/main.dart -o build/app.jit dart run build/app.jit可移植Kernel:bashdart compile kernel bin/main.dart -o build/app.dill dart run build/app.dill -
验证与修复循环 执行编译命令后,验证输出文件是否存在并测试其可执行性。bash
# 验证文件生成 ls -la build/ # 测试执行(以exe为例) ./build/app.exe错误处理: 如果编译因构建钩子失败,请回退到。如果在dart build或-O3级别编译JS时出现类型错误,请降级到-O4后重新编译。-O2
Constraints
约束条件
- DO NOT use or
dart compile exeif the package or its dependencies utilize build hooks; these commands will fail. Usedart compile aot-snapshotinstead.dart build - DO NOT use or
dart:mirrorsin code targeted fordart:developerorexecompilation.aot-snapshot - DO use for self-contained native binaries on Windows/macOS/Linux.
dart compile exe - DO use or
dart compile jsfor web deployment targets.dart compile wasm - DO optimize for performance using AOT compilation where supported.
- DO use for running AOT snapshots in resource-constrained environments.
dartaotruntime - DO NOT attempt cross-compilation for target operating systems other than Linux.
- DO NOT use or
-O3JavaScript optimizations without verifying that the application never throws a subtype of-O4(e.g.,Error) and handles edge-case user input safely.TypeError
- 如果包或其依赖使用了构建钩子,禁止使用或
dart compile exe,这些命令会执行失败,请改用dart compile aot-snapshot。dart build - 针对或
exe编译的代码中,禁止使用aot-snapshot或dart:mirrors。dart:developer - 针对Windows/macOS/Linux平台的独立原生二进制文件,请使用。
dart compile exe - 针对Web部署目标,请使用或
dart compile js。dart compile wasm - 在支持的场景下,请使用AOT编译优化性能。
- 在资源受限环境中运行AOT快照时,请使用。
dartaotruntime - 禁止尝试针对Linux以外的操作系统进行交叉编译。
- 未验证应用不会抛出子类(例如
Error)、且能安全处理边缘用户输入的情况下,禁止使用TypeError或-O3等级的JavaScript优化。-O4