Basic Usage
This code demonstrates the basic usage of the circular-dependency-plugin in a webpack configuration. It shows how to exclude certain files, include specific directories, fail on error, and set the current working directory.
const CircularDependencyPlugin = require('circular-dependency-plugin');
module.exports = {
// other webpack config
plugins: [
new CircularDependencyPlugin({
// exclude detection of files based on a RegExp
exclude: /a\.js|node_modules/,
// include specific files based on a RegExp
include: /dir/,
// add errors to webpack instead of warnings
failOnError: true,
// allow import cycles that include an asyncronous import,
allowAsyncCycles: false,
// set the current working directory for displaying module paths
cwd: process.cwd(),
})
]
};
Custom Error Message
This code sample shows how to customize the error message when a circular dependency is detected. The onDetected callback allows you to push a custom error message to the compilation errors.
const CircularDependencyPlugin = require('circular-dependency-plugin');
module.exports = {
// other webpack config
plugins: [
new CircularDependencyPlugin({
onDetected({ module: webpackModuleRecord, paths, compilation }) {
compilation.errors.push(new Error(paths.join(' -> ')));
}
})
]
};