2022-01-26 17:16:26 -05:00
"use strict" ;
var _ _assign = ( this && this . _ _assign ) || function ( ) {
_ _assign = Object . assign || function ( t ) {
for ( var s , i = 1 , n = arguments . length ; i < n ; i ++ ) {
s = arguments [ i ] ;
for ( var p in s ) if ( Object . prototype . hasOwnProperty . call ( s , p ) )
t [ p ] = s [ p ] ;
}
return t ;
} ;
return _ _assign . apply ( this , arguments ) ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2022-06-27 14:12:49 -04:00
exports . TsJestTransformer = void 0 ;
2022-01-26 17:16:26 -05:00
var config _set _1 = require ( "./config/config-set" ) ;
var constants _1 = require ( "./constants" ) ;
2022-06-27 14:12:49 -04:00
var json _1 = require ( "./utils/json" ) ;
var jsonable _value _1 = require ( "./utils/jsonable-value" ) ;
var logger _1 = require ( "./utils/logger" ) ;
var messages _1 = require ( "./utils/messages" ) ;
var sha1 _1 = require ( "./utils/sha1" ) ;
2022-01-26 17:16:26 -05:00
var TsJestTransformer = ( function ( ) {
2022-06-27 14:12:49 -04:00
function TsJestTransformer ( ) {
this . logger = logger _1 . rootLogger . child ( { namespace : 'ts-jest-transformer' } ) ;
this . logger . debug ( 'created new transformer' ) ;
2022-01-26 17:16:26 -05:00
}
TsJestTransformer . prototype . configsFor = function ( jestConfig ) {
2022-06-27 14:12:49 -04:00
var ccs = TsJestTransformer . _cachedConfigSets . find ( function ( cs ) { return cs . jestConfig . value === jestConfig ; } ) ;
var configSet ;
if ( ccs ) {
this . _transformCfgStr = ccs . transformerCfgStr ;
configSet = ccs . configSet ;
}
else {
var serializedJestCfg _1 = json _1 . stringify ( jestConfig ) ;
var serializedCcs = TsJestTransformer . _cachedConfigSets . find ( function ( cs ) { return cs . jestConfig . serialized === serializedJestCfg _1 ; } ) ;
if ( serializedCcs ) {
serializedCcs . jestConfig . value = jestConfig ;
this . _transformCfgStr = serializedCcs . transformerCfgStr ;
configSet = serializedCcs . configSet ;
}
else {
this . logger . info ( 'no matching config-set found, creating a new one' ) ;
configSet = new config _set _1 . ConfigSet ( jestConfig ) ;
var jest _1 = _ _assign ( { } , jestConfig ) ;
var globals = ( jest _1 . globals = _ _assign ( { } , jest _1 . globals ) ) ;
jest _1 . name = undefined ;
jest _1 . cacheDirectory = undefined ;
delete globals [ 'ts-jest' ] ;
this . _transformCfgStr = new jsonable _value _1 . JsonableValue ( _ _assign ( _ _assign ( { digest : configSet . tsJestDigest , babel : configSet . babelConfig } , jest _1 ) , { tsconfig : {
options : configSet . parsedTsConfig . options ,
raw : configSet . parsedTsConfig . raw ,
} } ) ) . serialized ;
TsJestTransformer . _cachedConfigSets . push ( {
jestConfig : new jsonable _value _1 . JsonableValue ( jestConfig ) ,
configSet : configSet ,
transformerCfgStr : this . _transformCfgStr ,
} ) ;
}
2022-01-26 17:16:26 -05:00
}
return configSet ;
} ;
TsJestTransformer . prototype . process = function ( input , filePath , jestConfig , transformOptions ) {
this . logger . debug ( { fileName : filePath , transformOptions : transformOptions } , 'processing' , filePath ) ;
var result ;
var source = input ;
var configs = this . configsFor ( jestConfig ) ;
var hooks = configs . hooks ;
2022-06-27 14:12:49 -04:00
var shouldStringifyContent = configs . shouldStringifyContent ( filePath ) ;
var babelJest = shouldStringifyContent ? undefined : configs . babelJestTransformer ;
var isDefinitionFile = filePath . endsWith ( constants _1 . DECLARATION _TYPE _EXT ) ;
2022-01-26 17:16:26 -05:00
var isJsFile = constants _1 . JS _JSX _REGEX . test ( filePath ) ;
var isTsFile = ! isDefinitionFile && constants _1 . TS _TSX _REGEX . test ( filePath ) ;
2022-06-27 14:12:49 -04:00
if ( shouldStringifyContent ) {
result = "module.exports=" + json _1 . stringify ( source ) ;
2022-01-26 17:16:26 -05:00
}
else if ( isDefinitionFile ) {
result = '' ;
}
else if ( ! configs . parsedTsConfig . options . allowJs && isJsFile ) {
this . logger . warn ( { fileName : filePath } , messages _1 . interpolate ( "Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore" , { path : filePath } ) ) ;
result = source ;
}
else if ( isJsFile || isTsFile ) {
result = configs . tsCompiler . compile ( source , filePath ) ;
}
else {
var message = babelJest ? "Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files." : "Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore." ;
this . logger . warn ( { fileName : filePath } , messages _1 . interpolate ( message , { path : filePath } ) ) ;
result = source ;
}
if ( babelJest ) {
this . logger . debug ( { fileName : filePath } , 'calling babel-jest processor' ) ;
result = babelJest . process ( result , filePath , jestConfig , _ _assign ( _ _assign ( { } , transformOptions ) , { instrument : false } ) ) ;
}
if ( hooks . afterProcess ) {
this . logger . debug ( { fileName : filePath , hookName : 'afterProcess' } , 'calling afterProcess hook' ) ;
var newResult = hooks . afterProcess ( [ input , filePath , jestConfig , transformOptions ] , result ) ;
if ( newResult !== undefined ) {
return newResult ;
}
}
return result ;
} ;
TsJestTransformer . prototype . getCacheKey = function ( fileContent , filePath , _jestConfigStr , transformOptions ) {
var configs = this . configsFor ( transformOptions . config ) ;
2022-06-27 14:12:49 -04:00
this . logger . debug ( { fileName : filePath , transformOptions : transformOptions } , 'computing cache key for' , filePath ) ;
2022-01-26 17:16:26 -05:00
var _a = transformOptions . instrument , instrument = _a === void 0 ? false : _a , _b = transformOptions . rootDir , rootDir = _b === void 0 ? configs . rootDir : _b ;
2022-06-27 14:12:49 -04:00
return sha1 _1 . sha1 ( this . _transformCfgStr , '\x00' , rootDir , '\x00' , "instrument:" + ( instrument ? 'on' : 'off' ) , '\x00' , fileContent , '\x00' , filePath ) ;
2022-01-26 17:16:26 -05:00
} ;
2022-06-27 14:12:49 -04:00
TsJestTransformer . _cachedConfigSets = [ ] ;
2022-01-26 17:16:26 -05:00
return TsJestTransformer ;
} ( ) ) ;
exports . TsJestTransformer = TsJestTransformer ;