When you begin creating a mapping application using the ESRI JavaScript API, there's a good chance that you will choose Dojo as your framework, since the ESRI JS API is built on top of Dojo anyhow. Whether you begin with the Sample Viewer from ArcScripts or decide to roll your own solution, you will quickly encounter dependencies on Dojo files which are not "baked in" to the core ESRI JS API build.

How Dojo handles dependencies beyond those satisfied in the original JS imports is a topic for another post, but suffice to say that it creates a blizzard of HTTP requests that slows down the loading of an application. What we want to do is use Dojo's build system to concoct a single (generally) JavaScript file that has all of the dependencies included by default.

This turns out to be a bit complicated for a couple of reasons:

  • the ESRI JS API already includes much of the Dojo code we need in a packaged form, and we don't want to duplicate that code in our build.
  • Dojo cannot resolve any dependencies on ESRI core modules, since we don't have access to the JS source.

To tackle the first challenge, we can use the concept of discardable layers in our Dojo build profile. Our first layer (compiled JavaScript file) in the Dojo build is going to be exclusively comprised of Dojo modules that are already included in the core ESRI JS API build. We add this first layer as a dependency of our main layer to ensure that we don't duplicate code. This unfortunately requires a bunch of manual CRTL-F work inside the ESRI JS API to actually find out what they've included in their build, but it's not all that bad. It would be nice to see their Dojo build profile...

The second problem is resolved in a way that seems non-intuitive at first, but makes sense when you think about how Dojo loads required modules. We need to REMOVE all Dojo.require statements that reference core ESRI JS API classes. Commenting them out is nice since it preserves the original Dojo.require's as code documentation. You might think this would cause the application to stop working entirely, but that's not the case. When working with the ESRI JS API, all of the core modules are imported into your page with the initial script import, so there's nothing Dojo needs to do when you specify your require. It's generally still good practice for a few different reasons to continue using Dojo.require statements, but the core ESRI requires will prevent your build from running.

Have a look at the integration of Geocortex Essentials 2.0 with the JavaScript sample viewer on resources.geocortex.com if you want to see the pre-built application in action. I've left out many gory details, so feel free to contact me if you'd like to get some more detailed info, or even a sample Dojo build profile.