Feature request: Allow module names instead of paths for `reaches`, `doNotFollow`, etc.

Author: mrmckebCreated May 9, 2023Updated May 25, 2023
Labelsenhancement

Context

I'd like to be able to pass module names to dependency-cruiser, instead of paths to node modules, so that I can match those without having to list all potential paths.

I'm using the JS API, and want to support user input in my wrapper. The below references to "chalk" are just for example.

Expected Behavior

In this scenario, I want to find all modules that reach chalk, but not modules within chalk that reference other chalk modules.

typescript
const options = {
  reaches: ['^chalk$'],
  doNotFollow: ['^chalk$'],
};

Current Behavior

The example above returns no matches. With pnpm (7.x, 8.x), I need to use the following to get the example above to work.

typescript
const options = {
  reaches: [^node_modules/.pnpm/chalk@5.2.0/node_modules/chalk'],
  doNotFollow: [^node_modules/.pnpm/[email protected]/node_modules/chalk'],
};

If someone installed a new version of chalk in the monorepo, this wouldn't catch that version.

Possible Solution

I think adding moduleNames (or modules) alongside paths might be a good solution here.

typescript
const options = {
  reaches: {
    moduleNames: ['^chalk$'],
  },
  doNotFollow: {
    moduleNames: ['^chalk$'],
  },
};

Considered alternatives

I realise that I can just pass the string, but this would match any package with "chalk" in its name, i.e. definitely-not-chalk.

typescript
const options = {
  reaches: {
    moduleNames: ['chalk'],
  },
  doNotFollow: {
    moduleNames: ['chalk'],
  },
};

I could also pass with slashes, but that could match an internal directory of the same name, i.e. utils/chalk/wrapper.ts.

typescript
const options = {
  reaches: {
    moduleNames: ['/chalk/'],
  },
  doNotFollow: {
    moduleNames: ['/chalk/'],
  },
};

Source: sverweij/dependency-cruiser