项目作者: aackerman

项目描述 :
Detect circular dependencies in modules compiled with Webpack
高级语言: JavaScript
项目地址: git://github.com/aackerman/circular-dependency-plugin.git
创建时间: 2015-03-26T00:43:50Z
项目社区:https://github.com/aackerman/circular-dependency-plugin

开源协议:ISC License

下载


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

  1. // webpack.config.js
  2. const CircularDependencyPlugin = require('circular-dependency-plugin')
  3. module.exports = {
  4. entry: "./src/index",
  5. plugins: [
  6. new CircularDependencyPlugin({
  7. // exclude detection of files based on a RegExp
  8. exclude: /a\.js|node_modules/,
  9. // include specific files based on a RegExp
  10. include: /dir/,
  11. // add errors to webpack instead of warnings
  12. failOnError: true,
  13. // allow import cycles that include an asyncronous import,
  14. // e.g. via import(/* webpackMode: "weak" */ './file.js')
  15. allowAsyncCycles: false,
  16. // set the current working directory for displaying module paths
  17. cwd: process.cwd(),
  18. })
  19. ]
  20. }

Advanced Usage

  1. // webpack.config.js
  2. const CircularDependencyPlugin = require('circular-dependency-plugin')
  3. module.exports = {
  4. entry: "./src/index",
  5. plugins: [
  6. new CircularDependencyPlugin({
  7. // `onStart` is called before the cycle detection starts
  8. onStart({ compilation }) {
  9. console.log('start detecting webpack modules cycles');
  10. },
  11. // `onDetected` is called for each module that is cyclical
  12. onDetected({ module: webpackModuleRecord, paths, compilation }) {
  13. // `paths` will be an Array of the relative module paths that make up the cycle
  14. // `module` will be the module record generated by webpack that caused the cycle
  15. compilation.errors.push(new Error(paths.join(' -> ')))
  16. },
  17. // `onEnd` is called before the cycle detection ends
  18. onEnd({ compilation }) {
  19. console.log('end detecting webpack modules cycles');
  20. },
  21. })
  22. ]
  23. }

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.)

  1. // webpack.config.js
  2. const CircularDependencyPlugin = require('circular-dependency-plugin')
  3. const MAX_CYCLES = 5;
  4. let numCyclesDetected = 0;
  5. module.exports = {
  6. entry: "./src/index",
  7. plugins: [
  8. new CircularDependencyPlugin({
  9. onStart({ compilation }) {
  10. numCyclesDetected = 0;
  11. },
  12. onDetected({ module: webpackModuleRecord, paths, compilation }) {
  13. numCyclesDetected++;
  14. compilation.warnings.push(new Error(paths.join(' -> ')))
  15. },
  16. onEnd({ compilation }) {
  17. if (numCyclesDetected > MAX_CYCLES) {
  18. compilation.errors.push(new Error(
  19. `Detected ${numCyclesDetected} cycles which exceeds configured limit of ${MAX_CYCLES}`
  20. ));
  21. }
  22. },
  23. })
  24. ]
  25. }

Maintenance

This module is maintained despite few changes being necessary, please open issues if you find any bugs relating to integration with webpack core.