importedPackages

tcmts.cli.provided.importedPackages · #7ic2osz6 · term · runs on the server

uses tcmts.core.program.packageNameOf, which uses node:module

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) ?? []))];
}

uses

used by