Table Of Contents

Previous topic

Module 3 - Customizing the Application

Next topic

Module 5 - Creating Web Services

This Page

Module 4 - Building JavaScript

In this module you’re going to learn how to use the jsbuild tool to minify the JavaScript of your application. Minifying the JavaScript code is important if you care about performance, as it drastically reduces the time to load the JavaScript code.


The jsbuild tool is included in the JSTools Python package. The MapFish framework package depends on JSTools, so JSTools was installed in the virtual Python environment as part of the framework installation.

You can check that jsbuild is properly installed by running this command:

$ jsbuild --help

It should produce this output:

Usage: [options] filename1.cfg [filename2.cfg...]

  -h, --help            show this help message and exit
  -u, --uncompress      Don't compresses aggregated javascript
  -v, --verbose         print more info
  -o OUTPUT_DIR, --output=OUTPUT_DIR
                        Output directory
                        resource base directory (for interpolation)
                        Only create file for this section
  -s CONCAT, --single-file-build=CONCAT
                        Create a single file of all of possible output
  -c COMPRESSOR, --compressor=COMPRESSOR
                        Compressor plugin to use in form

Creating Build Profile

To be able to minify your application JavaScript code you must first create a build profile. A build profile holds the build configuration: paths to JavaScript folders, etc.

A build profile for the default user interface is provided in jsbuild/app.cfg. It looks like this:

root =
first =
include =
exclude =
app.js will be the name of the resulting build file.
The root property provides the paths to the folders containing JavaScript code. It is to be noted that, with the above build profile, the JavaScript code for OpenLayers, GeoExt, and the application is minified and merged in a single build file, app.js.
The first property provides the list of JavaScript files that must be first in the resulting build file.
The include property provides the list of files that should be included in the build file. You’ll note that only one file is specified here: the entry point app file. The other JavaScript files to be included in the build file are specified using @include directives in the JavaScript application files themselves.
The exclude property provides the list of files that should not be included in the build file.


Before actually building the JavaScript code you need to create the folder where the build file will be placed. In the public folder create a folder named build:

$ mkdir -p mapfishapp/public/build

You can now launch the build command:

$ cd jsbuild
$ jsbuild -o ../mapfishapp/public/build   app.cfg

After a small while, the output should be:


You can now edit public/index.html to use the built version of the JavaScript code. For that comment the debug mode section and uncomment the non debug mode section. Like this:

<html xmlns="">
        <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
        <meta name="content-language" content="en" />

        <link rel="stylesheet" type="text/css" href="lib/ext/Ext/resources/css/ext-all.css"></link>
        <link rel="stylesheet" type="text/css" href="lib/ext/Ext/resources/css/xtheme-gray.css"></link>
        <link rel="stylesheet" type="text/css" href="lib/openlayers/theme/default/style.css"></link>
        <link rel="stylesheet" type="text/css" href="lib/geoext/resources/css/popup.css"></link>
        <link rel="stylesheet" type="text/css" href="app/css/main.css"></link>

        <script type="text/javascript" src="lib/ext/Ext/adapter/ext/ext-base.js"></script>

        <!-- debug mode (begin) -->
        <script type="text/javascript" src="lib/ext/Ext/ext-all-debug.js"></script>
        <script type="text/javascript" src="lib/openlayers/lib/OpenLayers.js"></script>
        <script type="text/javascript" src="lib/geoext/lib/GeoExt.js"></script>
        <script type="text/javascript" src="app/lib/App/layout.js"></script>
        <script type="text/javascript" src="app/lib/App/main.js"></script>
        <!-- debug mode (end) -->

        <!-- non debug mode (begin) -->
        <script type="text/javascript" src="build/app.js"></script>
        <!-- non debug mode (end) -->



You can now reload http://localhost:5000 in your browser.

You should get JavaScript errors in the FireBug console, this is because you added functionality in the previous modules of this workshop without adding proper @include directives in the layout.js. You should be able to fix that by adding the missing @include directives, for instance:

 * @include OpenLayers/Layer/WMS.js
 * @include OpenLayers/Map.js
 * @include OpenLayers/Projection.js
 * @include OpenLayers/Layer/XYZ.js
 * @include OpenLayers/Tile/Image.js
 * @include OpenLayers/Control/Navigation.js
 * @include OpenLayers/Control/ZoomBox.js
 * @include OpenLayers/Control/NavigationHistory.js
 * @include GeoExt/data/LayerStore.js
 * @include GeoExt/widgets/MapPanel.js
 * @include GeoExt/widgets/Action.js
 * @include GeoExt/widgets/ZoomSlider.js
 * @include GeoExt/widgets/tips/ZoomSliderTip.js
 * @include GeoExt/widgets/tree/LayerContainer.js