Do This One Thing Or Your Dart Web App Will Break

This is not link bait, this is a serious call to action for our Dartisans. If you do nothing, your Dart web app will stop working in a few weeks. Luckily, the fix is easy. To learn how to keep your Dart web app working, read on!

Background

Almost all Dart web apps use a small, but crucial, JavaScript file to bootstrap. While strictly not required, this dart.js file contains the necessary logic to turn on the Dart VM, swap out Dart code for JavaScript code if the VM isn't available, enable Dart-JavaScript interop, and more.

Most Dart web apps have this line at the bottom of their HTML file:

<!-- Don't do this anymore, for illustrative purposes only -->
<script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>

The Problem

There are a few problems with the current arrangement.
  1. The Google Code SVN server is not a CDN, and is not optimized for acting as a CDN.
  2. Relying on a public HTTP link makes it impossible to work without a network connection. Hacking on Dart on a plane is difficult unless you plan ahead.
  3. The dart.js file cannot move inside the SVN repo, making it difficult to refactor directory layouts in our repository.
  4. Dart web apps start slower that normal because the SVN server is not an optimized static asset server.
It's clear we needed to do something better, especially before Dart 1.0.

The Solution (aka Do This Now!)

Luckily, Dart ships out of the box with pub, our package manager. Pub helps you use third-party libraries with ease, and it turns out it works just as well with static assets like, you guessed it, JavaScript files.

We put dart.js into a pub package named browser and hosted it on pub.dartlang.org. Pub is now the canonical location for dart.js.

Step 1 - Add the browser package to your app

Create a pubspec.yaml file for your project (if you haven't already), and add the browser dependency:

dependencies:
  browser: any

The browser package contains the dart.js file. Learn more about the pubspec.yaml file.

Step 2 - Run pub install

Now run pub install to download the browser package and link it to your application. This command can be run from the command line via the Dart SDK, or via Dart Editor (from the Tools menu). You'll know this works when the packages directory contains a symlink to the browser package.

Step 3 - Update links to dart.js

Comb through your HTML files and replace:

<script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>

with

<script src="packages/browser/dart.js"></script>

Step 4 - Put web assets into web/ (optional)

The pub convention says to put all your web assets, like HTML, CSS, etc, into a web/ directory inside your pub package. Though not strictly required, we expect to be able to offer more helpful utilities if pub packages conform to conventions. Learn more about pub package layouts.

Summary

The dart.js file, probably used in your Dart web app today, will be deleted from SVN in a few weeks. You should install the browser package and update your <script> tags to point to the new location of dart.js.

You can send feedback to the original thread on the mailing list, where we look forward to your comments. We know these changes are disruptive, and we appreciate your help as we refactor quickly on the road to Dart 1.0.

Known Issues

Pub likes symlinks. A lot. Symlinks can be tricky to deploy, and now that dart.js is pulled in by a symlink, deployment of a Dart Web App is now a bit more complication. This is a very well know issue and we expect to have an answer for Dart web app deployments soon.

Popular posts from this blog

Lists and arrays in Dart

Converting Array to List in Scala

Null-aware operators in Dart