Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Created July 20, 2016 23:23
Show Gist options
  • Save AndrewRayCode/cee708b05bfd88b7d966a6e73d949a9a to your computer and use it in GitHub Desktop.
Save AndrewRayCode/cee708b05bfd88b7d966a6e73d949a9a to your computer and use it in GitHub Desktop.
// Loop over every module, which looks like
// {
// id: 1, reasons: [{ moduleId: 3 }]
// id: 2, reasons: [{ moduleId: 3 }]
// id: 3, reasons: []
// }
// and invert it to
// {
// 3: { dependencies: [ 1, 2 ] }
// }
const fileDependencies = stats.modules.reduce( ( memo, modjule ) => {
return modjule.reasons.reduce( ( memo, reason ) => {
const existing = memo[ reason.moduleId ] || [];
const newAssign = {};
newAssign[ reason.moduleId ] = existing.concat( modjule.id );
return Object.assign( {}, memo, newAssign );
}, memo );
}, {} );
@forestbelton
Copy link

modules
    .flatMap(module =>
        module.reasons.map(reason => ({ from: module.id, to: reason.moduleId }))
    )
    .reduce((graph, edge) => {
        graph[edge.to] = (graph[edge.to] || []).concat(edge.from);
        return graph;
    }, {})

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