Configuring the Built-in dart2js Transformer for Pub

Send feedback

The pub serve, pub build, and pub run commands use the dart2js compiler (unless you specify another compiler) to convert your Dart files to JavaScript.

You can configure dart2js in the pubspec for your package. To do this, add $dart2js (note the leading dollar sign) under the transformers: field. If present, $dart2js is usually the last transformer.

For example, by default, pub minifies an app when in release mode (pub build defaults to release mode), but not otherwise (pub serve defaults to debug mode). The following example ensures that pub minifies the app in all situations:

- ...list of transformers...
- $dart2js:
    minify: true

Some apps, for example, Chrome Apps and apps that run as Chrome extensions, must generate the CSP (content-security policy) version of JavaScript. You can do this by passing the --csp flag to dart2js with a value of true.

You can specify the csp flag in the pubspec file, as follows:

- chrome
- $dart2js:
    csp: true


The following options are available. For more information on how these options work, see the documentation for dart2js:

- $dart2js:
    analyzeAll: true
    checked: true
    csp: true
    environment: {name: value, ...}
    minify: true
    sourceMaps: true
    suppressHints: true
    suppressWarnings: true
    terse: true
    verbose: true

For command-line options not covered in this list, see Special-case options.

Excluding an asset

You can also exclude a particular asset, or set of assets, from being processed by a transformer. You can also configure a transformer to run only on a particular asset, or set of assets. For more information, see How to exclude assets in Pub Assets and Transformers.

Special-case options

commandLineOptions: [...args...]

To specify one or more dart2js options that have no corresponding pub option, use commandLineOptions.

The following example illustrates how to specify --enable-foo and --enable-bar configuration options:

- angular2:
    entry_points: web/main.dart
- dart_to_js_script_rewriter
- $dart2js:
    commandLineOptions: [--enable-foo, --enable-bar]