Skip to content

Instantly share code, notes, and snippets.

@tanmaypatel
Created August 12, 2014 10:23
Show Gist options
  • Save tanmaypatel/43f9d5b2c292e1aa118e to your computer and use it in GitHub Desktop.
Save tanmaypatel/43f9d5b2c292e1aa118e to your computer and use it in GitHub Desktop.
Defining modules supporting AMD/CommonJS/Node and Globals!
/*
* From blogpost @ http://ifandelse.com/its-not-hard-making-your-library-support-amd-and-commonjs/
*/
(function (root, factory)
{
if(typeof define === "function" && define.amd)
{
// Now we're wrapping the factory and assigning the return
// value to the root (window) and returning it as well to
// the AMD loader.
define(["dependency"], function(dependency)
{
return (root.theModule = factory(dependency));
});
}
else if(typeof module === "object" && module.exports)
{
// I've not encountered a need for this yet, since I haven't
// run into a scenario where plain modules depend on CommonJS
// *and* I happen to be loading in a CJS browser environment
// but I'm including it for the sake of being thorough
module.exports = (root.theModule = factory(require("dependency")));
}
else
{
root.theModule = factory(root.dependency);
}
}(this, function(dependency)
{
// module code here....
return theModule;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment