5.2.2 • Published 3 years ago

circular-dependency-plugin v5.2.2

Weekly downloads
1,986,769
License
ISC
Repository
github
Last release
3 years ago

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.

Webpack Versions

The latest major version of this plugin 5, supports webpack 4.0.1 and greater as a peer dependency. Major version 4 of this plugin and below are intended to support webpack 3.x.x and below as a peer dependency.

Basic Usage

// webpack.config.js
const CircularDependencyPlugin = require('circular-dependency-plugin')

module.exports = {
  entry: "./src/index",
  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,
      // e.g. via import(/* webpackMode: "weak" */ './file.js')
      allowAsyncCycles: false,
      // set the current working directory for displaying module paths
      cwd: process.cwd(),
    })
  ]
}

Advanced Usage

// webpack.config.js
const CircularDependencyPlugin = require('circular-dependency-plugin')

module.exports = {
  entry: "./src/index",
  plugins: [
    new CircularDependencyPlugin({
      // `onStart` is called before the cycle detection starts
      onStart({ compilation }) {
        console.log('start detecting webpack modules cycles');
      },
      // `onDetected` is called for each module that is cyclical
      onDetected({ module: webpackModuleRecord, paths, compilation }) {
        // `paths` will be an Array of the relative module paths that make up the cycle
        // `module` will be the module record generated by webpack that caused the cycle
        compilation.errors.push(new Error(paths.join(' -> ')))
      },
      // `onEnd` is called before the cycle detection ends
      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.)

// webpack.config.js
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.

@plurall/widget-responsibles@plone/volto@obstinate/react-script@obstinate/vue-scriptdependency-builder-beta@omnia/tooling-vuenx-electron-test@travelshift/webreact-static-jurosh@kibalabs/react-staticlaem-scraping-bundler@mcgordonite/theia-application-managerreact-ui-framework-momo-testgreencheap-uikit@electron.land/electron-scripts@ez-fe/core@ez-fe/ezvite-walletreact-application-base@jucesarsilva/notifications-firestore@jucesarsilva/notifications-frontend-firestore@jucesarsilva/notifications-realtime-database@infinitebrahmanuniverse/nolb-cir@raphaelcamar/my-financial-storybook@knapsack/app-ui@everything-registry/sub-chunk-1327node-x-scripts@gedit/clinexus-scriptsng-search-dropdownnam-react-scriptsnext-circular-dependencyngapp-clintf-nx-electronadipisciquidemalleycat-frontendalva@zextras/carbonio-ui-sdkangular-cli-pugangular-cli-patchedangular-devkit-web3angular-devkit_build-angular_v7-no-vulnaws-cognito-cli@anansi/webpack-config@ailjc/rserveratali-nx-electron@alta-ng-timeago/hiamtanh-alta@augustaba/rider-cli@atir-naveed-geeksltd/webpack-configberrywallet@akphi/dev-utils@akphi/dev-utils1@axa-ch/pod-ims-contacthub@axa-ch/pod-ims-documentupload@cashfarm/angular-advanced-cli@constgen/neutrino-dependency@composiv/liveui@bafsllc/nx-serverlesscomponent-footer-vuejs@cubesoft/nx-serverless@dabapps/create-webpack-config@devpodio/application-manager@boxfoxs/scraping-bundler@brudi-toolbox/webapp-dev@brudi/brudi-toolbox-react-webapp@bud-tools/application-manager@boldr/plugin-webpack@boldr/tools@bannerwise-packages/compilerboldr-dxboldr-plugin-webpack@bananajs/nxelectron@byte-craft-react/webpack-config@cadenzatv/nx-serverlesscatalyst@genesislcap/webpack-builder@bedrockio/pagesnx-electron-reactnx-serverless-rerahradish-serverqflow-cliporseapolopelletier.toolsprireact-flokireact-flowlistspack-alpha-pluginpapel-uireact-scripts-refinedreact-pintureact-static-6react-staticpd-react-scriptszsb_widgetxiaoliang2233angularclixm-webpack-configvisual-interface-pojectvfh-iam-mwf-basevistorian-bookmarkbrowser
5.2.2

3 years ago

5.2.1

3 years ago

5.2.0

5 years ago

5.1.0

5 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.4.0

6 years ago

4.3.0

6 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.0.0

7 years ago

2.0.0

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago