Use the dart compile
command to compile
a Dart program to a target platform.
The output—which you specify using a subcommand—can
either include a Dart runtime or be a module
(also known as a snapshot).
Here’s an example of using the exe
subcommand
to produce a self-contained executable file (myapp.exe
):
$ dart compile exe bin/myapp.dart
Generated: /Users/me/myapp/bin/myapp.exe
The next example uses the aot-snapshot
subcommand to
produce an ahead-of-time (AOT) compiled module (myapp.aot
).
It then uses the dartaotruntime
command
(which provides a Dart runtime)
to run the AOT module:
$ dart compile aot-snapshot bin/myapp.dart
Generated: /Users/me/myapp/bin/myapp.aot
$ dartaotruntime bin/myapp.aot
To specify the path to the output file,
use the -o
or --output
option:
$ dart compile exe bin/myapp.dart -o bin/runme
For more options and usage information,
run dart compile [<subcommand>] --help
:
$ dart compile exe --help
The dart compile
command replaces the
dart2native
, dart2aot
, and dart2js
commands.
Refer to the native_app sample for a simple example of using dart compile
to compile a native app,
followed by examples of running the app.
Subcommands
The following table shows the subcommands of dart compile
.
Subcommand | Output | More information |
---|---|---|
exe |
Self-contained executable | A standalone, architecture-specific executable file containing the source code
compiled to machine code and a small Dart runtime.
Learn more. |
aot-snapshot |
AOT module | An architecture-specific file containing the source code
compiled to machine code, but no Dart runtime.
Learn more. |
jit-snapshot |
JIT module | An architecture-specific file with
an intermediate representation of all source code,
plus an optimized representation of the source code
that executed during a training run of the program.
JIT-compiled code can have faster peak performance than AOT code
if the training data is good.
Learn more. |
kernel |
Kernel module | A portable,
intermediate representation
of the source code.
Learn more. |
js |
JavaScript | A deployable JavaScript file,
compiled from the source code.
Learn more. |
Types of output
The following sections have details about each type of output
that dart compile
can produce.
Self-contained executables (exe)
The exe
subcommand produces a standalone executable for
Windows, macOS, or Linux.
A standalone executable is native machine code that’s compiled from
the specified Dart file and its dependencies,
plus a small Dart runtime that handles
type checking and garbage collection.
You can distribute and run the output file like you would any other executable file:
$ dart compile exe bin/myapp.dart -o /tmp/myapp
Generated: /tmp/myapp
$ cd /tmp
$ ./myapp
Signing
Executables created with dart compile exe
support signing on macOS and Windows.
For detailed documentation,
see the platform documentation for those operating systems,
such as the Windows SignTool.exe
documentation,
and the Apple Code Signing guide.
Known limitations
The exe
and aot-snapshot
subcommands have some known limitations:
- No cross-compilation support (issue 28617)
- The compiler supports creating machine code only for the operating system it’s running on. You need to run the compiler three times—on macOS, Windows, and Linux—to create executables for all three operating systems. A workaround is to use a CI (continuous integration) provider that supports all three operating systems.
- No support for
dart:mirrors
anddart:developer
- For a complete list of the core libraries you can use, see the All and AOT entries in the table of core Dart libraries.
AOT modules (aot-snapshot)
Use AOT modules to reduce disk space requirements
when distributing multiple command-line apps.
The aot-snapshot
subcommand produces an output file
that’s specific to the current architecture.
For example, if you use macOS to create a .aot
file,
then that file can run on macOS only.
AOT modules are supported on Windows, macOS, and Linux.
$ dart compile aot-snapshot bin/myapp.dart
Generated: /Users/me/myapp/bin/myapp.aot
$ dartaotruntime bin/myapp.aot
For more information, see
Known limitations and the
dartaotruntime
documentation.
JIT modules (jit-snapshot)
JIT modules include all the parsed classes and compiled code that’s generated during a training run of a program.
$ dart compile jit-snapshot bin/myapp.dart
Compiling bin/myapp.dart to jit-snapshot file bin/myapp.jit.
Hello world!
$ dart run bin/myapp.jit
Hello world!
When running from an application module, the Dart VM doesn’t need to parse or compile classes and functions that were already used during the training run, so the VM starts running user code sooner.
These modules are architecture specific,
unlike modules produced using the
kernel
subcommand.
Portable modules (kernel)
Use the kernel
subcommand to package up an app into a
single, portable file that
can be run on all operating systems and CPU architectures.
A kernel module contains a binary form of the abstract syntax tree
(Kernel AST) for a Dart program.
Here’s an example of creating and running a kernel module:
$ dart compile kernel bin/myapp.dart
Compiling bin/myapp.dart to kernel file bin/myapp.dill.
$ dart run bin/myapp.dill
Although kernel modules have reduced startup time compared to Dart code, they can have much slower startup than architecture-specific AOT output formats.
JavaScript (js)
The js
subcommand compiles Dart code to deployable JavaScript.
Here’s an example of compiling a Dart application to JavaScript with many optimizations enabled:
$ dart compile js -02 -o out/main.js web/main.dart
For more information on configuring the compiler, see the dart2js compiler options.
To learn more about building and deploying JavaScript applications, check out Web deployment.