|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
| 3 | +import ts from "typescript"; |
3 | 4 |
|
4 | 5 | const ROOT = path.resolve(import.meta.dirname, ".."); |
5 | 6 |
|
@@ -46,15 +47,28 @@ if (entryPoints.length === 0) { |
46 | 47 | process.exit(1); |
47 | 48 | } |
48 | 49 |
|
49 | | -// --- Import extraction --- |
| 50 | +// --- Import extraction (type-aware) --- |
50 | 51 |
|
51 | | -const IMPORT_RE = |
52 | | - /(?:import|export)\s+(?:type\s+)?(?:(?:\{[^}]*\}|[\w*]+(?:\s*,\s*\{[^}]*\})?)\s+from\s+)?["']([^"']+)["']/g; |
| 52 | +const tsConfig: ts.CompilerOptions = { |
| 53 | + module: ts.ModuleKind.ESNext, |
| 54 | + target: ts.ScriptTarget.ESNext, |
| 55 | + jsx: ts.JsxEmit.Preserve, |
| 56 | + sourceMap: false, |
| 57 | + declaration: false, |
| 58 | + isolatedModules: true, |
| 59 | +}; |
| 60 | + |
| 61 | +const JS_IMPORT_RE = |
| 62 | + /(?:import|export)\s+(?:(?:\{[^}]*\}|[\w*]+(?:\s*,\s*\{[^}]*\})?)\s+from\s+)?["']([^"']+)["']/g; |
53 | 63 |
|
54 | 64 | function extractImports(filePath: string): string[] { |
55 | 65 | const content = fs.readFileSync(filePath, "utf-8"); |
| 66 | + const { outputText } = ts.transpileModule(content, { |
| 67 | + compilerOptions: tsConfig, |
| 68 | + fileName: filePath, |
| 69 | + }); |
56 | 70 | const specifiers: string[] = []; |
57 | | - for (const match of content.matchAll(IMPORT_RE)) { |
| 71 | + for (const match of outputText.matchAll(JS_IMPORT_RE)) { |
58 | 72 | const spec = match[1]; |
59 | 73 | if (spec !== undefined) specifiers.push(spec); |
60 | 74 | } |
|
0 commit comments