Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesMGreene/4711301 to your computer and use it in GitHub Desktop.
Save JamesMGreene/4711301 to your computer and use it in GitHub Desktop.
// Typical AMD factory that returns a value, but uses an r-value (sync) require(),
// rather than a long, awkward dependency list.
// You cannot use module.exports or exports to declare the module:
(function (define){
define(function (require) {
"use strict";
var mod = require('pkb/modA');
return {
delegate: function () {}
};
});
}(
typeof define == 'function' && define.amd
? define
: function (factory) { module.exports = factory(require); }
));
// More CJS-like factory that uses module.exports to declare a value.
// You cannot declare the module by returning a value:
(function (define){
define(function (require, exports, module) {
"use strict";
var mod = require('pkb/modA');
module.exports = {
delegate: function () {}
};
});
}(
typeof define == 'function' && define.amd
? define
: function (factory) { factory(require, exports, module); }
));
// As long as modules are never declared as falsey values, this
// flavor will support typical AMD factories and CJS-like factories:
// HT to @bryanforbes
(function (define){
define(function (require, exports, module) {
"use strict";
var mod = require('pkb/modA');
module.exports = {
delegate: function () {}
};
// could also return here instead of using module.exports
});
}(
typeof define == 'function' && define.amd
? define
: function (factory) { module.exports = factory(require, exports, module) || exports; }
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment