11import path from 'node:path'
22import Debug from 'debug'
33import ts from 'typescript'
4- import { fsSystem , memorySystem } from './system.ts'
4+ import { createFsSystem , createMemorySystem } from './system.ts'
55import { createVueProgramFactory } from './vue.ts'
66import type { TsConfigJson } from 'get-tsconfig'
77import type { SourceMapInput } from 'rolldown'
88
9+ export interface TscContext {
10+ programs : ts . Program [ ]
11+ files : Map < string , string >
12+ }
13+
14+ export interface TscModule {
15+ program : ts . Program
16+ file : ts . SourceFile
17+ }
18+
19+ export interface TscOptions {
20+ tsconfig ?: string
21+ tsconfigRaw : TsConfigJson
22+ cwd : string
23+ incremental : boolean
24+ entries ?: string [ ]
25+ id : string
26+ vue ?: boolean
27+ context ?: TscContext
28+ }
29+
930const debug = Debug ( 'rolldown-plugin-dts:tsc' )
1031debug ( `loaded typescript: ${ ts . version } ` )
1132
12- const programs : ts . Program [ ] = [ ]
33+ export function createContext ( ) : TscContext {
34+ const programs : ts . Program [ ] = [ ]
35+ const files = new Map < string , string > ( )
36+ return { programs, files }
37+ }
38+
39+ const globalContext : TscContext = createContext ( )
1340
1441const formatHost : ts . FormatDiagnosticsHost = {
1542 getCurrentDirectory : ( ) => ts . sys . getCurrentDirectory ( ) ,
@@ -32,24 +59,9 @@ const defaultCompilerOptions: ts.CompilerOptions = {
3259 moduleResolution : ts . ModuleResolutionKind . Bundler ,
3360}
3461
35- export interface TscModule {
36- program : ts . Program
37- file : ts . SourceFile
38- }
39-
40- export interface TscOptions {
41- tsconfig ?: string
42- tsconfigRaw : TsConfigJson
43- cwd : string
44- incremental : boolean
45- entries ?: string [ ]
46- id : string
47- vue ?: boolean
48- }
49-
5062function createOrGetTsModule ( options : TscOptions ) : TscModule {
51- const { id, entries } = options
52- const program = programs . find ( ( program ) => {
63+ const { id, entries, context = globalContext } = options
64+ const program = context . programs . find ( ( program ) => {
5365 const roots = program . getRootFileNames ( )
5466 if ( entries ) {
5567 return entries . every ( ( entry ) => roots . includes ( entry ) )
@@ -67,7 +79,7 @@ function createOrGetTsModule(options: TscOptions): TscModule {
6779 const module = createTsProgram ( options )
6880 debug ( `created program for module: ${ id } ` )
6981
70- programs . push ( module . program )
82+ context . programs . push ( module . program )
7183 return module
7284}
7385
@@ -80,9 +92,15 @@ function createOrGetTsModule(options: TscOptions): TscModule {
8092 * changes) the build will be super fast. If `incremental` is `false`, the
8193 * `.tsbuildinfo` file will only be written to the memory.
8294 */
83- function buildSolution ( tsconfig : string , incremental : boolean ) {
95+ function buildSolution (
96+ tsconfig : string ,
97+ incremental : boolean ,
98+ context : TscContext ,
99+ ) {
84100 debug ( `building projects for ${ tsconfig } with incremental: ${ incremental } ` )
85- const system = incremental ? fsSystem : memorySystem
101+ const system = ( incremental ? createFsSystem : createMemorySystem ) (
102+ context . files ,
103+ )
86104
87105 const host = ts . createSolutionBuilderHost ( system )
88106 const builder = ts . createSolutionBuilder ( host , [ tsconfig ] , {
@@ -105,7 +123,9 @@ function createTsProgram({
105123 incremental,
106124 vue,
107125 cwd,
126+ context = globalContext ,
108127} : TscOptions ) : TscModule {
128+ const fsSystem = createFsSystem ( context . files )
109129 const parsedCmd = ts . parseJsonConfigFileContent (
110130 tsconfigRaw ,
111131 fsSystem ,
@@ -114,7 +134,7 @@ function createTsProgram({
114134
115135 // If the tsconfig has project references, build the project tree.
116136 if ( tsconfig && parsedCmd . projectReferences ?. length ) {
117- buildSolution ( tsconfig , incremental )
137+ buildSolution ( tsconfig , incremental , context )
118138 }
119139
120140 const compilerOptions : ts . CompilerOptions = {
0 commit comments