Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function amdResolve(id, mod) {
var parts = mod.id.split('/'); parts.pop();
var dir = parts.join('/');
return id.replace(/^\./, dir);
}
function amdMap(args, mod) {
var mapped = [];
function require(id) {
return Module.exports[amdResolve(id, mod)];
}
for (var i=0, len = args.length; i < len; i++) {
if (args[i] == 'require') {
mapped.push(require);
continue;
}
if (args[i] == 'exports') {
mod.exports = mod.exports || {};
mapped.push(mod.exports);
continue;
}
mapped.push(require(args[i]));
}
return mapped;
}
function amdDefine() {
var args = makeArray(arguments), dependencies = [], id, factory;
if (typeof args[0] == 'string') {
id = args.shift();
}
if (isArray(args[0])) {
dependencies = args.shift();
}
factory = args.shift();
return loadedModule = new Module(id, function(exports) {
var me = this, mods = [];
function executeAMD() {
var args = amdMap(makeArray(dependencies), me);
var exported = factory.apply(me, args)
if (typeof exported == 'undefined') {
exported = me.exports;
}
exports(exported);
}
for (var i=0, len=dependencies.length; i < len; i++) {
var d = dependencies[i];
if (indexOf(['require', 'exports'], d) == -1) {
mods.push(amdResolve(d, me));
}
}
if (mods.length > 0) {
using.apply(this, mods.concat(executeAMD));
} else {
executeAMD();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.