madge icon indicating copy to clipboard operation
madge copied to clipboard

limit the level or depth of search to top level "require()"

Open babakyakhchali opened this issue 6 years ago • 4 comments

Hi I'm using this library to find problems related to circular dependencies. Is there any options for limiting madge to just top level require statements in js files? for example I need to exclude a require statement inside a function like this:

// a.js
function f(){
    const b = require('./b');  // this will not cause a cyclic dependency problem
}
const bb = require('./b'); // but this one does!!

//b.js
const a = require('./a');

babakyakhchali avatar Oct 22 '18 12:10 babakyakhchali

Hey @babakyakhchali. Thanks for contributing.

There's no explicit option for limiting the scope of parsed requires to non-dynamic imports. Adding support for that is quite a bit of work, and I'm not sure it adds much aside from short-circuiting parts of the dependency-tree generation as a performance optimization.

An inelegant workaround is that if you know of all of the files that are being dynamically imported, you could skip them using the dependencyFilter function in the madge configuration (https://github.com/pahen/madge#configuration). If the current filepath during tree generation matches an element in your list of dynamic import paths, then return false. It requires you to maintain that list, but it gets you the perf gains you're after.

mrjoelkemp avatar Oct 27 '18 12:10 mrjoelkemp

thank you very much This library helped me to find cycles and after that I solved the problems manually

babakyakhchali avatar Oct 28 '18 06:10 babakyakhchali

"aside from short-circuiting parts of the dependency-tree generation as a performance optimization." the problem is that I need to audit a large codebase to only the depth of 1, and this restriction completely bars me from being able to use this package because it will take far too long.

cakesmith avatar Jun 12 '19 23:06 cakesmith

  • In #328 some want to detect dynamic imports, which contradicts this issue. Maybe we should add an option?
  • I think we can see the cyclic dependency problem in test / runtime so we can(?) ignore this case.

PabloLION avatar Jan 28 '23 13:01 PabloLION