| 18 | return { |
| 19 | name: "who-imports-target", |
| 20 | buildEnd() { |
| 21 | // Build reverse graph: child -> [importers...] |
| 22 | const parents = new Map<string, string[]>(); |
| 23 | for (const id of (this as any).getModuleIds()) { |
| 24 | const info = (this as any).getModuleInfo(id); |
| 25 | if (!info) continue; |
| 26 | for (const child of [...info.importedIds, ...info.dynamicallyImportedIds]) { |
| 27 | const arr = parents.get(child) ?? []; |
| 28 | arr.push(id); |
| 29 | parents.set(child, arr); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Walk upward from TARGET and print paths to entries |
| 34 | const entries = [...parents.keys()].filter((id) => { |
| 35 | const m = (this as any).getModuleInfo(id); |
| 36 | return m?.isEntry; |
| 37 | }); |
| 38 | |
| 39 | const seen = new Set<string>(); |
| 40 | const stack: string[] = []; |
| 41 | const dfs = (node: string) => { |
| 42 | if (seen.has(node)) return; |
| 43 | seen.add(node); |
| 44 | stack.push(node); |
| 45 | const ps = parents.get(node) || []; |
| 46 | if (ps.length === 0) { |
| 47 | // hit a root (likely main entry or plugin virtual) |
| 48 | console.log("\nImporter chain:"); |
| 49 | stack |
| 50 | .slice() |
| 51 | .reverse() |
| 52 | .forEach((s) => console.log(" ↳", s)); |
| 53 | } else { |
| 54 | for (const p of ps) dfs(p); |
| 55 | } |
| 56 | stack.pop(); |
| 57 | }; |
| 58 | |
| 59 | if (!parents.has(target)) { |
| 60 | console.log(`[who-imports] TARGET not in MAIN graph: ${target}`); |
| 61 | } else { |
| 62 | dfs(target); |
| 63 | } |
| 64 | }, |
| 65 | async resolveId(id: any, importer: any) { |
| 66 | const r = await (this as any).resolve(id, importer, { skipSelf: true }); |
| 67 | if (r?.id === target) { |