Skip to content

Instantly share code, notes, and snippets.

@foxbunny
Last active December 21, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save foxbunny/6352500 to your computer and use it in GitHub Desktop.
Save foxbunny/6352500 to your computer and use it in GitHub Desktop.
CoffeeScript UMD wrapper for modules that use CommonJS format. Also does some dependency checks when not using a module loader. The exported value is also put under a namespace `MyModule.somePath`.
define = ((root) ->
if typeof root.define isnt 'function' or not root.define.amd
if GLOBAL? and typeof module is 'object' and module.exports
(factory) ->
module.exports = factory(GLOBAL.require)
else
require = (dep) ->
(() =>
switch dep
when 'underscore' then @_
when 'foo' then @foo
else null
)() or throw new Error "Unmet dependency #{dep}"
(factory) ->
(root.MyModule or= {}).somePath or= {}
root.MyModule.somePath.bar = factory(require)
)(this)
define (require) ->
_ = require 'underscore'
foo = require 'foo'
###
var define;
define = (function(root) {
var require;
if (typeof root.define !== 'function' || !root.define.amd) {
if ((typeof GLOBAL !== "undefined" && GLOBAL !== null) && typeof module === 'object' && module.exports) {
return function(factory) {
return module.exports = factory(GLOBAL.require);
};
} else {
require = function(dep) {
var _this = this;
return (function() {
switch (dep) {
case 'underscore':
return _this._;
case 'foo':
return _this.foo;
default:
return null;
}
})() || (function() {
throw new Error("Unmet dependency " + dep);
})();
};
return function(factory) {
var _base;
(_base = (root.MyModule || (root.MyModule = {}))).somePath || (_base.somePath = {});
return root.MyModule.somePath.bar = factory(require);
};
}
}
})(this);
define(function(require) {
var foo, _;
_ = require('underscore');
return foo = require('foo');
});
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment