CSKR
CSKR

Reputation: 121

What is the purpose of "define.amd = {jQuery: true }" in the code of RequireJS?

Can any one explain with details, why in the require.js file the following code is set:

define.amd = {
    jQuery: true
};

Reference Link: Default set to jquery as true

Upvotes: 4

Views: 2974

Answers (1)

Louis
Louis

Reputation: 151441

Setting define.amd to some value is done to help distinguish the define function provided by an AMD loader from any old define provided by something else. A typical pattern for code that should handle multiple loading scenarios is this:

if (typeof define === 'function' && define.amd) {
    define(['exports'], factory(exports));
} else if (typeof module === 'object' && module.exports) {
    factory(module.exports);
} else {
    factory(exports);
}

The first line checks whether you have an AMD loader available, and will use the AMD loader if present. If a define function exists but it does not have the amd property set, then it is some random foreign define.

The name define is pretty generic. If it were not for the amd property, it would be sometimes difficult to determine whether the define that is present is really the one we care about.


Setting it to { jQuery: true } is a historical artifact. There's a pull request to jQuery that explains it. The author of the request is the author of RequireJS. In brief, the goal there was to indicate that the AMD loader is able to handle the case where multiple versions of jQuery call define. jQuery would call define only if define.amd.jQuery was truthy. It's probably been used by a few versions of jQuery in the past but newer versions no longer use this flag so it is still present mainly for supporting older jQuery versions.

Upvotes: 8

Related Questions