Lesson 22 / 25
Compilation and Deployment Targets
Choose between JIT, AOT, JavaScript and WebAssembly outputs.
Picking the right output
Dart compiles to several targets with dart compile. exe produces a self-contained native executable with an AOT-compiled snapshot and a small runtime: fast start-up, no Dart SDK needed on the target machine, ideal for CLIs and servers in containers (cross-compilation support for Linux targets has been improving across releases). aot-snapshot produces a snapshot run with dartaotruntime. jit-snapshot speeds up start-up of JIT-run apps. js compiles to optimised JavaScript for browsers, and wasm compiles to WebAssembly using WasmGC, which Flutter web can also use for better performance in supporting browsers. kernel produces a portable intermediate format. Flutter's build commands (flutter build apk, ipa, web, macos, windows, linux) use these compilers under the hood. AOT code cannot use dart:mirrors (reflection), which is one reason the Dart ecosystem relies on code generation rather than run-time reflection for JSON serialisation and dependency injection.
From source to targets
The same Dart source becomes a native executable, JavaScript or WebAssembly depending on the build command.
Building a small native CLI for a container
AOT executable in a multi-stage image with a minimal runtime base.
FROM dart:stable AS build
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get
COPY . .
RUN dart compile exe bin/server.dart -o bin/server
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/server
EXPOSE 8080
CMD ["/app/bin/server"]
# other targets:
# dart compile js web/main.dart -o build/main.js -O2
# dart compile wasm web/main.dart -o build/main.wasmNo reflection in AOT
Libraries that rely on dart:mirrors do not work in Flutter or AOT-compiled code. Prefer packages built on code generation, such as json_serializable, freezed and injectable.
Quick check: Which dart compile target produces a self-contained native executable?
- js
- kernel
- exe
- wasm
Answer
exe — dart compile exe bundles AOT code with a small runtime into one executable.