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\textquotesingle{}t always imply a bug, but in the case where you believe a bug exists, this module may help find it.\hypertarget{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5138}{}\doxysubsection{Webpack Versions}\label{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5138}
The latest major version of this plugin {\ttfamily 5}, supports webpack {\ttfamily 4.\+0.\+1} and greater as a peer dependency. Major version {\ttfamily 4} of this plugin and below are intended to support webpack {\ttfamily 3.\+x.\+x} and below as a peer dependency.\hypertarget{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5139}{}\doxysubsection{Basic Usage}\label{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5139}

\begin{DoxyCode}{0}
\DoxyCodeLine{// webpack.config.js}
\DoxyCodeLine{const CircularDependencyPlugin = require('circular-\/dependency-\/plugin')}
\DoxyCodeLine{}
\DoxyCodeLine{module.exports = \{}
\DoxyCodeLine{  entry: "{}./src/index"{},}
\DoxyCodeLine{  plugins: [}
\DoxyCodeLine{    new CircularDependencyPlugin(\{}
\DoxyCodeLine{      // exclude detection of files based on a RegExp}
\DoxyCodeLine{      exclude: /a\(\backslash\).js|node\_modules/,}
\DoxyCodeLine{      // include specific files based on a RegExp}
\DoxyCodeLine{      include: /dir/,}
\DoxyCodeLine{      // add errors to webpack instead of warnings}
\DoxyCodeLine{      failOnError: true,}
\DoxyCodeLine{      // allow import cycles that include an asyncronous import,}
\DoxyCodeLine{      // e.g. via import(/* webpackMode: "{}weak"{} */ './file.js')}
\DoxyCodeLine{      allowAsyncCycles: false,}
\DoxyCodeLine{      // set the current working directory for displaying module paths}
\DoxyCodeLine{      cwd: process.cwd(),}
\DoxyCodeLine{    \})}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5140}{}\doxysubsection{Advanced Usage}\label{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5140}

\begin{DoxyCode}{0}
\DoxyCodeLine{// webpack.config.js}
\DoxyCodeLine{const CircularDependencyPlugin = require('circular-\/dependency-\/plugin')}
\DoxyCodeLine{}
\DoxyCodeLine{module.exports = \{}
\DoxyCodeLine{  entry: "{}./src/index"{},}
\DoxyCodeLine{  plugins: [}
\DoxyCodeLine{    new CircularDependencyPlugin(\{}
\DoxyCodeLine{      // `onStart` is called before the cycle detection starts}
\DoxyCodeLine{      onStart(\{ compilation \}) \{}
\DoxyCodeLine{        console.log('start detecting webpack modules cycles');}
\DoxyCodeLine{      \},}
\DoxyCodeLine{      // `onDetected` is called for each module that is cyclical}
\DoxyCodeLine{      onDetected(\{ module: webpackModuleRecord, paths, compilation \}) \{}
\DoxyCodeLine{        // `paths` will be an Array of the relative module paths that make up the cycle}
\DoxyCodeLine{        // `module` will be the module record generated by webpack that caused the cycle}
\DoxyCodeLine{        compilation.errors.push(new Error(paths.join(' -\/> ')))}
\DoxyCodeLine{      \},}
\DoxyCodeLine{      // `onEnd` is called before the cycle detection ends}
\DoxyCodeLine{      onEnd(\{ compilation \}) \{}
\DoxyCodeLine{        console.log('end detecting webpack modules cycles');}
\DoxyCodeLine{      \},}
\DoxyCodeLine{    \})}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}


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\textquotesingle{}t catch that.)


\begin{DoxyCode}{0}
\DoxyCodeLine{// webpack.config.js}
\DoxyCodeLine{const CircularDependencyPlugin = require('circular-\/dependency-\/plugin')}
\DoxyCodeLine{}
\DoxyCodeLine{const MAX\_CYCLES = 5;}
\DoxyCodeLine{let numCyclesDetected = 0;}
\DoxyCodeLine{}
\DoxyCodeLine{module.exports = \{}
\DoxyCodeLine{  entry: "{}./src/index"{},}
\DoxyCodeLine{  plugins: [}
\DoxyCodeLine{    new CircularDependencyPlugin(\{}
\DoxyCodeLine{      onStart(\{ compilation \}) \{}
\DoxyCodeLine{        numCyclesDetected = 0;}
\DoxyCodeLine{      \},}
\DoxyCodeLine{      onDetected(\{ module: webpackModuleRecord, paths, compilation \}) \{}
\DoxyCodeLine{        numCyclesDetected++;}
\DoxyCodeLine{        compilation.warnings.push(new Error(paths.join(' -\/> ')))}
\DoxyCodeLine{      \},}
\DoxyCodeLine{      onEnd(\{ compilation \}) \{}
\DoxyCodeLine{        if (numCyclesDetected > MAX\_CYCLES) \{}
\DoxyCodeLine{          compilation.errors.push(new Error(}
\DoxyCodeLine{            `Detected \$\{numCyclesDetected\} cycles which exceeds configured limit of \$\{MAX\_CYCLES\}`}
\DoxyCodeLine{          ));}
\DoxyCodeLine{        \}}
\DoxyCodeLine{      \},}
\DoxyCodeLine{    \})}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5141}{}\doxysubsection{Maintenance}\label{md_sfcode_node_modules_circular_dependency_plugin_README_autotoc_md5141}
This module is maintained despite few changes being necessary, please open issues if you find any bugs relating to integration with webpack core. 