sverweij / dependency-cruiser

Validate and visualize dependencies. Your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
https://npmjs.com/dependency-cruiser
MIT License
5.15k stars 249 forks source link

Feature request: Allow type only imports to be omitted entirely from the dependency graph (typescript) #943

Closed brocef closed 2 months ago

brocef commented 2 months ago

Type only imports are always included in the output graph, even if you mark them to not be followed via the doNotFollow option. It would be nice if there was either a way to exclude them entirely from the graph, or perhaps to filter them out via the dot output filters.

Context

Typescript project

Trying to build a dependency graph of files which are actually imported after transpilation to verify the exact runtime dependencies of certain files.

Expected Behavior

n/a

Current Behavior

n/a

Possible Solution

I feel like an output filter is probably the easiest, since the output contains relevant information about the edge connecting two nodes in the graph.

Considered alternatives

Filter the dot output manually via shell script then generate image

sverweij commented 2 months ago

Hi @brocef thanks for this suggestion!

It looks like your use case is a good fit to set the tsPrecompilationDeps option to false - dependency-cruiser will then first compile typescript down to javascript and after that determine which dependencies exist.

If you create a dependency-cruiser configuration with --init in a TypeScript project/ repo it'll ask Also regard TypeScript dependencies that exist only before compilation? . If answered with Yes (which is the default), that option will be switched to true in .dependency-cruiser.js.

To switch that back, in your .dependency-cruiser.js:

//
  options: {
    // ...
    tsPreCompilationDeps: false,
    // ...
  } 
//

Type only imports are always included in the output graph, even if you mark them to not be followed via the doNotFollow option.

doNotFollow can be a bit confusing. It still includes whatever matches it (as you found). However, it doesn't follow any dependencies it would have. This is useful for e.g. to see that you have a dependency on something in node_modules but are not interested in whatever that pulls in.

brocef commented 2 months ago

Thank you, that change in config is just what I was looking for!