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
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:
<file>. If not specified, the output goes in a file named
-vhfor information about all options.)
Some other handy options include:
The following options help you control the output of dart2js:
The following options control the analysis that dart2js performs on Dart code:
main(). This option is useful for finding errors in libraries, but using it can result in bigger and slower output.
--analyze-only, but skip analysis of method bodies and field initializers.
out.info.jsonfile with information about the generated code. You can inspect the generated file with the viewer.
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
--minify option 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:isolate and 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 Pause on Uncaught Exceptions.
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:
To debug in Safari: