Circular Dependency Plugin
Detect modules with circular dependencies when bundling with webpack.
Circular dependencies are often a necessity in complex software, the presence of a circular dependency doesn't always imply a bug, but in the case where you believe a bug exists, this module may help find it.
Differences from the upstream repo
Adds support for a faster development workflow by providing options to reduce the number of times
this plugin does its processing.
module.exports = {
...
plugins: [
new CircularDependencyPlugin({
devMode: true,
checkInterval: 2 * 60 * 1000,
...
})
]
...
}
Webpack Versions
The latest major version of this plugin 5
Basic Usage
const CircularDependencyPlugin = require('@gathertown/circular-dependency-plugin')
module.exports = {
entry: "./src/index",
plugins: [
new CircularDependencyPlugin({
exclude: /a\.js|node_modules/,
include: /dir/,
failOnError: true,
allowAsyncCycles: false,
cwd: process.cwd(),
})
]
}
Advanced Usage
const CircularDependencyPlugin = require('circular-dependency-plugin')
module.exports = {
entry: "./src/index",
plugins: [
new CircularDependencyPlugin({
onStart({ compilation }) {
console.log('start detecting webpack modules cycles');
},
onDetected({ module: webpackModuleRecord, paths, compilation }) {
compilation.errors.push(new Error(paths.join(' -> ')))
},
onEnd({ compilation }) {
console.log('end detecting webpack modules cycles');
},
})
]
}
If you have some number of cycles and want to fail if any new ones are
introduced, you can use the life cycle methods to count and fail when the
count is exceeded. (Note if you care about detecting a cycle being replaced by
another, this won't catch that.)
const CircularDependencyPlugin = require('circular-dependency-plugin')
const MAX_CYCLES = 5;
let numCyclesDetected = 0;
module.exports = {
entry: "./src/index",
plugins: [
new CircularDependencyPlugin({
onStart({ compilation }) {
numCyclesDetected = 0;
},
onDetected({ module: webpackModuleRecord, paths, compilation }) {
numCyclesDetected++;
compilation.warnings.push(new Error(paths.join(' -> ')))
},
onEnd({ compilation }) {
if (numCyclesDetected > MAX_CYCLES) {
compilation.errors.push(new Error(
`Detected ${numCyclesDetected} cycles which exceeds configured limit of ${MAX_CYCLES}`
));
}
},
})
]
}
Maintenance
This module is maintained despite few changes being necessary, please open issues if you find any bugs relating to integration with webpack core.