Sunday, December 2, 2012

Running dart2js

‹prev | My Chain | next›

It's been a long while since I tried dart2js. In fact it has been so long that it was called frogc when I last saw it. Since I have an open errata for Dart for Hipsters testifying to that fact, this seems like a good time to revisit the command.

When I load my Dart Comics sample app in vanilla Chrome, I am currently seeing the following failure:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8000/scripts/web/main.dart.js
This is the JavaScript fallback for Dart kicking in. The problem is that I do not have compiled JavaScript to fall back to. So let's see if dart2js can fix this.

So I move into the public/scripts/web directory and run dart2js:
➜  web git:(master) dart2js main.dart 
Using snapshot /home/chris/local/dart/dart-sdk/bin/../lib/_internal/compiler/implementation/dart2js.dart.snapshot
Dart file main.dart compiled to JavaScript: out.js
I looks as though my application compiled successfully, but to the wrong filename. I could rename it manually, but dart2js now has command line options (the source of the errata):
➜  web git:(master) ✗ dart2js -h       
Usage: dart2js [options] dartfile

Compiles Dart to JavaScript.

Common options:
  -o Generate the output into .
  -c       Insert runtime type checks and enable assertions (checked mode).
  -h       Display this message (add -v for information about all options).
So I make use of that -o option:
➜  web git:(master) dart2js -omain.dart.js main.dart
Using snapshot /home/chris/local/dart/dart-sdk/bin/../lib/_internal/compiler/implementation/dart2js.dart.snapshot
There is no success message, but main.dart.js exists:
➜  web git:(master) ✗ ls -lh main.dart.js*
-rw-r--r-- 1 chris chris 360K Dec  2 23:14 main.dart.js
-rw-r--r-- 1 chris chris 5.7K Dec  2 23:14 main.dart.js.deps
-rw-r--r-- 1 chris chris  96K Dec  2 23:14
At 360k (and 12 kLOC), that is a pretty hefty JavaScript file. Granted, Dart Comics is a hefty client-side application (MVC, localStorage, Modal Dialog), but JavaScript output remains quite large.

Thankfully, it works flawlessly. I can create a Dart Comics record in Chrome and see it in Dartium:

And best of all, it was very easy and extremely quick.

Day #587

No comments:

Post a Comment