Skip to content

Instantly share code, notes, and snippets.

@jtanguy
Created June 6, 2018 19:20
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtanguy/7707db901ceb6b5c8691c23ca0daa787 to your computer and use it in GitHub Desktop.
Save jtanguy/7707db901ceb6b5c8691c23ca0daa787 to your computer and use it in GitHub Desktop.
Webpack plugin to generate the dependency graph
const path = require('path');
class DependencyGraphPlugin {
apply(compiler) {
compiler.hooks.emit.tap("DependencyGraphPlugin", (compilation) => {
let deps = [];
compilation.modules.forEach(m => {
// console.log(m)
const file = path.relative('', m.resource)
const issuer= m.issuer && path.relative('', m.issuer.resource)
if(file !== issuer){
deps.push([issuer, file]);
}
})
// Insert this list into the webpack build as a new file asset:
const source = `digraph sources {
rankdir=LR;
${deps.map(d => `"${d[0]}" -> "${d[1]}";`).join('\n\t')}
}`;
compilation.assets['graph.dot'] = {
source: function() {
return source;
},
size: function() {
return source.length;
}
};
})
}
}
module.exports = DependencyGraphPlugin
@mykeels
Copy link

mykeels commented Nov 9, 2018

To prevent broken dependencies:

class DependencyGraphPlugin {
  apply(compiler) {
    compiler.hooks.emit.tap("DependencyGraphPlugin", (compilation) => {
      let deps = [];

      compilation.modules.forEach(m =>  {
        if (m.resource) {
          const file = path.relative('', m.resource)
          const issuer= m.issuer && m.issuer.resource && path.relative('', m.issuer.resource)

          if(file !== issuer){
            deps.push([issuer, file]);
          }
        }
        
      })
      // Insert this list into the webpack build as a new file asset:
      const source = `digraph sources {
        rankdir=LR;
        ${deps.map(d => `"${d[0]}" -> "${d[1]}";`).join('\n\t')} 
      }`;
      compilation.assets['graph.dot'] = {
        source: function() {
          return source;
        },
        size: function() {
          return source.length;
        }
      };
    })
  }
}

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