Skip to content

Commit 4c88da5

Browse files
jsaguetahnpnl
authored andcommitted
perf: remove ts resolved module cache file
1 parent 0785348 commit 4c88da5

File tree

2 files changed

+5
-99
lines changed

2 files changed

+5
-99
lines changed

src/legacy/ts-jest-transformer.spec.ts

+2-73
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ import fs from 'fs'
22
import path from 'path'
33

44
import { LogLevels } from 'bs-logger'
5-
import { removeSync, writeFileSync } from 'fs-extra'
5+
import { removeSync } from 'fs-extra'
66

7-
import { createConfigSet } from '../__helpers__/fakers'
87
import { logTargetMock } from '../__helpers__/mocks'
9-
import type { DepGraphInfo } from '../types'
10-
import { stringify } from '../utils'
118
import { importer } from '../utils/importer'
12-
import { sha1 } from '../utils/sha1'
139

1410
import { TsJestCompiler } from './compiler'
15-
import { CACHE_KEY_EL_SEPARATOR, TsJestTransformer } from './ts-jest-transformer'
11+
import { TsJestTransformer } from './ts-jest-transformer'
1612

1713
const SOURCE_MAPPING_PREFIX = 'sourceMappingURL='
1814

@@ -50,7 +46,6 @@ describe('TsJestTransformer', () => {
5046
"transformerCfgStr",
5147
"compiler",
5248
"depGraphs",
53-
"tsResolvedModulesCachePath",
5449
"watchMode",
5550
]
5651
`)
@@ -79,72 +74,6 @@ describe('TsJestTransformer', () => {
7974

8075
expect(cs2).toBe(cs1)
8176
})
82-
83-
test(`should not read disk cache with isolatedModules true`, () => {
84-
const tr = new TsJestTransformer()
85-
const cs = createConfigSet({
86-
jestConfig: {
87-
globals: { 'ts-jest': { isolatedModules: true } },
88-
},
89-
})
90-
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync')
91-
92-
// @ts-expect-error testing purpose
93-
tr._getFsCachedResolvedModules(cs)
94-
95-
expect(readFileSyncSpy).not.toHaveBeenCalled()
96-
97-
readFileSyncSpy.mockRestore()
98-
})
99-
100-
test(`should not read disk cache with isolatedModules false and no jest cache`, () => {
101-
const tr = new TsJestTransformer()
102-
const cs = createConfigSet({
103-
jestConfig: {
104-
globals: { 'ts-jest': { isolatedModules: false } },
105-
},
106-
})
107-
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync')
108-
109-
// @ts-expect-error testing purpose
110-
tr._getFsCachedResolvedModules(cs)
111-
112-
expect(readFileSyncSpy).not.toHaveBeenCalled()
113-
114-
readFileSyncSpy.mockRestore()
115-
})
116-
117-
test(`should read disk cache with isolatedModules false and use jest cache`, () => {
118-
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync')
119-
const fileName = 'foo.ts'
120-
const tr = new TsJestTransformer()
121-
const cs = createConfigSet({
122-
jestConfig: {
123-
cache: true,
124-
cacheDirectory: cacheDir,
125-
globals: { 'ts-jest': { isolatedModules: false } },
126-
},
127-
})
128-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
129-
const tsCacheDir = cs.tsCacheDir!
130-
const depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
131-
depGraphs.set(fileName, {
132-
fileContent: 'const foo = 1',
133-
resolvedModuleNames: [],
134-
})
135-
const resolvedModulesCacheDir = path.join(tsCacheDir, sha1('ts-jest-resolved-modules', CACHE_KEY_EL_SEPARATOR))
136-
fs.mkdirSync(tsCacheDir, { recursive: true })
137-
writeFileSync(resolvedModulesCacheDir, stringify([...depGraphs]))
138-
139-
// @ts-expect-error testing purpose
140-
tr._getFsCachedResolvedModules(cs)
141-
142-
// @ts-expect-error testing purpose
143-
expect(tr._depGraphs.has(fileName)).toBe(true)
144-
expect(readFileSyncSpy.mock.calls).toEqual(expect.arrayContaining([[resolvedModulesCacheDir, 'utf-8']]))
145-
146-
removeSync(cacheDir)
147-
})
14877
})
14978

15079
describe('getCacheKey', () => {

src/legacy/ts-jest-transformer.ts

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, readFileSync, statSync, writeFileSync, mkdirSync } from 'fs'
1+
import { existsSync, statSync } from 'fs'
22
import path from 'path'
33

44
import type { SyncTransformer, TransformedSource } from '@jest/transform'
@@ -12,7 +12,7 @@ import type {
1212
TsJestTransformerOptions,
1313
TsJestTransformOptions,
1414
} from '../types'
15-
import { parse, stringify, JsonableValue, rootLogger } from '../utils'
15+
import { stringify, JsonableValue, rootLogger } from '../utils'
1616
import { importer } from '../utils/importer'
1717
import { Deprecations, Errors, interpolate } from '../utils/messages'
1818
import { sha1 } from '../utils/sha1'
@@ -27,7 +27,6 @@ interface CachedConfigSet {
2727
transformerCfgStr: string
2828
compiler: CompilerInstance
2929
depGraphs: Map<string, DepGraphInfo>
30-
tsResolvedModulesCachePath: string | undefined
3130
watchMode: boolean
3231
}
3332

@@ -50,7 +49,6 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
5049
private static readonly _cachedConfigSets: CachedConfigSet[] = []
5150
private readonly _logger: Logger
5251
protected _compiler!: CompilerInstance
53-
private _tsResolvedModulesCachePath: string | undefined
5452
private _transformCfgStr!: string
5553
private _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
5654
private _watchMode = false
@@ -81,7 +79,6 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
8179
this._transformCfgStr = ccs.transformerCfgStr
8280
this._compiler = ccs.compiler
8381
this._depGraphs = ccs.depGraphs
84-
this._tsResolvedModulesCachePath = ccs.tsResolvedModulesCachePath
8582
this._watchMode = ccs.watchMode
8683
configSet = ccs.configSet
8784
} else {
@@ -98,7 +95,6 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
9895
this._transformCfgStr = serializedCcs.transformerCfgStr
9996
this._compiler = serializedCcs.compiler
10097
this._depGraphs = serializedCcs.depGraphs
101-
this._tsResolvedModulesCachePath = serializedCcs.tsResolvedModulesCachePath
10298
this._watchMode = serializedCcs.watchMode
10399
configSet = serializedCcs.configSet
104100
} else {
@@ -129,15 +125,13 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
129125
jest.cacheDirectory = undefined as any // eslint-disable-line @typescript-eslint/no-explicit-any
130126
this._transformCfgStr = `${new JsonableValue(jest).serialized}${configSet.cacheSuffix}`
131127
this._createCompiler(configSet, cacheFS)
132-
this._getFsCachedResolvedModules(configSet)
133128
this._watchMode = process.argv.includes('--watch')
134129
TsJestTransformer._cachedConfigSets.push({
135130
jestConfig: new JsonableValue(config),
136131
configSet,
137132
transformerCfgStr: this._transformCfgStr,
138133
compiler: this._compiler,
139134
depGraphs: this._depGraphs,
140-
tsResolvedModulesCachePath: this._tsResolvedModulesCachePath,
141135
watchMode: this._watchMode,
142136
})
143137
}
@@ -321,7 +315,7 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
321315
CACHE_KEY_EL_SEPARATOR,
322316
filePath,
323317
]
324-
if (!configs.isolatedModules && this._tsResolvedModulesCachePath) {
318+
if (!configs.isolatedModules && configs.tsCacheDir) {
325319
let resolvedModuleNames: string[]
326320
if (this._depGraphs.get(filePath)?.fileContent === fileContent) {
327321
this._logger.debug(
@@ -346,7 +340,6 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
346340
fileContent,
347341
resolvedModuleNames,
348342
})
349-
writeFileSync(this._tsResolvedModulesCachePath, stringify([...this._depGraphs]))
350343
}
351344
resolvedModuleNames.forEach((moduleName) => {
352345
constructingCacheKeyElements.push(
@@ -368,20 +361,4 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
368361
): Promise<string> {
369362
return Promise.resolve(this.getCacheKey(sourceText, sourcePath, transformOptions))
370363
}
371-
372-
/**
373-
* Subclasses extends `TsJestTransformer` can call this method to get resolved module disk cache
374-
*/
375-
private _getFsCachedResolvedModules(configSet: ConfigSet): void {
376-
const cacheDir = configSet.tsCacheDir
377-
if (!configSet.isolatedModules && cacheDir) {
378-
// Make sure the cache directory exists before continuing.
379-
mkdirSync(cacheDir, { recursive: true })
380-
this._tsResolvedModulesCachePath = path.join(cacheDir, sha1('ts-jest-resolved-modules', CACHE_KEY_EL_SEPARATOR))
381-
try {
382-
const cachedTSResolvedModules = readFileSync(this._tsResolvedModulesCachePath, 'utf-8')
383-
this._depGraphs = new Map(parse(cachedTSResolvedModules))
384-
} catch (e) {}
385-
}
386-
}
387364
}

0 commit comments

Comments
 (0)