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 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 a snapshot (myapp.aot
).
It then uses the dartaotruntime
command (which provides a Dart runtime)
to run the snapshot:
$ 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.
Types of output
The following table shows the subcommands of dart compile
.
Subcommand | Output | More information |
---|---|---|
exe |
Self-contained executable | A standalone, architecture-specific executable file. Learn more. |
aot-snapshot |
AOT snapshot | An architecture-specific file with no Dart runtime. Learn more. |
jit-snapshot |
JIT snapshot | An architecture-specific file with parsed classes and compiled code that's generated during a training run of the program. Learn more. |
kernel |
Kernel snapshot | A portable binary snapshot. Learn more. |
js |
JavaScript | A deployable JavaScript file. Learn more. |
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
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 signing support (issue 39106)
- The format of the executables isn’t compatible with standard signing tools such as codesign and SignTool.
- No support for dart:mirrors and dart: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 snapshots (aot-snapshot)
Use AOT snapshots 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 snapshots 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 snapshots (jit-snapshot)
JIT snapshots 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 snapshot, 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 snapshots are architecture specific,
unlike snapshots produced using the
kernel
subcommand.
Portable snapshots (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 snapshot 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 snapshot:
$ dart compile kernel bin/myapp.dart
Compiling bin/myapp.dart to kernel file bin/myapp.dill.
$ dart run bin/myapp.dill
Although kernel snapshots 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.
Another Dart-to-JavaScript compiler, dartdevc
,
is for development use only.
You usually use the webdev
tool instead of
directly using a Dart-to-JavaScript compiler.
The webdev build
command, by default,
produces deployable JavaScript.
The webdev serve
command uses dartdevc
by default, but you can switch
to producing deployable JavaScript by using the --release
flag.
For more information, see the dart2js
documentation.