Skip to content

Instantly share code, notes, and snippets.

@simonsmith
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save simonsmith/10871480 to your computer and use it in GitHub Desktop.

Select an option

Save simonsmith/10871480 to your computer and use it in GitHub Desktop.
(function(context) {
var factories = {}, loaded = {};
var isArray = Array.isArray || function(obj) {
return obj.constructor == Array;
};
var map = Array.map || function(arr, fn, scope) {
for (var i = 0, len = arr.length, result = []; i < len; i++) {
result.push(fn.call(scope, arr[i]));
}
return result;
};
function define() {
var args = Array.prototype.slice.call(arguments), dependencies = [], id, factory;
if (typeof args[0] == 'string') {
id = args.shift();
}
if (isArray(args[0])) {
dependencies = args.shift();
}
factory = args.shift();
factories[id] = [dependencies, factory];
}
// Some UMD wrapped code might need define.amd
define.amd = true;
function require(id) {
function resolve(dep) {
var relativeParts = id.split('/'), depParts = dep.split('/'), relative = false;
relativeParts.pop();
while (depParts[0] == '..' && relativeParts.length) {
relativeParts.pop();
depParts.shift();
relative = true;
}
if (depParts[0] == '.') {
depParts.shift();
relative = true;
}
if (relative) {
depParts = relativeParts.concat(depParts);
}
return depParts.join('/');
}
var unresolved, factory, dependencies;
if (typeof loaded[id] == 'undefined') {
unresolved = factories[id];
if (unresolved) {
dependencies = unresolved[0];
factory = unresolved[1];
loaded[id] = factory.apply(undefined, map(dependencies, function(id) {
return require(resolve(id));
}));
}
}
return loaded[id];
}
// App code here
// Expose app to a global
context.myApp = require('lib/index');
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment