Javascript vs Native Dart
If you're writing programs in Dart, you're going to have trouble publishing code that can run because there's no native Dart support in browsers. However, even though you can't execute Dart code directly in the browser, you can execute Javascript which is supported by all modern browsers. Luckily you can can easily convert your Dart code directly in to Javascript using various tools supplied with Dart.
This means you can develop your applications in Dart, convert them to Javascript and create interactive websites users can use immediately. Then when Dart support is enabled in browsers, you can simply replace the Javascript with your Dart code and users will generally not notice any change except for improvements in performance and responsiveness.
So you get the best of both worlds. You get to develop robust and maintainable code, while your users still get to use your application.
Let's that a look at the various ways you can edit Dart code. We'll start by looking at tools that can generate Javascript. With the exception of the IDE, most of these tools require getting and building the Dart code which we'll look at in the next post.
The Dart IDE
This is by far the easiest method. If you're new to Dart and uncomfortable with concepts such as checking out and compiling your own code, then this is probably the only method you want to use for now.
The Dart IDE allows you to write Dart code and preview it directly within your browser by converting it to Javascript. When you're happy with your code you can even have the IDE create optimised Javascript that you can publish to the Internet.
![]() |
The Dart IDE |
htmlconverter.py
The htmlconverter tool is slightly different to all the other tools in that it not only converts Dart to Javascript but it also creates a single HTML file containing all of the converted Javascript. Instead of passing in the Dart file to be converted, with the htmlconverter tool you provide the HTML file to be converted. The tool then uses the script reference to the Dart file to include the converted Javascript in the HTML output.
In reality, the htmlconverter tool is a wrapper for dartc and frogsh both which we'll cover below.
dartc
This is the Dart compiler and behaves much like any other compiler. It operates from the command line and like any compiler includes an array of command line switches that affects how your code is compiled. You can use this tool to convert Dart code to Javascript but right now, the IDE does a slightly better job of creating smaller files. If you do decide to use dartc however, ensure you call it with the --optimize flag to generate optimised Javascript.
frogsh
frogsh is an additional converter for Dart code that utilises node.js. You can either use it directly but or you can use htmlconverter.py by passing in the --frog flag. The advantage of using frogsh is that it generates Javascript that right now is far more optimised than dartc.
One option is to do your development using the Dart IDE, then instead of generating optimised Javascript using the IDE, process your Dart using frogsh.
The command line for frogsh is rather non-obvious. The basic command line is
frogsh --compile-only --out=<path to js file> <path to dart file>
There's a number of other options you can see if you investigate the source, however the most useful self-explanatory ones are --ignore-unrecognized-flags, --verbose, --suppress_warnings, --warnings_as_errors, --throw_on_errors and --throw_on_warnings
Tip
Tip is an extensionfor the Chrome browser that allows you to quickly edit, then convert and run Dart code. You can see in the picture below that there are 4 panes. On the top left you can see the Dart code pane which is fully editable. Once you compile your code, the Javascript output shows in the top left window. The bottom panes show any compiler warnings in the left pane and the actual result is shown in the bottom left pane.
One ideal use for Tip is for quick testing and prototyping as it's very simple to test your code to check the output. You could create simple standalone classes and methods which you can then integrate into your main code once you're happy with it.
Toss
Toss is another handy tool for rapid testing and prototyping of your code. Toss is a simple web server that converts form Dart to Javascript "on the fly". The beauty of this is that you can easily save your Dart code to disk and rapidly view it in the browser. It's very new and functionality is bound to change so as of writing it's most definitely caveat emptor!
To run Toss, you'll need to have downloaded the Dart course code. Once you have it, you can run Toss from within the frog directory with the command line ./frogsh ../utils/tip/toss.dart which will create a server running on localhost for you to browse to.
Note that the web server is pretty basic and doesn't support directory/file browsing. By default, the document root of the server is the top level directory of your Dart source code directory and it's best if you type the location of the file directly e.g http://localhost:1337/client/samples/Clock.html. If you want to change the document root, some judicious editing of toss.dart will do the job (hint: change the value of homedir in the main() method)
Dartium/Chromium
This is real bleeding edge stuff but you can actually execute native code by running a version of Chromium with Dart support built in. It's called Dartium, get it? For the uninitiated, Chromium is the open source version of Chrome. it has its home at http://chromium.org/
To run Dartium, from within your Dart source execute the command client/tests/drt/Chromium.app/Contents/MacOS/Chromium from the top level directory of your Dart source code assuming you're using a Mac.
Next steps
I bet you want to try some of these cool tools out right? Next we'll cover checking and building the Dart source.