Skip to content

Instantly share code, notes, and snippets.

@dustinboston
Created August 14, 2012 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinboston/3351410 to your computer and use it in GitHub Desktop.
Save dustinboston/3351410 to your computer and use it in GitHub Desktop.
Generate dependency tree for RequireJS apps with xrayquire
xrayquire = {
// ...
getTree: function (contextName) {
var context = requirejs.s.contexts[contextName || '_'],
xray = getX(context),
traced = xray.traced,
tree = {};
sortTraceOrder(xray.traceOrder);
each(xray.traceOrder, function (id) {
var mod = traced[id];
if (isRequire(mod.map.id) && (!mod.deps || !mod.deps.length)) {
return;
}
if (mod.map.name.indexOf("_@") > -1) {
return;
}
tree[mod.map.name] = [];
each(mod.deps || [], function (dep) {
tree[mod.map.name].push(dep.name);
});
});
return tree;
},
generateUml: function () {
var dep, key, rt, uml, val, _i, _len, _ref;
rt = this.getTree();
uml = [];
for (key in rt) {
val = rt[key];
if (rt.hasOwnProperty(key)) {
_ref = val;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
dep = _ref[_i];
uml.push("[" + key + "]->[" + dep + "]");
}
}
}
return uml.join("\n");
}
}
@cybear
Copy link

cybear commented Aug 15, 2012

I don't know if require.js allows the inclusion of "_@" in your module name somehow, but if it is, then it would be safer to write

if (mod.map.name.indexOf("_@")==0) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment