0

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.


UPDATE

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() .

象嘉道
  • 3,657
  • 5
  • 33
  • 49

1 Answers1

1
dart2js
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)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thank you Günter Zöchbauer. I'd tried dart2js; so, a whole closure calling is result, and I need to clean it manually? cause I don't see the same class name and function name or similar ones exposed in the output js as in the Dart file. – 象嘉道 Jun 19 '17 at 15:19
  • Not sure what your comment is about. The output is not supposed to contain any classes. The output is ES5, tree-shaken and minimized. – Günter Zöchbauer Jun 19 '17 at 15:21
  • I mean I used dart2js on my library, and its output basically is (function() {...})(). my js is here: https://repl.it/Iq37/0 . ArrayList is the class name defined in my lib. – 象嘉道 Jun 19 '17 at 15:28
  • What do you expect the file to contain? – Günter Zöchbauer Jun 19 '17 at 15:28
  • I expect there's a js output containing some object definition like: function ArrayList() { ... }. And in a client js one can use the object like: var a = new ArrayList() . – 象嘉道 Jun 19 '17 at 15:41
  • Dart-to-JS transpilation only does whole-application transpilation. You can't use this code easily from JS code for example. You can use dart-js-interop to make certain identifiers available to JS, but without that tree-shaking and minification makes it impossible to use the code from JS. It's supposed to be added using a script tag and then executed automatically. There is some experimental DDC (Dart Development Compiler) that produces readable ES6 output, but currently it's only supposed to be used for development. – Günter Zöchbauer Jun 19 '17 at 15:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147095/discussion-between-xando-and-gunter-zochbauer). – 象嘉道 Jun 19 '17 at 15:55
  • Sorry, I don't have time to chat. If you have further questions (in comments) I'll try to answer. – Günter Zöchbauer Jun 19 '17 at 15:57
  • The update in your question isn't a well supported use case with Dart. See my 2nd last comment. – Günter Zöchbauer Jun 19 '17 at 15:59