Configuring the Built-in dart2js Transformer for Pub

The pub serve, pub build, and pub run commands use the dart2js 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.

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:

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:

transformers:
- chrome
- $dart2js:
  csp: true

Options

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

transformers:
- $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, such as --enable-enum, 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...]

In addition to the options previously listed, commandLineOptions is available as a fallback for cases where a new command-line option has been added to dart2js, but a corresponding configuration option has not yet been added to pub.

An example of this is the --enable-enum option.

dependencies:
  polymer: '>=0.14.0 <0.15.0'
  core_elements: any
  browser: any
  range:
transformers:
- polymer:
    entry_points: web/index.html
- $dart2js:
    commandLineOptions: [--enable-enum]