Skip to content

Instantly share code, notes, and snippets.

@ssnau
Created December 25, 2015 08:18
Show Gist options
  • Save ssnau/98504fc4d730a6aac5f7 to your computer and use it in GitHub Desktop.
Save ssnau/98504fc4d730a6aac5f7 to your computer and use it in GitHub Desktop.
var content = require('fs').readFileSync('./jsbundle', 'utf8');
var text = content.slice(0);
var dx = "__d(";
var count = 0;
var graph = {};
while (text.indexOf(dx) > -1) {
count++;
//console.log('#' + count);
var start = text.indexOf(dx);
var end = text.slice(start + 2).indexOf(dx);
//console.log(start, end + start + 2);
var snipet = text.slice(start, end + start + 2);
//console.log(snipet.slice(0, 10));
var matched = snipet.match(/__d\(\'([^']+)\'/);
modName = (matched) ? matched[1] : null;
//console.log(modName);
text = text.slice(end + start + 2);
if (modName) {
graph[modName] = getDeps(snipet);
}
}
//console.log(graph);
Object.keys(graph).forEach(function(name){
graph[name].forEach(function(dep) {
console.log(`"${name}" -> "${dep}";`);
});
});
function getDeps(code) {
var regs = [
/require\s*\("([^"]*)"\)/,
/require\s*\('([^']*)'\)/,
];
var deps = [];
code.split('\n')
.filter(line => line.indexOf("require") > -1)
.forEach(function(line) {
regs.forEach(function(reg) {
var matched = line.match(reg);
if (!matched) return;
deps.push(matched[1]);
});
});
return deps;
}
// can copy the result to http://arborjs.org/halfviz/#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment