pahen / madge

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

Support package.json "imports" field ("#hash"-style imports) #406

Open towerofnix opened 3 months ago

towerofnix commented 3 months ago

Hey, first time using Madge so I admit I might have missed an important detail!

In my ESM Node project, I use the top-level package.json imports property to make certain import names accessible no matter what file I'm importing them from. For example:

"imports": {
    "#aggregate": "./src/util/aggregate.js",
    "#cacheable-object": "./src/data/cacheable-object.js",
    "#colors": "./src/util/colors.js",
    ...
}

This makes it so I can use this code...

import {openAggregate} from '#aggregate';
import {getColors} from '#colors';

...anywhere in my project at all! It makes keeping track of imports way easier, since I don't need to think how many levels deep the current JS file is nested, or where anything else is. (Plus I can move around the actual imported files and just update the corresponding record in package.json, instead of changing every single import, as long as the # name stays the same!)

But, I don't think Madge recognizes this syntax (and cross-references with package.json appropriately)? I created a graph using madge --image graph.svg src/upd8.js and it only contains nodes for the imports which use the normal from './write/build-modes/index.js' syntax (for example).

Unfortunately that means Madge really can't do anything for my project. Luckily my code doesn't really involve dynamic imports, so I could probably write a series of find-replaces that more or less swap out all the from '#aggregate'-style lines with the normal syntax, but that's a pretty inconvenient barrier for using Madge, obviously.