pahen / madge

Create graphs from your CommonJS, AMD or ES6 module dependencies
MIT License
9.06k stars 317 forks source link

possible to skip particular imports / requires in code with a directive? #357

Open srsudar opened 1 year ago

srsudar commented 1 year ago

Is it possible to skip specific requires/imports via a directive? For example, say that I have something like:

foo.js:

exports.doFoo() => {
  const bar = require('./bar');
  return bar.doBar() + ' do foo';
};

bar.js

exports.doBar() => {
  return 'do bar';
}

exports.doSomethingElse() => {
  const foo = require('./foo');
  return 'do something else' + foo.doFoo();
}

I believe this will not give runtime errors, though it will still be a circular dependency.

I'm trying to use madge in a large codebase where resolving all circular deps immediately is impractical. As an incremental way of progressing, I'd like to enable madge but mark some imports as safe. Afaict, the only way to do this is via the excludeRegExp value in the config. I could do the following to exclude these dirs:

package.json

"madge": {
  excludeRegExp: [ 'foo.js', 'bar.js' ]
}

However, that will continue to remove those files from checking even if they are modified, and unsafe dependencies are added. I'd prefer a way to tell madge "I've vetted this import, don't worry about it." Is such a thing possible? I'm imagining something like:

exports.doFoo() => {
  // madge-ignore-circular vetted
  const bar = require('./bar');
  return bar.doBar() + ' do foo';
};

Is there some way to do this that I'm missing?

srsudar commented 1 year ago

Alternatively I could just ignore all non-top-level imports, but I don't think there's a way to do this.

srsudar commented 1 year ago

Ah, it looks like some older answers to issues might be out of date now that madge uses depenceny-tree? It looks like I can ignore all dynamically imported modules using detectiveOptions, as described here.

      "amd": {
        "skipLazyLoaded": true
      }

Some dynamic imports might still be problematic, so some per-usage vetting would still be of interest.