Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created October 4, 2011 21:26
Show Gist options
  • Save jrburke/1262861 to your computer and use it in GitHub Desktop.
Save jrburke/1262861 to your computer and use it in GitHub Desktop.
Universal (AMD/Node/plain browser) module
/**
* First, better, "set exports/return" option
*/
(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments. However, if want
//to make an anonymous module, remove the 'id'
//below, and remove the id use in the define shim.
define('id', function (require) {
//If have dependencies, get them here
var a = require('a');
//Return the module definition.
return value;
});
}(typeof define === 'function' && define.amd ? define : function (id, factory) {
if (typeof module !== 'undefined' && module.exports) {
//Node
module.exports = factory(require);
} else {
//Create a global function. Only works if
//the code does not have dependencies, or
//dependencies fit the call pattern below.
window[id] = factory(function(value) {
return window[value];
});
}
}));
/**
* exports object based version, if need to make a
* circular dependency or need compatibility with
* commonjs-like environments that are not Node.
*/
(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments. However, if want
//to make an anonymous module, remove the 'id'
//below, and remove the id use in the define shim.
define('id', function (require, exports) {
//If have dependencies, get them here
var a = require('a');
//Attach properties to exports.
exports.name = value;
});
}(typeof define === 'function' && define.amd ? define : function (id, factory) {
if (typeof exports !== 'undefined') {
//commonjs
factory(require, exports);
} else {
//Create a global function. Only works if
//the code does not have dependencies, or
//dependencies fit the call pattern below.
factory(function(value) {
return window[value];
}, (window[id] = {}));
}
}));
@jrburke
Copy link
Author

jrburke commented Oct 19, 2011

@addyosmani I'm open to publishing it somewhere else than the AMD wiki. That wiki was just convenient since a few of us already have access to it. I was thinking actually not having it on the wiki portion but as part of a doc in a repo so that changes and comments on changes could be tracked easier. I can see it may be a collection of documents -- one for each sort of sub-type (something specific to jQuery plugins for example).

So all that said, I'm open to some other place to host it. If you have a specific idea or want to set something up, I'm all game.

As far as this specific boilerplate, I am feeling positive about it sticking. I am just waiting to hear back officially from one other person in the AMD world, but initial vibe was receptive. It also will work well with some changes that may be happening for the jetpack/add-on toolkit for firefox add-ons.

@addyosmani
Copy link

@jrburke Perfect.I was thinking of something along the lines of setting up a new repo or organization for taking the UMD work further. Let me setup something this evening. I'll add everyone that's been involved in discussions so far and we can see how things might shape there.

@Yehonal
Copy link

Yehonal commented Jul 17, 2014

@PandaWood
Copy link

PandaWood commented Jun 11, 2018

7 years on, and UMD to consider - are there any updates (or links for referral)?

@thadguidry
Copy link

thadguidry commented Oct 30, 2019

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