Skip to content

Instantly share code, notes, and snippets.

@MarcoPolo
Last active November 10, 2016 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoPolo/db5ffb4ca5f14d42d5f242a6af0b2524 to your computer and use it in GitHub Desktop.
Save MarcoPolo/db5ffb4ca5f14d42d5f242a6af0b2524 to your computer and use it in GitHub Desktop.
Traverse inverse dependency react native
counts = {}
a = {} // your inverse dep map
// helper to return a string of n spaces... omg this is left pad
function s (n) { let k = ''; for (i = 0; i<n; i++) {k = k + ' '}; return k}
function f (k, ttl, indent, seen) {
if (seen[k]) { console.warn('circular dep found', k, seen) }
let kl = (''+k).length; counts[k] = (counts[k] || 0) + 1 ;return (ttl > 0 && a[k] && a[k].length ? k + ' -> ' + a[k].map(i => f(i, ttl - 1, indent + s(4+kl), Object.assign(seen, {[k]: true}))).join('\n' + s(kl + 4) + indent) : k)
}
// Figure out most used deps
Object.keys(counts).map(i => [counts[i], i]).sort(([a, _a], [b, _b]) => b - a).slice(0,10)
// call like so
'\n'+Object.keys(a).map(i => f(i, 2, '', {})) && true // (remove the && true to see the string printout)
@MarcoPolo
Copy link
Author

for parsing what the module names are from the bundle:

wget http://localhost:8081/index.android.bundle\?platform\=android\&dev\=true\&hot\=true\&minify\=false
cat index.android.bundle\?platform=android\&dev=true\&hot=true\&minify=false | grep -o 'require([0-9]*\ \/\*\([^;]*\) \*\/);' | sed 's/.*require(\([0-9]*\)\ \/\* \(.*\) \*\/).*;/\1: "\2",/' > parsed-output

@MarcoPolo
Copy link
Author

If you turn those into a names Obj. you can then do:

function f (k, ttl, indent, seen) { 
  let name = names[k] || k
  if (seen[k]) { console.warn('circular dep found', k, seen) }
  let kl = (''+name).length; counts[k] = (counts[k] || 0) + 1 ;return (ttl > 0 && a[k] && a[k].length ? name + ' -> ' + a[k].map(i => f(i, ttl - 1, indent + s(4+kl), Object.assign(seen, {[k]: true}))).join('\n' + s(kl + 4) + indent) : name)
}

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