@@ -10,11 +10,16 @@ import type {
1010 ResolvedModuleFull ,
1111 TranspileOutput ,
1212 CompilerOptions ,
13+ SourceFile ,
14+ Program ,
15+ TransformerFactory ,
16+ Bundle ,
17+ CustomTransformerFactory ,
1318} from 'typescript'
1419
1520import type { ConfigSet } from '../config/config-set'
1621import { LINE_FEED } from '../constants'
17- import type { CompilerInstance , ResolvedModulesMap , StringMap , TTypeScript } from '../types'
22+ import type { ResolvedModulesMap , StringMap , TsCompilerInstance , TTypeScript } from '../types'
1823import { rootLogger } from '../utils/logger'
1924import { Errors , interpolate } from '../utils/messages'
2025
@@ -23,23 +28,22 @@ import { updateOutput } from './compiler-utils'
2328/**
2429 * @internal
2530 */
26- export class TsCompiler implements CompilerInstance {
31+ export class TsCompiler implements TsCompilerInstance {
2732 private readonly _logger : Logger
2833 private readonly _ts : TTypeScript
2934 private readonly _parsedTsConfig : ParsedCommandLine
3035 private readonly _compilerCacheFS : Map < string , number > = new Map < string , number > ( )
31- private readonly _jestCacheFS : StringMap
3236 private readonly _initialCompilerOptions : CompilerOptions
3337 private _compilerOptions : CompilerOptions
3438 private _cachedReadFile : ( ( fileName : string ) => string | undefined ) | undefined
3539 private _projectVersion = 1
3640 private _languageService : LanguageService | undefined
41+ program : Program | undefined
3742
3843 constructor ( readonly configSet : ConfigSet , readonly jestCacheFS : StringMap ) {
3944 this . _ts = configSet . compilerModule
4045 this . _logger = rootLogger . child ( { namespace : 'ts-compiler' } )
4146 this . _parsedTsConfig = this . configSet . parsedTsConfig as ParsedCommandLine
42- this . _jestCacheFS = jestCacheFS
4347 this . _initialCompilerOptions = { ...this . _parsedTsConfig . options }
4448 this . _compilerOptions = { ...this . _initialCompilerOptions }
4549 if ( ! this . configSet . isolatedModules ) {
@@ -96,13 +100,13 @@ export class TsCompiler implements CompilerInstance {
96100 // Read contents from TypeScript memory cache.
97101 if ( ! hit ) {
98102 const fileContent =
99- this . _jestCacheFS . get ( normalizedFileName ) ?? this . _cachedReadFile ?.( normalizedFileName ) ?? undefined
103+ this . jestCacheFS . get ( normalizedFileName ) ?? this . _cachedReadFile ?.( normalizedFileName ) ?? undefined
100104 if ( fileContent ) {
101- this . _jestCacheFS . set ( normalizedFileName , fileContent )
105+ this . jestCacheFS . set ( normalizedFileName , fileContent )
102106 this . _compilerCacheFS . set ( normalizedFileName , 1 )
103107 }
104108 }
105- const contents = this . _jestCacheFS . get ( normalizedFileName )
109+ const contents = this . jestCacheFS . get ( normalizedFileName )
106110
107111 if ( contents === undefined ) return
108112
@@ -118,7 +122,17 @@ export class TsCompiler implements CompilerInstance {
118122 getCurrentDirectory : ( ) => this . configSet . cwd ,
119123 getCompilationSettings : ( ) => this . _compilerOptions ,
120124 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+ } ) ,
122136 resolveModuleNames : ( moduleNames : string [ ] , containingFile : string ) : ( ResolvedModuleFull | undefined ) [ ] =>
123137 moduleNames . map ( ( moduleName ) => {
124138 const { resolvedModule } = this . _ts . resolveModuleName (
@@ -136,6 +150,7 @@ export class TsCompiler implements CompilerInstance {
136150 this . _logger . debug ( 'created language service' )
137151
138152 this . _languageService = this . _ts . createLanguageService ( serviceHost , this . _ts . createDocumentRegistry ( ) )
153+ this . program = this . _languageService . getProgram ( )
139154 }
140155
141156 getResolvedModulesMap ( fileContent : string , fileName : string ) : ResolvedModulesMap {
@@ -197,7 +212,17 @@ export class TsCompiler implements CompilerInstance {
197212
198213 const result : TranspileOutput = this . _ts . transpileModule ( fileContent , {
199214 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+ } ,
201226 compilerOptions : this . _compilerOptions ,
202227 reportDiagnostics : this . configSet . shouldReportDiagnostics ( fileName ) ,
203228 } )
@@ -212,9 +237,7 @@ export class TsCompiler implements CompilerInstance {
212237
213238 private _isFileInCache ( fileName : string ) : boolean {
214239 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
218241 )
219242 }
220243
@@ -229,7 +252,7 @@ export class TsCompiler implements CompilerInstance {
229252 shouldIncrementProjectVersion = true
230253 } else {
231254 const prevVersion = this . _compilerCacheFS . get ( fileName ) ?? 0
232- const previousContents = this . _jestCacheFS . get ( fileName )
255+ const previousContents = this . jestCacheFS . get ( fileName )
233256 // Avoid incrementing cache when nothing has changed.
234257 if ( previousContents !== contents ) {
235258 this . _compilerCacheFS . set ( fileName , prevVersion + 1 )
0 commit comments