Wenn Sie nur mit Browser (nicht node.js) zu tun, es ist nur eine Handvoll von Linien, die die Bibliothek Unterstützung sowohl AMD und Nicht-AMD zu machen.
Zum Beispiel here is the file from jQuery that does it, von denen alle bis auf vier sind Kommentare:
// Execute the factory to produce jQuery
var jQuery = factory(window);
// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// 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.
if (typeof define === "function" && define.amd) {
define("jquery", [], function() {
return jQuery;
});
}
Und ein sehr ähnlicher Ausschnitt aus Knockout kann here finden:
} else if (typeof define === 'function' && define['amd']) {
// [2] AMD anonymous module
define(['exports'], factory);
}
Beachten Sie, dass jQuery nimmt den named module Ansatz während Knockout ein anonymes Modul verwendet. jQuery verlässt auch $
und jQuery
im globalen Namespace, even when AMD is detected, während Knockout (und wahrscheinlich viele andere) tun nicht alles in globalen Namespace, wenn AMD entdeckt. Es gibt Vor-und Nachteile jeder Ansatz, durch diese Fragen dargestellt als:
See [beide Commonjs Stütz- und AMD] (http://stackoverflow.com/questions/ 13673346/supporting-both-commonjs-and-amd) und auch der Quellcode vieler gebräuchlicher Bibliotheken wie jQuery und Knockout, die sowohl AMD als auch nicht-AMD unterstützen – explunit
UMD sieht so aus, als ob es versucht, jeden Fall durch Hinzufügen von viel Komplexität zu beheben . Gibt es eine Best Practice, die einfacher und flexibler ist? –