@@ -10,11 +10,16 @@ import type {
10
10
ResolvedModuleFull ,
11
11
TranspileOutput ,
12
12
CompilerOptions ,
13
+ SourceFile ,
14
+ Program ,
15
+ TransformerFactory ,
16
+ Bundle ,
17
+ CustomTransformerFactory ,
13
18
} from 'typescript'
14
19
15
20
import type { ConfigSet } from '../config/config-set'
16
21
import { LINE_FEED } from '../constants'
17
- import type { CompilerInstance , ResolvedModulesMap , StringMap , TTypeScript } from '../types'
22
+ import type { ResolvedModulesMap , StringMap , TsCompilerInstance , TTypeScript } from '../types'
18
23
import { rootLogger } from '../utils/logger'
19
24
import { Errors , interpolate } from '../utils/messages'
20
25
@@ -23,23 +28,22 @@ import { updateOutput } from './compiler-utils'
23
28
/**
24
29
* @internal
25
30
*/
26
- export class TsCompiler implements CompilerInstance {
31
+ export class TsCompiler implements TsCompilerInstance {
27
32
private readonly _logger : Logger
28
33
private readonly _ts : TTypeScript
29
34
private readonly _parsedTsConfig : ParsedCommandLine
30
35
private readonly _compilerCacheFS : Map < string , number > = new Map < string , number > ( )
31
- private readonly _jestCacheFS : StringMap
32
36
private readonly _initialCompilerOptions : CompilerOptions
33
37
private _compilerOptions : CompilerOptions
34
38
private _cachedReadFile : ( ( fileName : string ) => string | undefined ) | undefined
35
39
private _projectVersion = 1
36
40
private _languageService : LanguageService | undefined
41
+ program : Program | undefined
37
42
38
43
constructor ( readonly configSet : ConfigSet , readonly jestCacheFS : StringMap ) {
39
44
this . _ts = configSet . compilerModule
40
45
this . _logger = rootLogger . child ( { namespace : 'ts-compiler' } )
41
46
this . _parsedTsConfig = this . configSet . parsedTsConfig as ParsedCommandLine
42
- this . _jestCacheFS = jestCacheFS
43
47
this . _initialCompilerOptions = { ...this . _parsedTsConfig . options }
44
48
this . _compilerOptions = { ...this . _initialCompilerOptions }
45
49
if ( ! this . configSet . isolatedModules ) {
@@ -96,13 +100,13 @@ export class TsCompiler implements CompilerInstance {
96
100
// Read contents from TypeScript memory cache.
97
101
if ( ! hit ) {
98
102
const fileContent =
99
- this . _jestCacheFS . get ( normalizedFileName ) ?? this . _cachedReadFile ?.( normalizedFileName ) ?? undefined
103
+ this . jestCacheFS . get ( normalizedFileName ) ?? this . _cachedReadFile ?.( normalizedFileName ) ?? undefined
100
104
if ( fileContent ) {
101
- this . _jestCacheFS . set ( normalizedFileName , fileContent )
105
+ this . jestCacheFS . set ( normalizedFileName , fileContent )
102
106
this . _compilerCacheFS . set ( normalizedFileName , 1 )
103
107
}
104
108
}
105
- const contents = this . _jestCacheFS . get ( normalizedFileName )
109
+ const contents = this . jestCacheFS . get ( normalizedFileName )
106
110
107
111
if ( contents === undefined ) return
108
112
@@ -118,7 +122,17 @@ export class TsCompiler implements CompilerInstance {
118
122
getCurrentDirectory : ( ) => this . configSet . cwd ,
119
123
getCompilationSettings : ( ) => this . _compilerOptions ,
120
124
getDefaultLibFileName : ( ) => this . _ts . getDefaultLibFilePath ( this . _compilerOptions ) ,
121
- getCustomTransformers : ( ) => this . configSet . customTransformers ,
125
+ getCustomTransformers : ( ) => ( {
126
+ before : this . configSet . resolvedTransformers . before . map ( ( beforeTransformer ) =>
127
+ beforeTransformer . factory ( this , beforeTransformer . options ) ,
128
+ ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
129
+ after : this . configSet . resolvedTransformers . after . map ( ( afterTransformer ) =>
130
+ afterTransformer . factory ( this , afterTransformer . options ) ,
131
+ ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
132
+ afterDeclarations : this . configSet . resolvedTransformers . afterDeclarations . map ( ( afterDeclarations ) =>
133
+ afterDeclarations . factory ( this , afterDeclarations . options ) ,
134
+ ) as TransformerFactory < SourceFile | Bundle > [ ] ,
135
+ } ) ,
122
136
resolveModuleNames : ( moduleNames : string [ ] , containingFile : string ) : ( ResolvedModuleFull | undefined ) [ ] =>
123
137
moduleNames . map ( ( moduleName ) => {
124
138
const { resolvedModule } = this . _ts . resolveModuleName (
@@ -136,6 +150,7 @@ export class TsCompiler implements CompilerInstance {
136
150
this . _logger . debug ( 'created language service' )
137
151
138
152
this . _languageService = this . _ts . createLanguageService ( serviceHost , this . _ts . createDocumentRegistry ( ) )
153
+ this . program = this . _languageService . getProgram ( )
139
154
}
140
155
141
156
getResolvedModulesMap ( fileContent : string , fileName : string ) : ResolvedModulesMap {
@@ -197,7 +212,17 @@ export class TsCompiler implements CompilerInstance {
197
212
198
213
const result : TranspileOutput = this . _ts . transpileModule ( fileContent , {
199
214
fileName,
200
- transformers : this . configSet . customTransformers ,
215
+ transformers : {
216
+ before : this . configSet . resolvedTransformers . before . map ( ( beforeTransformer ) =>
217
+ beforeTransformer . factory ( this , beforeTransformer . options ) ,
218
+ ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
219
+ after : this . configSet . resolvedTransformers . after . map ( ( afterTransformer ) =>
220
+ afterTransformer . factory ( this , afterTransformer . options ) ,
221
+ ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
222
+ afterDeclarations : this . configSet . resolvedTransformers . afterDeclarations . map ( ( afterDeclarations ) =>
223
+ afterDeclarations . factory ( this , afterDeclarations . options ) ,
224
+ ) as TransformerFactory < SourceFile | Bundle > [ ] ,
225
+ } ,
201
226
compilerOptions : this . _compilerOptions ,
202
227
reportDiagnostics : this . configSet . shouldReportDiagnostics ( fileName ) ,
203
228
} )
@@ -212,9 +237,7 @@ export class TsCompiler implements CompilerInstance {
212
237
213
238
private _isFileInCache ( fileName : string ) : boolean {
214
239
return (
215
- this . _jestCacheFS . has ( fileName ) &&
216
- this . _compilerCacheFS . has ( fileName ) &&
217
- this . _compilerCacheFS . get ( fileName ) !== 0
240
+ this . jestCacheFS . has ( fileName ) && this . _compilerCacheFS . has ( fileName ) && this . _compilerCacheFS . get ( fileName ) !== 0
218
241
)
219
242
}
220
243
@@ -229,7 +252,7 @@ export class TsCompiler implements CompilerInstance {
229
252
shouldIncrementProjectVersion = true
230
253
} else {
231
254
const prevVersion = this . _compilerCacheFS . get ( fileName ) ?? 0
232
- const previousContents = this . _jestCacheFS . get ( fileName )
255
+ const previousContents = this . jestCacheFS . get ( fileName )
233
256
// Avoid incrementing cache when nothing has changed.
234
257
if ( previousContents !== contents ) {
235
258
this . _compilerCacheFS . set ( fileName , prevVersion + 1 )
0 commit comments