Dev Depot: RequireJS, Harmonizing Asset Allocation
Savvy coders are increasingly making use of modular programming techniques — breaking projects into more readily manageable blocks of code that boost their reusability while making maintenance and updates easier. While these are great benefits, being able to manage the dependencies between these modules can become a problematic process.
An easy example of this can be found in jQuery plugins, all of which are dependent on the base jQuery library, which must be loaded by your page before any of its plugins.
Although RequireJS doesn’t require jQuery, it’s often used alongside the technology, and demands some special considerations in order to make everything smooth and easy.
“While RequireJS loads jQuery just like any other dependency, jQuery’s wide use and extensive plugin ecosystem mean you’ll likely have other scripts in your project that also depend on jQuery,” a script spokesperson states. “You might approach your jQuery RequireJS configuration differently depending on whether you are starting a new project or whether you are adapting existing code.”
One consideration is the use of global variables, as jQuery will register itself as the global variables “$” and “jQuery,” even when it detects AMD/RequireJS.
“The AMD approach advises against the use of global functions, but the decision to turn off these jQuery globals hinges on whether you have non-AMD code that depends on them,” the spokesperson explains. “JQuery has a noConflict function that supports releasing control of the global variables and this can be automated in the require.config.”
Another consideration is module names, since jQuery defines a named AMD module “jquery” (all lower case) when it detects AMD/RequireJS, so the publisher recommends that to reduce the confusion, developers should use “jquery” as the module name in their require.config.
For example, in the require.config below, the module ID is shown on the left (jquery), while the path to the jQuery file is on the right side (jquery-1.9.0), relative to the baseUrl — in other words, the function is calling jQuery 1.9.0, located at js/lib/jquery-1.9.0.js, relative to the HTML page. Note that the path does not include the “.js” file extension:
"The other (recommended) solution is to just name the file ‘jquery.js’ and place it in the baseUrl directory. Then the above paths entry is not needed,” the spokesperson added.
“You can avoid lots of configuration lines by placing the files according to the default ID-to-path convention of baseUrl + moduleID + ‘.js’.”
The RequireJS website offers a wide range of coding examples, such as illustrating how to set the baseUrl to be the directory for third-party, library code, using one extra path config in the app code. Other instances involve using a shim configuration, as well as how to modify the plugins to be wrapped in a define() instead, for further flexibility.
Other examples show how to set the path of jQuery to point to a Google-hosted CDN, which the company says, benefits users who might already have the file in the browser cache and so won’t have to download it again. One factor to note when loading an asset from a CDN is that all plugins needing that asset as a dependency, have to call define().