Skip to content

Wrapping style breaks AMD inclusion when built into other projects #70

@ixtli

Description

@ixtli

(This is a bit of a complicated one so thanks for bearing with me.)

The tl;dr: of this is that as of jQuery UI 1.11 all modules support loading with AMD loaders like RequireJS. Referring to this by the name jQuery breaks the way jQuery conforms to AMD and you should change the last line of your built source to reflect the convention .

The reason for this is because they wrap the entire build jquery-ui.js file in the following:

(function( factory ) {
    if ( typeof define === "function" && define.amd ) {
        // AMD. Register as an anonymous module.
        define([ "jquery" ], factory );
    } else {
        // Browser globals
        factory( jQuery );
    }
}(function( $ ) {
[...]
}));

So, every module loaded has access to the top level jQuery object $ and they all operate on it by doing $.thing(. This project appears to break the convention by referring to jquery by its exported name jQuery here: https://github.com/jquery/jquery-color/blob/master/jquery.color.js#L663

Perhaps there is a reason for this, but it causes problems with AMD loading as I will explain below. The easiest fix is to change jQuery in line 663 to $.

If you're interested in the gorier details (and why I think this works for most people) they are as follows:

I am including jquery-ui with RequireJS. jQueryUI obviously requires jQuery as a dependency. This is achieved by adding the following to your require config:

    shim: {
        'jquery-ui' : { exports: '$', deps: ['jquery'] },
        'jquery': { exports: '$' }
    },

This tells require that it should load jquery before jquery-ui and pass the exported object ($1) as an invocation parameter to jQuery UI. (Hence the }(function( $ ) { in the top snippet.) This allows us to keep jQuery AND jQuery-UI completely out of the global namespace achieving true modularity. The reason I think this works is that the default build of jQuery includes this file: https://github.com/jquery/jquery/blob/master/src/exports/global.js which installs it in (and clobbers) the global namespace as $ and jQuery. However I and other make builds that don't include the global module using the supported custom argument to grunt when making jQuery. ( Reference: https://github.com/jquery/jquery/blob/master/README.md#custom-build-examples ) This produces a build of jquery that does nothing unless executed in the presence of an AMD loader ( reference: https://github.com/jquery/jquery/blob/master/src/exports/amd.js ) for maximum conformance.

Thanks for reading a lot of words about why you should change six characters to one character in your source =)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions