3232import { Logger } from 'bs-logger'
3333import { readFileSync , writeFileSync } from 'fs'
3434import mkdirp = require( 'mkdirp' )
35- import { basename , extname , join , normalize } from 'path'
35+ import { basename , extname , join } from 'path'
3636
3737import { ConfigSet } from '../config/config-set'
3838import { CompileFn , CompilerInstance , MemoryCache , TSFile , TsCompiler } from '../types'
@@ -92,60 +92,54 @@ const isValidCacheContent = (contents: string): boolean => {
9292 * cache mode
9393 */
9494const compileAndCacheResult = (
95- cachedir : string | undefined ,
95+ cacheDir : string | undefined ,
9696 memoryCache : MemoryCache ,
9797 compileFn : CompileFn ,
9898 getExtension : ( fileName : string ) => string ,
9999 logger : Logger ,
100100) => {
101- if ( ! cachedir ) {
102- return ( code : string , fileName : string , lineOffset ?: number ) => {
103- const normalizedFileName = normalize ( fileName )
104-
105- logger . debug ( { normalizedFileName } , 'readThrough(): no cache' )
106-
107- const [ value , sourceMap ] = compileFn ( code , normalizedFileName , lineOffset )
101+ return ( code : string , fileName : string , lineOffset ?: number ) => {
102+ function getCompileOutput ( ) : string {
103+ const [ value , sourceMap ] = compileFn ( code , fileName , lineOffset )
108104 const output = updateOutput ( value , fileName , sourceMap , getExtension )
109- memoryCache . outputs [ normalizedFileName ] = output
105+ memoryCache . files . set ( fileName , {
106+ ...memoryCache . files . get ( fileName ) ! ,
107+ output,
108+ } )
110109
111110 return output
112111 }
113- }
114-
115- // Make sure the cache directory exists before continuing.
116- mkdirp . sync ( cachedir )
117- try {
118- const resolvedModulesCache = readFileSync ( getResolvedModulesCache ( cachedir ) , 'utf-8' )
119- /* istanbul ignore next (covered by e2e) */
120- memoryCache . resolvedModules = JSON . parse ( resolvedModulesCache )
121- } catch ( e ) { }
112+ if ( ! cacheDir ) {
113+ logger . debug ( { fileName } , 'compileAndCacheResult(): no cache' )
122114
123- return ( code : string , fileName : string , lineOffset ?: number ) => {
124- const normalizedFileName = normalize ( fileName )
125- const cachePath = join ( cachedir , getCacheName ( code , normalizedFileName ) )
126- const extension = getExtension ( normalizedFileName )
127- const outputPath = `${ cachePath } ${ extension } `
128- try {
129- const output = readFileSync ( outputPath , 'utf8' )
130- if ( isValidCacheContent ( output ) ) {
131- logger . debug ( { normalizedFileName } , 'readThrough(): cache hit' )
132- memoryCache . outputs [ normalizedFileName ] = output
115+ return getCompileOutput ( )
116+ } else {
117+ const cachePath = join ( cacheDir , getCacheName ( code , fileName ) )
118+ const extension = getExtension ( fileName )
119+ const outputPath = `${ cachePath } ${ extension } `
120+ try {
121+ const output = readFileSync ( outputPath , 'utf8' )
122+ if ( isValidCacheContent ( output ) ) {
123+ logger . debug ( { fileName } , 'compileAndCacheResult(): cache hit' )
124+ memoryCache . files . set ( fileName , {
125+ ...memoryCache . files . get ( fileName ) ! ,
126+ output,
127+ } )
133128
134- return output
135- }
136- } catch ( err ) { }
129+ return output
130+ }
131+ } catch ( err ) { }
137132
138- logger . debug ( { fileName } , 'readThrough (): cache miss' )
133+ logger . debug ( { fileName } , 'compileAndCacheResult (): cache miss' )
139134
140- const [ value , sourceMap ] = compileFn ( code , normalizedFileName , lineOffset )
141- const output = updateOutput ( value , normalizedFileName , sourceMap , getExtension )
135+ const output = getCompileOutput ( )
142136
143- logger . debug ( { normalizedFileName , outputPath } , 'readThrough (): writing caches' )
137+ logger . debug ( { fileName , outputPath } , 'compileAndCacheResult (): writing caches' )
144138
145- memoryCache . outputs [ normalizedFileName ] = output
146- writeFileSync ( outputPath , output )
139+ writeFileSync ( outputPath , output )
147140
148- return output
141+ return output
142+ }
149143 }
150144}
151145
@@ -156,16 +150,13 @@ const compileAndCacheResult = (
156150export const createCompilerInstance = ( configs : ConfigSet ) : TsCompiler => {
157151 const logger = configs . logger . child ( { namespace : 'ts-compiler' } )
158152 const {
159- typescript : { options : compilerOptions , fileNames } ,
153+ typescript : { options : compilerOptions } ,
160154 tsJest,
161155 } = configs
162- const cachedir = configs . tsCacheDir
156+ const cacheDir = configs . tsCacheDir
163157 const ts = configs . compilerModule // Require the TypeScript compiler and configuration.
164158 const extensions = [ '.ts' , '.tsx' ]
165159 const memoryCache : MemoryCache = {
166- contents : Object . create ( null ) ,
167- versions : Object . create ( null ) ,
168- outputs : Object . create ( null ) ,
169160 resolvedModules : Object . create ( null ) ,
170161 files : new Map < string , TSFile > ( ) ,
171162 }
@@ -174,14 +165,21 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
174165 extensions . push ( '.js' )
175166 extensions . push ( '.jsx' )
176167 }
177- // Initialize files from TypeScript into project.
178- for ( const path of fileNames ) {
179- const normalizedFilePath = normalize ( path )
180- memoryCache . versions [ normalizedFilePath ] = 1
181- memoryCache . files . set ( normalizedFilePath , {
168+ if ( cacheDir ) {
169+ // Make sure the cache directory exists before continuing.
170+ mkdirp . sync ( cacheDir )
171+ try {
172+ const resolvedModulesCache = readFileSync ( getResolvedModulesCache ( cacheDir ) , 'utf-8' )
173+ /* istanbul ignore next (covered by e2e) */
174+ memoryCache . resolvedModules = JSON . parse ( resolvedModulesCache )
175+ } catch ( e ) { }
176+ }
177+ /* istanbul ignore next (we leave this for e2e) */
178+ configs . jest . setupFiles . concat ( configs . jest . setupFilesAfterEnv ) . forEach ( setupFile => {
179+ memoryCache . files . set ( setupFile , {
182180 version : 0 ,
183181 } )
184- }
182+ } )
185183 /**
186184 * Get the extension for a transpiled file.
187185 */
@@ -196,7 +194,7 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
196194 } else {
197195 compilerInstance = initializeTranspilerInstance ( configs , memoryCache , logger )
198196 }
199- const compile = compileAndCacheResult ( cachedir , memoryCache , compilerInstance . compileFn , getExtension , logger )
197+ const compile = compileAndCacheResult ( cacheDir , memoryCache , compilerInstance . compileFn , getExtension , logger )
200198
201199 return { cwd : configs . cwd , compile, program : compilerInstance . program }
202200}
0 commit comments