2020-03-31 20:42:07 +08: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 } ) ;
var util _1 = require ( "util" ) ;
var config _set _1 = require ( "./config/config-set" ) ;
2020-04-30 20:40:07 +08:00
var constants _1 = require ( "./constants" ) ;
2020-03-31 20:42:07 +08:00
var json _1 = require ( "./util/json" ) ;
var jsonable _value _1 = require ( "./util/jsonable-value" ) ;
var logger _1 = require ( "./util/logger" ) ;
var messages _1 = require ( "./util/messages" ) ;
var sha1 _1 = require ( "./util/sha1" ) ;
var INSPECT _CUSTOM = util _1 . inspect . custom || 'inspect' ;
var TsJestTransformer = ( function ( ) {
function TsJestTransformer ( baseOptions ) {
if ( baseOptions === void 0 ) { baseOptions = { } ; }
this . options = _ _assign ( { } , baseOptions ) ;
this . id = TsJestTransformer . _nextTransformerId ;
this . logger = logger _1 . rootLogger . child ( {
transformerId : this . id ,
namespace : 'jest-transformer' ,
} ) ;
this . logger . debug ( { baseOptions : baseOptions } , 'created new transformer' ) ;
}
Object . defineProperty ( TsJestTransformer , "_nextTransformerId" , {
get : function ( ) {
return ++ TsJestTransformer . _lastTransformerId ;
} ,
enumerable : true ,
configurable : true
} ) ;
TsJestTransformer . prototype [ INSPECT _CUSTOM ] = function ( ) {
return "[object TsJestTransformer<#" + this . id + ">]" ;
} ;
TsJestTransformer . prototype . configsFor = function ( jestConfig ) {
2020-04-30 20:40:07 +08:00
var csi = TsJestTransformer . _configSetsIndex . find ( function ( cs ) { return cs . jestConfig . value === jestConfig ; } ) ;
if ( csi )
return csi . configSet ;
var serialized = json _1 . stringify ( jestConfig ) ;
csi = TsJestTransformer . _configSetsIndex . find ( function ( cs ) { return cs . jestConfig . serialized === serialized ; } ) ;
if ( csi ) {
csi . jestConfig . value = jestConfig ;
return csi . configSet ;
2020-03-31 20:42:07 +08:00
}
2020-04-30 20:40:07 +08:00
var jestConfigObj = jestConfig ;
2020-05-15 05:33:08 +08:00
this . logger . info ( 'no matching config-set found, creating a new one' ) ;
2020-03-31 20:42:07 +08:00
var configSet = new config _set _1 . ConfigSet ( jestConfigObj , this . options , this . logger ) ;
TsJestTransformer . _configSetsIndex . push ( {
jestConfig : new jsonable _value _1 . JsonableValue ( jestConfigObj ) ,
configSet : configSet ,
} ) ;
return configSet ;
} ;
TsJestTransformer . prototype . process = function ( input , filePath , jestConfig , transformOptions ) {
this . logger . debug ( { fileName : filePath , transformOptions : transformOptions } , 'processing' , filePath ) ;
var result ;
2020-04-30 20:40:07 +08:00
var source = input ;
var configs = this . configsFor ( jestConfig ) ;
var hooks = configs . hooks ;
var stringify = configs . shouldStringifyContent ( filePath ) ;
var babelJest = stringify ? undefined : configs . babelJestTransformer ;
var isDefinitionFile = filePath . endsWith ( '.d.ts' ) ;
var isJsFile = constants _1 . JS _JSX _REGEX . test ( filePath ) ;
var isTsFile = ! isDefinitionFile && constants _1 . TS _TSX _REGEX . test ( filePath ) ;
2020-03-31 20:42:07 +08:00
if ( stringify ) {
result = "module.exports=" + JSON . stringify ( source ) ;
}
else if ( isDefinitionFile ) {
result = '' ;
}
2020-05-15 05:33:08 +08:00
else if ( ! configs . parsedTsConfig . options . allowJs && isJsFile ) {
2020-03-31 20:42:07 +08:00
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 ;
} ;
2020-04-30 20:40:07 +08:00
TsJestTransformer . prototype . getCacheKey = function ( fileContent , filePath , _jestConfigStr , transformOptions ) {
2020-03-31 20:42:07 +08:00
this . logger . debug ( { fileName : filePath , transformOptions : transformOptions } , 'computing cache key for' , filePath ) ;
2020-04-30 20:40:07 +08:00
var configs = this . configsFor ( transformOptions . config ) ;
2020-03-31 20:42:07 +08:00
var _a = transformOptions . instrument , instrument = _a === void 0 ? false : _a , _b = transformOptions . rootDir , rootDir = _b === void 0 ? configs . rootDir : _b ;
return sha1 _1 . sha1 ( configs . cacheKey , '\x00' , rootDir , '\x00' , "instrument:" + ( instrument ? 'on' : 'off' ) , '\x00' , fileContent , '\x00' , filePath ) ;
} ;
TsJestTransformer . _configSetsIndex = [ ] ;
TsJestTransformer . _lastTransformerId = 0 ;
return TsJestTransformer ;
} ( ) ) ;
exports . TsJestTransformer = TsJestTransformer ;