Skip to content

Instantly share code, notes, and snippets.

@bttmly
Last active February 11, 2024 23:02
Show Gist options
  • Save bttmly/f7172c00fdcd45b9415a to your computer and use it in GitHub Desktop.
Save bttmly/f7172c00fdcd45b9415a to your computer and use it in GitHub Desktop.
(function(root, factory) {
// Set up Module appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'exports'], function(_, exports) {
// Export global even in AMD case in case this script is loaded with others that may still expect a global "Module".
root.Module = factory(root, exports, $);
});
// Next for Node.js or CommonJS.
} else if (typeof exports !== 'undefined') {
var _ = require('underscore');
factory(root, exports, _);
//Finally, as a browser global.
} else {
root.Module = factory(root, {}, root._ );
}
}(this, function(root, Module, _, ) {
// your library here.
return Module;
}));
@bttmly
Copy link
Author

bttmly commented Jun 29, 2014

Set up a JS project to be environment agnostic. Shamelessly stolen from the beginning of Backbone

This example has Underscore as an example dependency. Use the pattern demonstrated to add any dependencies to your library. "Module" should be replaced with the name of your library's main object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment