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

Goodbye to Noncompete Agreements in the US?

A noncompetition agreement, also known as a noncompete clause or covenant not to compete, is a contract between an employer and an employee, or between two companies.

Corey D. Silverstein ·
opinion

The Search for Perfection in Your Payments Page

There has been a lot of talk about changes to cross sales and checkout pages. You have likely noticed that acquirers are now actively pushing back on allowing merchants to offer a negative option, upsell or any cross sales on payment pages.

Cathy Beardsley ·
opinion

Unpacking the Payment Card Industry's Latest Data Security Standard

The Payment Card Industry Data Security Standard (PCI DSS) is a set of requirements and guidelines that apply to all businesses that accept credit card payments, and is designed to ensure the security of those transactions.

Jonathan Corona ·
opinion

Compliance With State Age Verification Laws

During the past year, website operators have faced a slew of new state age verification laws entailing a variety of inconsistent compliance obligations.

Lawrence Walters ·
opinion

Merchants in Spotlight With Visa's VIRP

By now, most merchants know about the Visa Integrity Risk Program (VIRP) rolled out in spring 2023. The program is designed to ensure that acquirers and their designated agents — payment facilitators, independent sales organizations and wallets — maintain proper controls and oversight to prevent illegal transactions from entering the Visa payment system.

Cathy Beardsley ·
opinion

How to Know When Hosting Upgrades Are Really Needed

I was reminded about an annoyingly common experience that often frustrates website owners: upgrades. Sometimes, an upgrade of physical system resources like CPU, RAM or storage really is required to solve a problem or improve performance… but how do you know you’re not just being upsold?

Brad Mitchell ·
profile

WIA Profile: Natasha Inamorata

Natasha Inamorata was just a kid when she first picked up a disposable camera. She quickly became enamored with it and continued to shoot with whatever equipment she could afford. In her teens, she saved enough money to purchase a digital Canon ELPH, began taking portraits of her friends, shot an entire wedding on a point-and-shoot camera and edited the photos with Picnik.

Women in Adult ·
trends

Collab Nation: Top Creators Share Best Practices for Fruitful Co-Shoots

One of the fastest ways for creators to gain new subscribers and buyers, not to mention monetize their existing fan base, is to collaborate with other creators. The extra star power can multiply potential earnings, broaden brand reach and boost a creator’s reputation in the community.

Alejandro Freixes ·
opinion

Bridging Generational Divides in Payment Preferences

While Baby Boomers and Gen Xers tend to be most comfortable with the traditional payment methods to which they are accustomed, like cash and credit cards, the younger cohorts — Millennials and Gen Z — have veered sharply toward digital-first payment solutions.

Jonathan Corona ·
opinion

Legal and Business Safety for Creators at Trade Shows

As I write this, I am preparing to attend XBIZ Miami, which reminds me of attending my first trade show 20 years ago. Since then, I have met thousands of people from all over the world who were doing business — or seeking to do business — in the adult industry.

Corey D. Silverstein ·
Show More