Skip to content

Instantly share code, notes, and snippets.

@jamesnicholls
Created December 16, 2014 13:27
Show Gist options
  • Save jamesnicholls/086aaf41d55e2ba265ac to your computer and use it in GitHub Desktop.
Save jamesnicholls/086aaf41d55e2ba265ac to your computer and use it in GitHub Desktop.
Multi-environment module pattern
// function will execute immediately
(function(root, factory) {
if (typeof exports === 'object') {
// commonjs
module.exports = factory(
// pass dependencies via require
);
} else if (typeof define === 'function' && define.amd) {
// amd
define([
// dependencies go here
], factory);
} else {
// browser
// expose variable as property of window
root.foo = factory(
// pass dependencies here
// Dependencies should have already been included via script tags and exposed as properties of window
);
}
})
(this, function() {
// module code goes here
// any dependencies should be passed in as arguments
// return the symbol to expose
return;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment