dart2js 命令 (Dart 生成 JS 编译器)
The webdev build command uses dart2js by default.
The webdev serve command uses dartdevc by default, but you can switch
to dart2js using the
The dart2js tool provides hints for improving your Dart code and removing unused code. Also see dartanalyzer, which performs a similar analysis but has a different implementation.
$ dart2js -O2 -o test.js test.dart
Build config usage
You can also configure dart2js options in a build config file. For more information, see the build_web_compilers package.
Common command-line options for dart2js include:
- Generates the output into
<file>. If not specified, the output goes in a file named
- Enables assertion checking.
- Controls optimizations that can help reduce code size and
improve performance of the generated code.
For more details on these optimizations, run
-O0: Disables many optimizations.
-O1: Enables default optimizations.
-O1optimizations, plus additional ones (such as minification) that respect the language semantics and are safe for all programs.
-O2optimizations, plus omits implicit type checks.
-O4: Enables more aggressive optimizations than
-O3, but with the same assumptions.
- Displays help. To get information about all options, use
Path and environment options
Some other handy options include:
- Specifies the path to the package resolution configuration file. For more information, see Package Resolution Configuration File.
- Defines an environment variable.
- Displays version information for dart2js.
The following options help you control the output of dart2js:
- Doesn’t display warnings.
- Doesn’t display hints.
- Emits diagnostics, without suggesting how to get rid of the diagnosed problems.
- Displays lots of information.
The following options control the analysis that dart2js performs on Dart code:
- Adds colors to diagnostic messages.
- Shows warnings and hints generated from packages.
- Disables dynamic generation of code in the generated output. This is necessary to satisfy CSP restrictions (see W3C Content Security Policy.)
- 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
- Don’t use
- Don’t override
- 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 app.
To debug in Edge:
- Update to the latest version of Edge.
- Load Developer Tools (F12). For more information, see Using the F12 developer tools.
- Reload the app. 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 Faster”.
- Click Pause on exceptions, as shown in the
- Reload your app.
To debug in Safari: