importedPackages
uses tcmts.core.ts.parse, which uses tcmts.core.ts.compiler, which uses process
The packages a scratch file imports: bare specifiers, and the JSX runtime for a .tsx file.
import { packageNameOf } from "#codebase/tcmts/core/program"; import type { CompilerOptions } from "#codebase/tcmts/core/program"; import { parse } from "#codebase/tcmts/core/ts"; import type { StringLiteral as ts_StringLiteral } from "typescript/ast"; export function importedPackages(file: string, text: string, options: CompilerOptions): string[] { const specs = parse(file, text).imports.map((n) => (n as ts_StringLiteral).text); if (file.endsWith(".tsx")) specs.push(`${options.jsxImportSource ?? "react"}/jsx-runtime`); return [...new Set(specs.flatMap((s) => packageNameOf(s) ?? []))]; }