Skip to content

Commit e89ec7c

Browse files
committed
impr: strip out type only imports in import tree script
!nuf
1 parent f5fe3de commit e89ec7c

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

frontend/scripts/import-tree.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import ts from "typescript";
34

45
const ROOT = path.resolve(import.meta.dirname, "..");
56

@@ -46,15 +47,28 @@ if (entryPoints.length === 0) {
4647
process.exit(1);
4748
}
4849

49-
// --- Import extraction ---
50+
// --- Import extraction (type-aware) ---
5051

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;
5363

5464
function extractImports(filePath: string): string[] {
5565
const content = fs.readFileSync(filePath, "utf-8");
66+
const { outputText } = ts.transpileModule(content, {
67+
compilerOptions: tsConfig,
68+
fileName: filePath,
69+
});
5670
const specifiers: string[] = [];
57-
for (const match of content.matchAll(IMPORT_RE)) {
71+
for (const match of outputText.matchAll(JS_IMPORT_RE)) {
5872
const spec = match[1];
5973
if (spec !== undefined) specifiers.push(spec);
6074
}

0 commit comments

Comments
 (0)