How to transpile a Dart lib to a JavaScript one?


If I develop a Dart library, is there an idiom to transpile the Dart library to a JavaScript one?

Assume the Dart library mainly exports two things: a class and a function. On JavaScript side, a JavaScript class/object and a function are expected.


What I expect can be addressed in this scenario: I define a class ArrayList in my Dart lib (the directive library and the kerword export are used), and then I use some way to transpile this Dart lib to a JavaScript lib, in which there’s some definition like: function ArrayList() { … } . Thus, in a client JavaScript one can use it like: var a = new ArrayList() .


Usage: dart2js [options] dartfile

Compiles Dart to JavaScript.

Common options:
  -o <file> Generate the output into <file>.
  -c        Insert runtime type checks and enable assertions (checked mode).
  -m        Generate minified output.
  -h        Display this message (add -v for information about all options).

If you have a pub package (with pubspec.yaml), then you can just run

pub build

to get the whole package to be transpiled to JavaScript (and transformers being run that are used by some frameworks for code generation)

Answered By – Günter Zöchbauer

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.