Forked from unscriptable/AMD-factory-with-require.js
Created
February 5, 2013 01:20
-
-
Save JamesMGreene/4711301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); } | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); } | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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