Created
June 6, 2018 19:20
-
-
Save jtanguy/7707db901ceb6b5c8691c23ca0daa787 to your computer and use it in GitHub Desktop.
Webpack plugin to generate the dependency graph
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To prevent broken dependencies: