Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Using RequireJS

requirejs.png

Require.js is a JavaScript module loading library. Popularized by @jrburke, this script loader is one of many ways to address dependency hell in client-side JavaScript.

This sample depicts the basic usage form of the module loader, while the book analyzes the benefits and drawbacks of using it. The library version being used is RequireJS 2.1.9. Run the example from your shell, using:

open test.html

(or just open test.html in your favorite browser)

Note that this sample merely outputs a few values to the developer console, so you better open it up!

The bottom line is that RequireJS is better than doing no dependency management whatsoever, but probably more convoluted than necessary, as the alternatives we'll explore next will show.

RequireJS uses a data-main attribute to indicate the first script to load. In async mode, the rest of the scripts will be fetched as they get resolved. When using the optimizer to bundle everything together, this pattern will be useful just to learn the order in which the dependency graph should be put together into a single file. This way, we don't have to worry about ordering script tags, like we do in the classical approach of just adding a script tag for each script we write.

A deeper analysis can be found in the book.