Introduction to the dojo build system

Source: Internet
Author: User

1. Introduction

The build system is the last process of product release. Its efficiency and quality directly determine the efficiency and quality of the final product. Although the build system has been quite mature in traditional applications, in the Web field, it is still a great place to go online without building. The fragmentation and complexity of web systems also make it a complicated task to build them. But through dojo, we realized an open-source Web application build system: bdbuild and its powerful functions. It is an independent general build system that is fully written in JavaScript and can run on nodejs, Rhino, and other Javascript Engines. In theory, it can build any web application. The following describes how to build a web application using the bdbuild System in Dojo. Starting from this point, you can also use bdbuild to build your own web application system and package non-Dojo applications.


Bdbuild is an open-source front-end application build engine. It is fully written in JavaScript and can run on node. JS, Rhino, and other Javascript Engines. Its powerful functions and flexible and scalable architecture enable it to be used for packaging any web application. Build can be translated into build in traditional applications, but in the web, it may be more suitable to translate into a package. A qualified web build system should be able to do at least the following: 2.
Bdbuild System Overview
  1. Exclude development files, such as backup files, code debugging information, unit test files, and so on.
  2. Package Multiple resource files required by the page into one file and compress them. These files are usually for JavaScript and CSS files. This process includes automatic processing of dependencies between resource files and packaging in order.
  3. Copy the resource file to the target location.

Basically, the ultimate goal of this work is to improve the performance. the characteristics of the HTTP protocol determine that the efficiency of multiple requests to obtain multiple small files is much lower than that of one request for a large file. Therefore, the primary goal of Web build is to merge small files to form a large file. To implement these general functions, the bdbuild system divides the build process into the following steps:

  1. Discover resources (discovery ). Find all the resources required by the final product, including not only JavaScript files, but also images and CSS. Bdbuild implements the default resource discovery engine. You can also create your own resource discovery engine to tell the system how to find the required resources.
  2. Transform ). That is, each resource is processed accordingly, which is an orderly and scalable step. For example, to read text and compress JavaScript code, there are two different conversion steps. The result of all the preceding conversion steps is used as the input of the next step. This achieves multiple processing of resources. You can add your own resource processing steps through the configuration file.
  3. Build control ). This uses a configuration file to precisely control the order and dependency of each resource conversion. Bdbuild also introduces the gate concept, which divides the packaging process into different stages.

Although the bdbuild system is still in Alpha testing, dojo has completed and encapsulated it to form its own build system. In the released dojo 1.7 beta version, we can use it to package web applications.

3. Introduction to the dojo build system

To use the dojo build system, you must first download the source code version of the release package. In util/build, It is the build-related tool:

Dojo encapsulates bdbuild systems to a considerable extent, making it easier to package dojo. By introducing the concept of layer, you can easily specify which modules should be compressed to the same Javascript file. In addition, the dojo build system automatically copies other files to the target folder, and each JavaScript or CSS file is compressed at the same time. The following code illustrates the role of a layer:

<script type="text/javascript" src="/dojoroot/dojo/dojo.js"></script><script type="text/javascript" src="/dojoroot/dijit/dijit.js"></script><script type="text/javascript" src="/js /mylayer.js"></script>

This is a typical piece of code that introduces Javascript in the page, where dojo. JS, dijit. JS and mylayer. JS is the three layers configured in the build system. They all contain all the code required by each other. Through layer, You Can Package Multiple JavaScript files required by the page into one, which speeds up page loading. Layer is specified in the profile of the build configuration file, as described below.

Profile Concept

Generally, a complete system has not only one layer, but also multiple layers for the same page. Therefore, Dojo defines the concept of profile to describe the configuration and parameters of the entire package .. Under dojoroot/util/buildscripts/profiles/, you can find some built-in profile files, which are used to package standard dojo. js and dijit. js files. An example of a typical profile file is as follows:

dependencies = {    layers: [    {        name: "dojo/dojo.js",        dependencies: [            "dijit._Widget",            "dijit._Templated",            "dojo.fx",            "dojo.NodeList-fx"        ]    }    ],    prefixes: [    [ "dijit", "../dijit" ],    [ "dojox", "../dojox" ],    [ "myNamespace", "~/src/myNamespace" ]    ]}

The file defines a Global Object dependencies. Includes two required attributes: layers and prefixes. You can define multiple layers in the layers array. The name attribute of each layer indicates the output file, which is a relative path relative to releasedir (described later. The dependencies array contains the modules required by this layer. The build system recursively searches for all dependent modules and packs them all.

Another attribute is the prefixes array, which defines the module ing relationship, which tells the system where to find a dojo module. The function is the same as the dojo. registermodulepath used in the application code. Each element in prefixes is also an array containing two elements. The prefix is the module prefix, for example, "dijit". The latter is the relative path relative to util/path, for example, ".. /dijit ".

With this profile, we can package it. In dojo, util/build. JS is used. Because the build system's own code is JavaScript, a javascript runtime environment is required. Currently, both nodejs and rhino are supported. The former usually requires the Linux or cygwin environment, and the latter requires the support of the Java environment. Nodejs is much faster than rhino. if conditions permit, you can use nodejs for build. The following code demonstrates how to start a build:

Use nodejs:

node dojoroot/dojo/dojo.js load=build [options]  

Use Java:

java -Xms256m -Xmx256m -jar path/to/util/shrinksafe/js.jar path/to/dojo/dojo.js baseUrl=path/to/dojo load=build [options]

As you can see, the build. js file accepts multiple parameters (options), and the complete parameter list can be obtained by specifying the-HELP parameter. For example, releasedir specifies the folder to output the build result. Action specifies the sequence of operations to be executed, such as action = clean, check, and release. Of course, there is also a key parameter: profile, which specifies the packaging configuration file to control the entire packaging process. For example:

Node dojoroot/dojo/dojo.js load=build profile=/home/dojo/my.profile.js action=check,release

The profile here can be of the following types:

  • If it is an HTML file, analyze the dojo. Require file to automatically generate a profile containing all modules.
  • If it is a standard profile file, it will be processed directly
  • If it is a folder, all the files ending with profile. js are scanned and packaged separately.

The complete parameters of the command line are shown in the following table:

Parameters Description
-- Build <FILENAME> Specifies the build control script.
-- Profile <FILENAME> Specify the profile file.
-- Action Specifies the operations to be performed, such as check, clean, and release.
-- Releasedir The folder that outputs the build result.
-- Version Specifies the version number.
Layer Definition

A profile can contain multiple layers. In addition to the name and dependencies described above, the configuration parameters of layer are also very useful: layerdependencies. It specifies the dependent layer. All modules that appear in the specified layer are not included in the current module. For example, if there are two pages P1 and P2, the code they share is packaged into layera, and the special ones are respectively packaged into layerb and layerc. When describing layerb and layerc, you can specify their layerdependencies as layera, so that the code in layera will be loaded only once by users, improving the loading speed. And all layers
All are implicitly dependent on the standard dojo. js layer. Therefore, the code displayed in Dojo. js does not appear in other layers.

In addition, layer also supports the following optional attributes:

Copyrightfile Specify a text file containing copyright information, and its content will be included in the generated layer file.
Custombase As mentioned above, all layers will depend on standard dojo. js. This attribute can change the default behavior to be dependent on a customized dojo. js. The possible scenario is that you need a smaller dojo. js. It is much smaller than the standard.
Keeprequires An array that contains the module that is running require. In dojo, you can use dojo. Require () to introduce a module. During build, these modules are directly introduced in the layer. If you need to request some files, you can specify them in this parameter.
Discard Boolean type. Indicates whether the current layer needs to write a file. The default value is false. That is, a layer must be written into a separate file. However, in some cases, some layers only exist as dependencies of other layers. In this case, this parameter can be set to true.
4. Summary

Bdbuild is a flexible and scalable open-source build system. It is written in JavaScript and runs in node. JS, Rhino, and other JavaScript environments. It is very suitable for front-end personnel to create custom packaging processes. The build system that comes with dojo is based on bdbuild. This article briefly introduces its principles and usage. Although it is sufficient for general dojo development, if you need to understand bdbuild in depth, even developing plug-ins requires more in-depth understanding. If you are interested, you can refer to the document to continue learning.

This article has been first published on the infoq Chinese site. All Rights Reserved. The original Article is "dojo ".
Build system introduction, If You Need To reprint, please be sure to attach this statement, thank you.
Infoq Chinese site is an online independent community for mid-and high-end technical personnel ,. net, Ruby, SOA, agility, architecture and other fields to provide timely and in-depth information, high-end technology conferences such as qcon, offline technology exchange activities qclub, free mini book download such as architect, etc..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.