Search our website

From the blog

ESRI JSAPI 3.4 and the Dojo Build System

Author · Scott Davis
Published · Apr 16, 2013
Last modified · Mar 20, 2024
Category · Developer
Read time · 2 min

In a previous post, I outlined how I use the Dojo Build System to optimize my web app code for production. Specifically I showed how I get around the problem of working with ESRI’s ArcGIS API for JavaScript library which has already been run through the build system. However, with their recent upgrade to AMD-style module loading my handy trick of using:

dojo['require']('esri.map');

… to fool the build system into skipping ‘esri…’ modules imports didn’t work anymore. I had no idea how to exclude modules when loading them like this:

define(['esri.map'], function (Map) {
  /* ... */
});

So I headed over to #dojo to see if the experts had any ideas. Fortunately for me, brianarn was there and was aware of the problem. After some brain storming, we came up with the idea to use a custom loader plugin for loading ESRI modules. Since the build system doesn’t try to flatten modules that are imported with nested requires, we hoped that importing them through the plugin would solve my problem. The plugin was a relatively simple implementation:

define(function () {
  // summary:
  //      A dojo loader plugin for loading esri modules so that
  //      they get ignored by the build system.
  return {
    load: function (id, require, callback) {
      // id: String
      //      esri module id
      // require: Function
      //      AMD require; usually a context-sensitive require bound to the module making the plugin request
      // callback: Function
      //      Callback function which will be called, when the loading finished.
      require([id], function (mod) {
        callback(mod);
      });
    },
  };
});

You can put this module within any package and use it like this:

define(['app/EsriLoader!esri/map'], function (Map) {
  /* ... */
});

Using the plugin to load ESRI module effectively prevents the build system from trying to include them in your layer files thus allowing the build script to complete successfully. Of course, none of this hacking would be needed if ESRI would just release their source code. :)

If you find a better way of getting around this problem or have any other suggestions please let me know at @SThomasDavis.