Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created November 23, 2011 05:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrburke/1387943 to your computer and use it in GitHub Desktop.
Save jrburke/1387943 to your computer and use it in GitHub Desktop.
Common module boilerplate that sets export value
// Define a module "a" that depends another module called "b". If
// that other module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you
// can remove the `root` use and the passing `this` as the first arg to
// the top function.
(function(root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(require('b'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], factory);
} else {
// Browser globals
root.a = factory(root.b);
}
}(this, function(b) {
//use b in some fashion.
//Just return a value to define the module export.
return {};
}));
@jrburke
Copy link
Author

jrburke commented Nov 23, 2011

This is a variation based on this gist for jQuery plugins which are all inspired by the boilerplate used in q

@jrburke
Copy link
Author

jrburke commented Jan 18, 2012

Please see the umdjs/umd project for different variations on this kind of boilerplate depending on what you want to support.

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