32
32
import { Logger } from 'bs-logger'
33
33
import { readFileSync , writeFileSync } from 'fs'
34
34
import mkdirp = require( 'mkdirp' )
35
- import { basename , extname , join , normalize } from 'path'
35
+ import { basename , extname , join } from 'path'
36
36
37
37
import { ConfigSet } from '../config/config-set'
38
38
import { CompileFn , CompilerInstance , MemoryCache , TSFile , TsCompiler } from '../types'
@@ -92,60 +92,54 @@ const isValidCacheContent = (contents: string): boolean => {
92
92
* cache mode
93
93
*/
94
94
const compileAndCacheResult = (
95
- cachedir : string | undefined ,
95
+ cacheDir : string | undefined ,
96
96
memoryCache : MemoryCache ,
97
97
compileFn : CompileFn ,
98
98
getExtension : ( fileName : string ) => string ,
99
99
logger : Logger ,
100
100
) => {
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 )
108
104
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
+ } )
110
109
111
110
return output
112
111
}
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' )
122
114
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
+ } )
133
128
134
- return output
135
- }
136
- } catch ( err ) { }
129
+ return output
130
+ }
131
+ } catch ( err ) { }
137
132
138
- logger . debug ( { fileName } , 'readThrough (): cache miss' )
133
+ logger . debug ( { fileName } , 'compileAndCacheResult (): cache miss' )
139
134
140
- const [ value , sourceMap ] = compileFn ( code , normalizedFileName , lineOffset )
141
- const output = updateOutput ( value , normalizedFileName , sourceMap , getExtension )
135
+ const output = getCompileOutput ( )
142
136
143
- logger . debug ( { normalizedFileName , outputPath } , 'readThrough (): writing caches' )
137
+ logger . debug ( { fileName , outputPath } , 'compileAndCacheResult (): writing caches' )
144
138
145
- memoryCache . outputs [ normalizedFileName ] = output
146
- writeFileSync ( outputPath , output )
139
+ writeFileSync ( outputPath , output )
147
140
148
- return output
141
+ return output
142
+ }
149
143
}
150
144
}
151
145
@@ -156,16 +150,13 @@ const compileAndCacheResult = (
156
150
export const createCompilerInstance = ( configs : ConfigSet ) : TsCompiler => {
157
151
const logger = configs . logger . child ( { namespace : 'ts-compiler' } )
158
152
const {
159
- typescript : { options : compilerOptions , fileNames } ,
153
+ typescript : { options : compilerOptions } ,
160
154
tsJest,
161
155
} = configs
162
- const cachedir = configs . tsCacheDir
156
+ const cacheDir = configs . tsCacheDir
163
157
const ts = configs . compilerModule // Require the TypeScript compiler and configuration.
164
158
const extensions = [ '.ts' , '.tsx' ]
165
159
const memoryCache : MemoryCache = {
166
- contents : Object . create ( null ) ,
167
- versions : Object . create ( null ) ,
168
- outputs : Object . create ( null ) ,
169
160
resolvedModules : Object . create ( null ) ,
170
161
files : new Map < string , TSFile > ( ) ,
171
162
}
@@ -174,14 +165,21 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
174
165
extensions . push ( '.js' )
175
166
extensions . push ( '.jsx' )
176
167
}
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 , {
182
180
version : 0 ,
183
181
} )
184
- }
182
+ } )
185
183
/**
186
184
* Get the extension for a transpiled file.
187
185
*/
@@ -196,7 +194,7 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
196
194
} else {
197
195
compilerInstance = initializeTranspilerInstance ( configs , memoryCache , logger )
198
196
}
199
- const compile = compileAndCacheResult ( cachedir , memoryCache , compilerInstance . compileFn , getExtension , logger )
197
+ const compile = compileAndCacheResult ( cacheDir , memoryCache , compilerInstance . compileFn , getExtension , logger )
200
198
201
199
return { cwd : configs . cwd , compile, program : compilerInstance . program }
202
200
}
0 commit comments