Visits to this blog

Listing the dependancies of a Dojo widget

The Dojo Javascript toolkit has a very cool packaging system that allows a developer to simply use the dojo.require function to pull in all the Javascript files needed for a widget. However, while this works perfectly well, the fact that it uses synchronous XmlHttpRequests to retrieve the files can be a nasty performance hit as browsers don’t cache these Javascript files, unlike files included using <script> elements. So, if at all possible it’s best to include the required files via <script> tags, and the user will only have to download them once.

The problem with this approach is that you have to include these script files in exactly the right order, otherwise Dojo’s packaging system will remotely load these files for you. I list three solutions below, skip to the last one if you’re lazy and just want the easiest way to solve the problem🙂

One way to figure out the correct order is by trial and error, using Firebug to see which combination results in zero Ajax requests. This is how I originally approached the problem, but obviously this is labour intensive, and has to be redone for every release of Dojo.

A second approach is to use Dojo’s build system to figure this out for you. To do this, open a command window in the /buildscripts folder (um, I should have said that you have to have a build of Dojo checked out from Subversion for this, and Java 5 installed), and type

This will output all the files you need, unfortunately including all of the base files that are only supposed to be baked into the dojo.js file, and you don’t want them. So, to find out what files to subtract from the first list, type:

java -jar lib/custom_rhino -opt -1 -f profiles/minimal.profile.js

This gives you a smaller list of files. Subtract these from your original list, and those are the files you need for your widget.

Finally, the third method is that you download an XML file which contains all the dependancies listed on a per widget basis. The files that you need are dependant on what profile of Dojo you use, as some of the required files are probably baked into the base dojo.js file. So, here’s some I created earlier: