How to tell if a javascript library supports AMD
amd, javascript
Solution
This is how jQuery declares its AMD. It's just a bunch of if statements. Unless libraries have some `library.AMD === true`, there's no way to check from the library itself.
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
module.exports = jQuery;
} else {
window.jQuery = window.$ = jQuery;
if ( typeof define === "function" && define.amd ) {
define( "jquery", [], function () { return jQuery; } );
}
}
However, there's a way to check already loaded modules. This answer states you can check `require.s.contexts._.defined`, which is an object containing the names-definition mapping of already loaded modules.
For example, if I loaded jQuery (which by default has AMD) into the page that also has RequireJS, a `jquery` property will exist in that object and contain the same jQuery object as the global. You can then compare. The following will return `true`:
require.s.contexts._.defined.jquery === jQuery
require.s.contexts._.defined.jquery === $
However, this assumes that you know the module name and/or there's a global to compare against. This might not work in all cases. For example, jQuery UI isn't just one big piece of code. It's a bunch of plugins housed under a `jquery-ui.js`. There's a possibility that either they could be named collectively or a module per widget. jQuery UI doesn't even have a global.
Problem
So I've started to learn how to use requirejs and combine it with some of the other javascript libraries available. As I understand it you need to shim all the libraries that are not Asynchronous module definition compatible (AMD), but apart from searching through the library code for "require" is there an easier way to figure out which libraries support AMD and which do not? As an example I know that jquery supports AMD but jqueryui does not, and I only know this because "someone told me".