import path from 'node:path' import { createDebug } from 'obug' import { normalizePath } from '../utils/path.ts' import type { ParsedCommandLine, Program } from 'typescript' const debug = createDebug('rolldown-plugin-dts:tsc-context') // A parsed tsconfig file with its path. export interface ParsedProject { tsconfigPath: string parsedConfig: ParsedCommandLine } // A map of a source file to the project it belongs to. This makes it faster to // find the project for a source file. export type SourceFileToProjectMap = Map export interface TscContext { programs: Program[] files: Map // A map of a root tsconfig to all projects referenced from it. projects: Map } /** Creates an empty, isolated TypeScript compiler context. */ export function createContext(): TscContext { const programs: Program[] = [] const files = new Map() const projects = new Map() return { programs, files, projects } } /** * Removes a file and any dependent compiler state from a context. * * @param context - Context to invalidate. * @param file - File path to remove. Relative paths resolve from the process * working directory. */ export function invalidateContextFile(context: TscContext, file: string): void { file = normalizePath(path.resolve(file)) debug(`invalidating context file: ${file}`) context.files.delete(file) context.programs = context.programs.filter((program) => { return program .getSourceFiles() .every((sourceFile) => sourceFile.fileName !== file) }) context.projects.clear() } /** Shared compiler context used when no explicit context is provided. */ export const globalContext: TscContext = createContext()