Created
October 6, 2015 00:07
-
-
Save ldong/af308cb17883fe659d6e to your computer and use it in GitHub Desktop.
Generate dependency tree for RequireJS apps
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
// 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); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From https://gist.github.com/dustinboston/3288778