pub run, and
pub build commands use dart2js.
If you are using dart2js through one of the pub commands, see Configuring
the Built-in dart2js Transformer for Pub
for information on how to specify dart2js flags in your pubspec file.
The dart2js tool provides hints for improving your Dart code and removing unused code. You can get these hints for all kinds of code—even command-line apps. Also see dartanalyzer, which performs a similar analysis but has a different implementation.
dart2js --out=test.js test.dart
Usage in pubspec
You can also configure dart2js options in the pubspec file. For more information, see Configuring the Built-in dart2js Transformer for Pub.
Common command-line options for dart2js include:
- Generate the output into
<file>. If not specified, the output goes in a file named
- Insert runtime type checks, and enable assertions (checked mode).
- Generate minified output.
- Display help. (Use
-vhfor information about all options.)
Some other handy options include:
- Specify the path to the package resolution configuration file.
For more information, see
Package Resolution Configuration File.
This option cannot be used with
- Specify where to find “package:” imports.
This option cannot be used with
- Define an environment variable.
- Display version information for dart2js.
The following options help you control the output of dart2js:
- Don’t display warnings.
- Don’t display hints.
- Emit diagnostics, but don’t suggest how to get rid of the diagnosed problems.
- Display lots of information.
The following options control the analysis that dart2js performs on Dart code:
- Analyze even the code that isn’t reachable from
main(). This option is useful for finding errors in libraries, but using it can result in bigger and slower output.
- Analyze the code, but don’t generate code.
--analyze-only, but skip analysis of method bodies and field initializers.
- Add colors to diagnostic messages.
- Show warnings and hints generated from packages.
- If true, disables dynamic generation of code in the generated output. This is necessary to satisfy CSP restrictions (see W3C Content Security Policy). The default is false.
- Generates a file (with the suffix
.info.json) that contains information about the generated code. You can inspect the generated file with the Dump Info Visualizer.
Helping dart2js generate better code
You can do a couple of things to improve the code that dart2js generates:
Write your code in a way that makes type inference easier.
Once you’re ready to deploy your app, use the dart2js
--minifyoption to reduce code size.
Avoid using the dart:mirrors library, directly or indirectly. If you must use it, provide
Avoid setting variables to null.
Be consistent with the types of arguments you pass into each function or method.
Whichever browser you use, you should enable pausing on at least uncaught exceptions, and perhaps on all exceptions. For frameworks such as dart:async that wrap user code in try-catch, we recommend pausing on all exceptions.
To debug in Chrome:
Open the Developer Tools window, as described in the Chrome DevTools documentation.
Turn on source maps, as described in the video SourceMaps in Chrome.
Enable debugging, either on all exceptions or only on uncaught exceptions, as described in How to set breakpoints.
Reload your application.
To debug in Internet Explorer:
Update to the latest version of Internet Explorer. (Source-map support was added to IE in April 2014).
Load Developer Tools (F12). For more information, see Using the F12 developer tools.
Reload the application. The debugger tab shows source-mapped files.
Exception behavior can be controlled through Ctrl+Shift+E; the default is Break on unhandled exceptions.
Firefox doesn’t yet support source maps (see bug #771597).
To debug in Firefox:
- Enable the Developer Toolbar, as described in Kevin Dangoor’s blog post,
New Firefox Command Line Helps You Develop
- Click Pause on exceptions, as shown in the
- Reload your application.
To debug in Safari: