Created
April 7, 2011 23:00
-
-
Save danwrong/908968 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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