Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active April 8, 2018 02:26
Show Gist options
  • Save rauschma/8063621 to your computer and use it in GitHub Desktop.
Save rauschma/8063621 to your computer and use it in GitHub Desktop.
My version of the UMD pattern. https://github.com/umdjs/umd
if (typeof define !== 'function') {
// Not AMD
if (typeof require === 'function') {
// Node.js
var define = function (body) {
module.exports = body(require);
};
} else {
// Vanilla browser
var define = function (body) {
window[body.name] = body(function (id) { return window[id] });
};
}
}
define(function moduleId(require) {
var import1 = require('import1');
var import2 = require('import2');
function export1() {
...
}
function export2() {
...
}
function modulePrivate() {
...
}
return {
export1: export1,
export2: export2
};
});
@rauschma
Copy link
Author

Pattern can be pruned if you don’t have any imports or if you don’t want to support vanilla browsers.

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