Skip to content

Instantly share code, notes, and snippets.

@dustinboston
Created August 7, 2012 19:50
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save dustinboston/3288778 to your computer and use it in GitHub Desktop.
Save dustinboston/3288778 to your computer and use it in GitHub Desktop.
Generate dependency tree for RequireJS apps
// Usage:
//
// 1. Put this in the file that gets first loaded by RequireJS
// 2. Once the page has loaded, type window.rtree.map() in the console
// This will map all dependencies in the window.rtree.tree object
// 3. To generate UML call window.rtree.toUml(). The output can be used
// here: http://yuml.me/diagram/scruffy/class/draw
requirejs.onResourceLoad = function (context, map, depMaps) {
if (!window.rtree) {
window.rtree = {};
window.rtree.tree = {};
window.rtree.map = function() {
var dep, key, rt, val, _i, _len, _ref;
rt = rtree.tree;
for (key in rt) {
val = rt[key];
if (rt.hasOwnProperty(key)) {
_ref = val.deps;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
dep = _ref[_i];
val.map[dep] = rt[dep];
}
}
}
};
window.rtree.toUml = function() {
var dep, key, rt, uml, val, _i, _len, _ref;
rt = rtree.tree;
uml = [];
for (key in rt) {
val = rt[key];
if (rt.hasOwnProperty(key)) {
_ref = val.deps;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
dep = _ref[_i];
uml.push("[" + key + "]->[" + dep + "]");
}
}
}
return uml.join("\n");
};
}
r = window.rtree.tree;
o = {deps: [], map: {}};
if (!r[map.name]) {
r[map.name] = o;
}
if (map.parentMap && map.parentMap.name) {
if (!r[map.parentMap.name]) {
r[map.parentMap.name] = o;
}
if (map.parentMap.name !== map.name) {
r[map.parentMap.name].deps.push(map.name);
}
}
};
@gavJackson
Copy link

wow that is very cool, just tried it on my app and worked a treat! nice one

@Birowsky
Copy link

Masterpiece. But I do have to ask: why were there quite a bit of loopbacks in my result?

@jr-codes
Copy link

@Birowsky Because r[map.name] on line 47 and r[map.parentMap.name] on line 51 both reference the same object.

I created a fork and fixed the loopback issue: https://gist.github.com/zarjay/6118461

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