Forked from unscriptable/0-final-captureDefines.js
Last active
August 29, 2015 14:06
-
-
Save briancavalier/766fcb240508d3f8cdb6 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
module.exports = captureDefines; | |
function captureDefines (amdEval) { | |
return function (load) { | |
var result, isAnon, _define; | |
result = { named: [] }; | |
_define = function captureDefine () { | |
var args, def; | |
args = copy(arguments); | |
// last arg is always the factory (or a plain value) | |
def = { factory: ensureFactory(args.pop()) }; | |
// if there are other args | |
if (args.length > 0) { | |
// get list of dependency module ids | |
def.depsList = args.pop(); | |
// if this is a string, then there are no deps | |
if (typeof def.depsList === 'string') { | |
def.name = def.depsList; | |
delete def.depsList; | |
} | |
else { | |
def.name = args.pop() || null; | |
} | |
if (args.length > 0) { | |
throw new Error('Unparsable AMD define arguments [' | |
+ copy(arguments) | |
+ '] found in ' + load.name | |
); | |
} | |
} | |
if (!def.name) { | |
if (isAnon) { | |
throw new Error('Multiple anon defines in' + load.name); | |
} | |
isAnon = true; | |
result.anon = def; | |
} | |
else { | |
result.named.push(def); | |
} | |
}; | |
// indicate we are AMD and we can handle the jqueries | |
_define.amd = { jQuery: {} }; | |
amdEval(global, _define, load.source); | |
if (!result) { | |
throw new Error('AMD define not called in ' + load.name); | |
} | |
return result; | |
}; | |
} | |
function ensureFactory (thing) { | |
return typeof thing === 'function' | |
? thing | |
: function () { return thing; } | |
} | |
var slice = Array.prototype.slice; | |
function copy (thing) { | |
return slice.call(thing); | |
} |
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
module.exports = captureDefines; | |
function captureDefines (amdEval) { | |
var inst = new AmdCapture(amdEval); | |
return function (load) { return inst.capture(load); }; | |
} | |
function AmdCapture (amdEval) { | |
var self = this; | |
this.eval = amdEval; | |
this.define = function () { return self._define.apply(self, arguments); } | |
// indicate we are AMD and we can handle the jqueries | |
this.define.amd = { jQuery: {} }; | |
} | |
AmdCapture.prototype = { | |
capture: function (load) { | |
this.result = { named: [], isAnon: false }; | |
try { | |
this.eval(global, this.define, load.source); | |
} | |
catch (ex) { | |
ex.message += ' in ' + load.name; | |
throw ex; | |
} | |
if (!this.result) { | |
throw new Error('AMD define not called in ' + load.name); | |
} | |
return this.result; | |
}, | |
_define: function () { | |
var result, args, def; | |
result = this.result; | |
args = copy(arguments); | |
// last arg is always the factory (or a plain value) | |
def = { factory: ensureFactory(args.pop()) }; | |
// if there are other args | |
if (args.length > 0) { | |
// get list of dependency module ids | |
def.depsList = args.pop(); | |
// if this is a string, then there are no deps | |
if (typeof def.depsList === 'string') { | |
def.name = def.depsList; | |
delete def.depsList; | |
} | |
else { | |
def.name = args.pop() || null; | |
} | |
if (args.length > 0) { | |
throw new Error('Unparsable AMD define arguments [' | |
+ copy(arguments) + ']' | |
); | |
} | |
} | |
if (!def.name) { | |
if (result.isAnon) { | |
throw new Error('Multiple anon defines'); | |
} | |
result.isAnon = true; | |
result.anon = def; | |
} | |
else { | |
result.named.push(def); | |
} | |
} | |
}; | |
function ensureFactory (thing) { | |
return typeof thing === 'function' | |
? thing | |
: function () { return thing; } | |
} | |
var slice = Array.prototype.slice; | |
function copy (thing) { | |
return slice.call(thing); | |
} | |
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
module.exports = captureDefines; | |
function captureDefines (amdEval) { | |
var context = { | |
eval: amdEval, | |
capture: capture, | |
define: define | |
}; | |
define.amd = { jQuery: {} }; | |
return function (load) { return context.capture(load); }; | |
function define () { | |
return _define(context, copy(arguments)); | |
} | |
} | |
function capture (load) { | |
this.result = { named: [], isAnon: false, anon: void 0 }; | |
try { | |
this.eval(global, this.define, load.source); | |
} | |
catch (ex) { | |
ex.message += ' in ' + load.name; | |
throw ex; | |
} | |
if (!this.result) { | |
throw new Error('AMD define not called in ' + load.name); | |
} | |
return this.result; | |
} | |
function _define (context, arguments) { | |
var result, args, def, undef; | |
result = context.result; | |
args = copy(arguments); | |
// last arg is always the factory (or a plain value) | |
def = { | |
factory: ensureFactory(args.pop()), | |
depsList: undef, | |
name: undef | |
}; | |
// if there are other args | |
if (args.length > 0) { | |
// get list of dependency module ids | |
def.depsList = args.pop(); | |
// if this is a string, then there are no deps | |
if (typeof def.depsList === 'string') { | |
def.name = def.depsList; | |
delete def.depsList; | |
} | |
else { | |
def.name = args.pop() || null; | |
} | |
if (args.length > 0) { | |
throw new Error('Unparsable AMD define arguments [' | |
+ copy(arguments) + ']' | |
); | |
} | |
} | |
if (!def.name) { | |
if (result.isAnon) { | |
throw new Error('Multiple anon defines'); | |
} | |
result.isAnon = true; | |
result.anon = def; | |
} | |
else { | |
result.named.push(def); | |
} | |
} | |
function ensureFactory (thing) { | |
return typeof thing === 'function' | |
? thing | |
: function () { return thing; } | |
} | |
var slice = Array.prototype.slice; | |
function copy (thing) { | |
return slice.call(thing); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment