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.

Consider the loading of JavaScript files, for example.

Many of today’s applications make use of a range of JavaScript files, often loaded individually using a separate <script> tag for each occurrence. This hurts performance and can also cause script conflicts when files that are dependent on other files are called in the wrong order.

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.

Enter RequireJS (www.requirejs.org), a JavaScript file and module loader designed to eliminate jQuery loading errors and other scripting conflicts, while improving the speed and quality of a website’s coding.

Optimized for in-browser use, RequireJS is compatible with Chrome 3+, Firefox 2+, IE 6+, Opera 10+ and Safari 3.2+, and can also be used in other JavaScript environments, such as Node or Rhino.

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:

require.config({
baseUrl: ‘js/lib’,
paths: {
jquery: ‘jquery-1.9.0’
}
});

"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().

It will take some time to get a grasp on the flexibility, power and value of RequireJS, but if your sites make heavy use of JavaScript and jQuery, then RequireJS is a winner.

Related:  

Copyright © 2024 Adnet Media. All Rights Reserved. XBIZ is a trademark of Adnet Media.
Reproduction in whole or in part in any form or medium without express written permission is prohibited.

More Articles

opinion

Strategic Upscaling of Non-4K Content

If content is king in adult, then technical quality is the throne upon which it sits. Technical quality drives customer acquisition and new sales, while cementing retention and long-term loyalty.

Brad Mitchell ·
profile

'Traffic Captain' Andy Wullmer Braves the High Seas as Spirited Exec

Wullmer networked and hobnobbed, gaining expertise in everything from ecommerce to SEO and traffic, making connections and over time rising through the ranks of several companies to become CEO of the mobile business arm of TrafficPartner.

Alejandro Freixes ·
opinion

To Cloud or Not to Cloud, That Is the Question

Let’s be honest. It just sounds way cooler to say your business is “in the cloud,” right? Buzzwords make everything sound chic and relevant. In fact, someone uninformed might even assume that any hosting that is not in the cloud is inferior. So what’s the truth?

Brad Mitchell ·
opinion

Upcoming Visa Price Changes to Registration, Transaction Fees

Visa is updating its fee structure. Effective April 1, both the card brand’s initial nonrefundable application fee and annual renewal fee will increase from $500 to $950. Visa is also introducing a fee of 10 cents for each settled transaction, and 10 basis points — 0.1% — on the payment volume of certain merchant accounts.

Jonathan Corona ·
opinion

Unpacking the New Digital Services Act

Do you hear the word “regulation” and get nervous? When it comes to the EU’s Digital Services Act (DSA), you shouldn’t worry. If you’re complying with the most up-to-date card brand regulations, you can breathe a sigh of relief.

Cathy Beardsley ·
opinion

The Perils of Relying on ChatGPT for Legal Advice

It surprised me how many people admitted that they had used ChatGPT or similar services either to draft legal documents or to provide legal advice. “Surprised” is probably an understatement of my reaction to learning about this, as “horrified” more accurately describes my emotional response.

Corey D. Silverstein ·
profile

WIA Profile: Holly Randall

If you’re one of the many regular listeners to Holly Randall’s celebrated podcast, you are already familiar with her charming intro spiel: “Hi, I’m Holly Randall and welcome to my podcast, ‘Holly Randall Unfiltered.’ This is the show about sex, the adult industry and the people in it.

Women In Adult ·
trends

What's Hot Now: Leading Content Players on Trending Genres, Monetization Strategies

The juggernaut creator economy hurtles along, fueled by ever-ascendant demand for personality-based authenticity and intimacy — yet any reports of the demise of the traditional paysite are greatly exaggerated.

Alejandro Freixes ·
opinion

An Ethical Approach to Global Tech Staffing

One thing my 24-year career as a technologist working to support the online adult entertainment industry has taught me about is the power of global staffing. Without a doubt, I have achieved significantly more business success as a direct result of hiring abroad.

Brad Mitchell ·
opinion

Finding the Right Payment Partner

Whenever I am talking with businesses that are just getting started, one particular question comes up a lot: “How do I get a merchant account?” It’s a simple question, but it has a complicated answer.

Jonathan Corona ·
Show More