Skip to content

Loading…

note on AMD best practices #1478

Closed
wants to merge 2 commits into from

2 participants

@sheppard

Not surprisingly, jQuery has become the go-to example for developers looking to add an AMD definition to their library. As we know, jQuery's near ubiquity means that certain AMD recommendations are not practical for jQuery. Unfortunately, the jQuery named module registration is referenced with the (reasonable) assumption that it's representative of AMD best practices. For example discussions, see Leaflet/Leaflet#1364. Leaflet/Leaflet#1778, janl/mustache.js#333, janl/mustache.js#345 and mbostock/d3#1672.

I'm hoping an extra comment will make it clear that jQuery is a special case.

@dmethvin
jQuery Foundation member

@sheppard It would be ideal if we could reference some quasi-authoritative document rather than just put the comment here. Where would be the best place to point people?

@sheppard

The best reference is probably
https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries

Though we could also link to the docs for define():
http://requirejs.org/docs/api.html#define

@sheppard sheppard referenced this pull request in mbostock/d3
Closed

Define "d3", not an anonymous module. #1764

@dmethvin dmethvin added a commit that closed this pull request
@sheppard sheppard AMD: Note on AMD best practices
Fixes #14687
Closes gh-1478
2585ee5
@dmethvin dmethvin closed this in 2585ee5
@dmethvin dmethvin added a commit that referenced this pull request
@sheppard sheppard AMD: Note on AMD best practices
Fixes #14687
Closes gh-1478
(cherry picked from commit 19220c0f29fa60d80a2beb76f048ffc8b972ba35)

Conflicts:
	AUTHORS.txt
6d76198
@sheppard sheppard deleted the sheppard:patch-1 branch
@sheppard sheppard referenced this pull request in Vestride/picturefill
@Vestride Vestride Update UMD wrapper so it always executes.
Pattern take from https://github.com/umdjs/umd and the returnExports.js example.
This fixes an issue where the script exits immediately when it detects native support for the <picture> element, skipping the module definition.
c694d1e
@mescoda mescoda pushed a commit to mescoda/jquery that referenced this pull request
@sheppard sheppard AMD: Note on AMD best practices
Fixes #14687
Closes gh-1478
(cherry picked from commit 19220c0f29fa60d80a2beb76f048ffc8b972ba35)

Conflicts:
	AUTHORS.txt
783038e
@fhemberger fhemberger added a commit that referenced this pull request
@sheppard sheppard AMD: Note on AMD best practices
Fixes #14687
Closes gh-1478
65bfd30
@bperel bperel pushed a commit that referenced this pull request
@sheppard sheppard AMD: Note on AMD best practices
Fixes #14687
Closes gh-1478
01082ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 9, 2014
  1. @sheppard

    note on AMD best practices

    sheppard committed
Commits on Jan 10, 2014
  1. @sheppard

    reference RequireJS wiki

    sheppard committed
Showing with 6 additions and 0 deletions.
  1. +6 −0 src/exports/amd.js
View
6 src/exports/amd.js
@@ -9,6 +9,12 @@ define([
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.
+
+// Note that for maximum portability, libraries that are not jQuery should
+// declare themselves as anonymous modules, and avoid setting a global if an
+// AMD loader is present. jQuery is a special case. For more information, see
+// https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#wiki-anon
+
if ( typeof define === "function" && define.amd ) {
define( "jquery", [], function() {
return jQuery;
Something went wrong with that request. Please try again.