dart2js 

Send to Kindle
home » snippets » dart » dart2js



Snippets

Usage: dart2js [options] dartfile

Compiles Dart to JavaScript.

Common options:
  -o<file> or --out=<file>
      Generate the output into <file>. If not specified, the output
      goes in a file named out.js.

  -c or --checked
      Insert runtime type checks, and enable assertions (checked
      mode).

  --minify
      Generate minified output.

  --disallow-unsafe-eval
      Disable dynamic generation of code in the output. Use this
      option when you want the generated JavaScript to satisfy CSP
      restrictions. For more information, see An Introduction to
      Content Security Policy.

  -h or --help
      Display help. (Use -vh for information about all options.)

MirrorsUsed to reduce file size

Ref: https://github.com/naomiblack/angulardart.org/pull/18/files

@MirrorsUsed(targets: const [
  Todo,
  BackendAppRouter], override: '*')
import 'dart:mirrors' show MirrorsUsed;

Using in nodejs

If you want to run the generated code in NodeJS, you'll need to make some changes.

// Fixup global object.
function Isolate() {}
var $globalThis =  typeof(window) !== 'undefined' ? window
                           : typeof(global) !== 'undefined' ? global
                           : this;
$globalThis.Isolate = Isolate;

// Define a print function.
print=console.log

Detecting if you're running in dart2js

// identical(1, 1.0) is true for dart2js and false for the VM.
bool runningDart2Js = identical(1, 1.0);

Snippets

dart2js --minify --analyze-all --output-type=dart --package-root=packages --out=out.dart main.dart
dart2js --analyze-all --output-type=dart --package-root=packages --out=out.dart main.dart

dart2js --categories=server --output-type=dart expression_extractor.dart