From db628fa94ddf6fc9ee04e5566dc9df2d936ea3dd Mon Sep 17 00:00:00 2001 From: Ahn Date: Fri, 24 Apr 2020 08:03:12 +0200 Subject: [PATCH] =?UTF-8?q?perf(compiler):=20don=E2=80=99t=20write=20compi?= =?UTF-8?q?le=20output=20to=20file=20system=20but=20rely=20on=20jest=20cac?= =?UTF-8?q?he=20(#1561)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__snapshots__/logger.test.ts.snap | 30 ++--- .../language-service.spec.ts.snap | 16 --- .../__snapshots__/transpiler.spec.ts.snap | 18 --- src/compiler/compiler-utils.ts | 12 +- src/compiler/instance.ts | 111 +++--------------- src/compiler/language-service.spec.ts | 48 -------- src/compiler/language-service.ts | 45 +++---- src/compiler/transpiler.spec.ts | 31 ----- src/compiler/transpiler.ts | 2 +- src/types.ts | 4 +- 10 files changed, 68 insertions(+), 249 deletions(-) diff --git a/e2e/__tests__/__snapshots__/logger.test.ts.snap b/e2e/__tests__/__snapshots__/logger.test.ts.snap index e388cdb108..85fd10dfcd 100644 --- a/e2e/__tests__/__snapshots__/logger.test.ts.snap +++ b/e2e/__tests__/__snapshots__/logger.test.ts.snap @@ -21,19 +21,19 @@ Array [ "[level:20] processing /Hello.spec.ts", "[level:20] file caching disabled", "[level:20] initializeLanguageServiceInstance(): create typescript compiler", - "[level:20] compileUsingLanguageService(): creating language service", - "[level:20] compileAndCacheResult(): no cache", + "[level:20] initializeLanguageServiceInstance(): creating language service", + "[level:20] compileAndCacheResult(): get compile output", "[level:20] compileFn(): compiling using language service", "[level:20] updateMemoryCache(): update memory cache for language service", "[level:20] visitSourceFileNode(): hoisting", - "[level:20] compileFn(): computing diagnostics for /Hello.spec.ts using language service", + "[level:20] compileFn(): computing diagnostics using language service", "[level:20] computing cache key for /Hello.ts", "[level:20] processing /Hello.ts", - "[level:20] compileAndCacheResult(): no cache", + "[level:20] compileAndCacheResult(): get compile output", "[level:20] compileFn(): compiling using language service", "[level:20] updateMemoryCache(): update memory cache for language service", "[level:20] visitSourceFileNode(): hoisting", - "[level:20] compileFn(): computing diagnostics for /Hello.ts using language service", + "[level:20] compileFn(): computing diagnostics using language service", ] `; @@ -62,20 +62,20 @@ Array [ "[level:20] checking version of babel-jest: OK", "[level:20] file caching disabled", "[level:20] initializeLanguageServiceInstance(): create typescript compiler", - "[level:20] compileUsingLanguageService(): creating language service", - "[level:20] compileAndCacheResult(): no cache", + "[level:20] initializeLanguageServiceInstance(): creating language service", + "[level:20] compileAndCacheResult(): get compile output", "[level:20] compileFn(): compiling using language service", "[level:20] updateMemoryCache(): update memory cache for language service", "[level:20] visitSourceFileNode(): hoisting", - "[level:20] compileFn(): computing diagnostics for /Hello.spec.ts using language service", + "[level:20] compileFn(): computing diagnostics using language service", "[level:20] calling babel-jest processor", "[level:20] computing cache key for /Hello.ts", "[level:20] processing /Hello.ts", - "[level:20] compileAndCacheResult(): no cache", + "[level:20] compileAndCacheResult(): get compile output", "[level:20] compileFn(): compiling using language service", "[level:20] updateMemoryCache(): update memory cache for language service", "[level:20] visitSourceFileNode(): hoisting", - "[level:20] compileFn(): computing diagnostics for /Hello.ts using language service", + "[level:20] compileFn(): computing diagnostics using language service", "[level:20] calling babel-jest processor", ] `; @@ -106,20 +106,20 @@ Array [ "[level:20] checking version of babel-jest: OK", "[level:20] file caching disabled", "[level:20] initializeLanguageServiceInstance(): create typescript compiler", - "[level:20] compileUsingLanguageService(): creating language service", - "[level:20] compileAndCacheResult(): no cache", + "[level:20] initializeLanguageServiceInstance(): creating language service", + "[level:20] compileAndCacheResult(): get compile output", "[level:20] compileFn(): compiling using language service", "[level:20] updateMemoryCache(): update memory cache for language service", "[level:20] visitSourceFileNode(): hoisting", - "[level:20] compileFn(): computing diagnostics for /Hello.spec.ts using language service", + "[level:20] compileFn(): computing diagnostics using language service", "[level:20] calling babel-jest processor", "[level:20] computing cache key for /Hello.ts", "[level:20] processing /Hello.ts", - "[level:20] compileAndCacheResult(): no cache", + "[level:20] compileAndCacheResult(): get compile output", "[level:20] compileFn(): compiling using language service", "[level:20] updateMemoryCache(): update memory cache for language service", "[level:20] visitSourceFileNode(): hoisting", - "[level:20] compileFn(): computing diagnostics for /Hello.ts using language service", + "[level:20] compileFn(): computing diagnostics using language service", "[level:20] calling babel-jest processor", ] `; diff --git a/src/compiler/__snapshots__/language-service.spec.ts.snap b/src/compiler/__snapshots__/language-service.spec.ts.snap index c6383bdc3c..4c8fb3d4f2 100644 --- a/src/compiler/__snapshots__/language-service.spec.ts.snap +++ b/src/compiler/__snapshots__/language-service.spec.ts.snap @@ -90,19 +90,3 @@ exports[`Language service should throw error when cannot compile 1`] = ` "Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts. This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`." `; - -exports[`Language service should use the cache 3`] = ` - ===[ FILE: test-cache.ts ]====================================================== - console.log("hello"); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1jYWNoZS50cyIsIm1hcHBpbmdzIjoiQUFBQSxPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtY2FjaGUudHMiXSwic291cmNlc0NvbnRlbnQiOlsiY29uc29sZS5sb2coXCJoZWxsb1wiKSJdLCJ2ZXJzaW9uIjozfQ== - ===[ INLINE SOURCE MAPS ]======================================================= - file: test-cache.ts - mappings: 'AAAA,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA' - names: [] - sources: - - test-cache.ts - sourcesContent: - - console.log("hello") - version: 3 - ================================================================================ -`; diff --git a/src/compiler/__snapshots__/transpiler.spec.ts.snap b/src/compiler/__snapshots__/transpiler.spec.ts.snap index bf6af90209..cb2aba9d45 100644 --- a/src/compiler/__snapshots__/transpiler.spec.ts.snap +++ b/src/compiler/__snapshots__/transpiler.spec.ts.snap @@ -66,24 +66,6 @@ exports[`Transpiler should compile tsx file for other jsx options 1`] = ` ================================================================================ `; -exports[`Transpiler should compile using transpileModule and not use cache 1`] = ` - ===[ FILE: src/compiler/transpiler.spec.ts ]==================================== - "use strict"; - Object.defineProperty(exports, "__esModule", { value: true }); - exports.default = 42; - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vc3JjL2NvbXBpbGVyL3RyYW5zcGlsZXIuc3BlYy50cyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9zcmMvY29tcGlsZXIvdHJhbnNwaWxlci5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9 - ===[ INLINE SOURCE MAPS ]======================================================= - file: /src/compiler/transpiler.spec.ts - mappings: ';;AAAA,kBAAe,EAAE,CAAA' - names: [] - sources: - - /src/compiler/transpiler.spec.ts - sourcesContent: - - export default 42 - version: 3 - ================================================================================ -`; - exports[`Transpiler should report diagnostics related to codes with pathRegex config is undefined 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`; exports[`Transpiler should report diagnostics related to codes with pathRegex config matches file name 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`; diff --git a/src/compiler/compiler-utils.ts b/src/compiler/compiler-utils.ts index d459f7dfba..3d2ae0f309 100644 --- a/src/compiler/compiler-utils.ts +++ b/src/compiler/compiler-utils.ts @@ -9,6 +9,10 @@ import { EXTENSION_REGEX, JSON_REGEX, TS_TSX_REGEX } from '../constants' import { MemoryCache, SourceOutput, TSFiles } from '../types' import { sha1 } from '../util/sha1' +/** + * @internal + */ +export const hasOwn = Object.prototype.hasOwnProperty /** * @internal */ @@ -35,7 +39,7 @@ export function cacheResolvedModules( * Ugly trick while waiting for https://github.com/microsoft/TypeScript/issues/33994 */ if (importReferences.length) { - logger.debug({ fileName }, `cacheResolvedModules(): get resolved modules of test file ${fileName}`) + logger.debug({ fileName }, `cacheResolvedModules(): get resolved modules`) memoryCache.resolvedModules[fileName] = Object.create(null) memoryCache.resolvedModules[fileName].modulePaths = importReferences @@ -113,7 +117,7 @@ export function getAndCacheProjectReference( files: TSFiles, projectReferences: ReadonlyArray<_ts.ProjectReference> | undefined, ) { - const file = files.get(filePath) + const file = files[filePath] if (file !== undefined && file.projectReference) { return file.projectReference.project } @@ -152,8 +156,8 @@ function getAndCacheOutputJSFileName( projectReference: _ts.ResolvedProjectReference, files: TSFiles, ) { - const file = files.get(inputFileName) - if (file && file.projectReference && file.projectReference.outputFileName) { + const file = files[inputFileName] + if (file?.projectReference && file.projectReference.outputFileName) { return file.projectReference.outputFileName } diff --git a/src/compiler/instance.ts b/src/compiler/instance.ts index 1ea45a1b5c..8ef63a39d8 100644 --- a/src/compiler/instance.ts +++ b/src/compiler/instance.ts @@ -1,42 +1,10 @@ -/** - * This code is heavily inspired from - * https://github.com/JsCommunity/make-error/blob/v1.3.4/index.js - * ...but more modified than expected :-D - * Below is the original license anyway: - * - * --- - * - * The MIT License (MIT) - * - * Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - import { Logger } from 'bs-logger' -import { readFileSync, writeFileSync } from 'fs' +import { readFileSync } from 'fs' import mkdirp = require('mkdirp') -import { basename, extname, join } from 'path' +import { basename, extname } from 'path' import { ConfigSet } from '../config/config-set' -import { CompileFn, CompilerInstance, MemoryCache, TSFile, TsCompiler } from '../types' -import { sha1 } from '../util/sha1' +import { CompileFn, CompilerInstance, MemoryCache, TsCompiler } from '../types' import { getResolvedModulesCache } from './compiler-utils' import { initializeLanguageServiceInstance } from './language-service' @@ -72,74 +40,27 @@ const updateSourceMap = (sourceMapText: string, normalizedFileName: string): str return JSON.stringify(sourceMap) } -/** - * Get the file name for the cache entry. - */ -const getCacheName = (sourceCode: string, normalizedFileName: string): string => { - return sha1(normalizedFileName, '\x00', sourceCode) -} - -/** - * Ensure the given cached content is valid by sniffing for a base64 encoded '}' - * at the end of the content, which should exist if there is a valid sourceMap present. - */ -const isValidCacheContent = (contents: string): boolean => { - return /(?:9|0=|Q==)$/.test(contents.slice(-3)) -} - /** * Compile files which are provided by jest via transform config and cache the result in file system if users run with * cache mode */ const compileAndCacheResult = ( - cacheDir: string | undefined, memoryCache: MemoryCache, compileFn: CompileFn, getExtension: (fileName: string) => string, logger: Logger, ) => { return (code: string, fileName: string, lineOffset?: number) => { - function getCompileOutput(): string { - const [value, sourceMap] = compileFn(code, fileName, lineOffset) - const output = updateOutput(value, fileName, sourceMap, getExtension) - memoryCache.files.set(fileName, { - ...memoryCache.files.get(fileName)!, - output, - }) + logger.debug({ fileName }, 'compileAndCacheResult(): get compile output') - return output + const [value, sourceMap] = compileFn(code, fileName, lineOffset) + const output = updateOutput(value, fileName, sourceMap, getExtension) + memoryCache.files[fileName] = { + ...memoryCache.files[fileName], + output, } - if (!cacheDir) { - logger.debug({ fileName }, 'compileAndCacheResult(): no cache') - - return getCompileOutput() - } else { - const cachePath = join(cacheDir, getCacheName(code, fileName)) - const extension = getExtension(fileName) - const outputPath = `${cachePath}${extension}` - try { - const output = readFileSync(outputPath, 'utf8') - if (isValidCacheContent(output)) { - logger.debug({ fileName }, 'compileAndCacheResult(): cache hit') - memoryCache.files.set(fileName, { - ...memoryCache.files.get(fileName)!, - output, - }) - - return output - } - } catch (err) {} - - logger.debug({ fileName }, 'compileAndCacheResult(): cache miss') - - const output = getCompileOutput() - logger.debug({ fileName, outputPath }, 'compileAndCacheResult(): writing caches') - - writeFileSync(outputPath, output) - - return output - } + return output } } @@ -157,8 +78,8 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => { const ts = configs.compilerModule // Require the TypeScript compiler and configuration. const extensions = ['.ts', '.tsx'] const memoryCache: MemoryCache = { + files: Object.create(null), resolvedModules: Object.create(null), - files: new Map(), } // Enable `allowJs` when flag is set. if (compilerOptions.allowJs) { @@ -169,16 +90,16 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => { // Make sure the cache directory exists before continuing. mkdirp.sync(cacheDir) try { - const resolvedModulesCache = readFileSync(getResolvedModulesCache(cacheDir), 'utf-8') + const fsMemoryCache = readFileSync(getResolvedModulesCache(cacheDir), 'utf-8') /* istanbul ignore next (covered by e2e) */ - memoryCache.resolvedModules = JSON.parse(resolvedModulesCache) + memoryCache.resolvedModules = JSON.parse(fsMemoryCache) } catch (e) {} } /* istanbul ignore next (we leave this for e2e) */ configs.jest.setupFiles.concat(configs.jest.setupFilesAfterEnv).forEach(setupFile => { - memoryCache.files.set(setupFile, { + memoryCache.files[setupFile] = { version: 0, - }) + } }) /** * Get the extension for a transpiled file. @@ -194,7 +115,7 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => { } else { compilerInstance = initializeTranspilerInstance(configs, memoryCache, logger) } - const compile = compileAndCacheResult(cacheDir, memoryCache, compilerInstance.compileFn, getExtension, logger) + const compile = compileAndCacheResult(memoryCache, compilerInstance.compileFn, getExtension, logger) return { cwd: configs.cwd, compile, program: compilerInstance.program } } diff --git a/src/compiler/language-service.spec.ts b/src/compiler/language-service.spec.ts index 90b0efa08a..5bd671d8d9 100644 --- a/src/compiler/language-service.spec.ts +++ b/src/compiler/language-service.spec.ts @@ -1,4 +1,3 @@ -import { LogLevels } from 'bs-logger' import { removeSync, writeFileSync } from 'fs-extra' import { makeCompiler } from '../__helpers__/fakers' @@ -16,53 +15,6 @@ describe('Language service', () => { logTarget.clear() }) - it('should use the cache', () => { - const tmp = tempDir('compiler') - const compiler = makeCompiler({ - jestConfig: { cache: true, cacheDirectory: tmp }, - tsJestConfig: { tsConfig: false }, - }) - const source = 'console.log("hello")' - const fileName = 'test-cache.ts' - - writeFileSync(fileName, source, 'utf8') - - logTarget.clear() - const compiled1 = compiler.compile(source, fileName) - - expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchInlineSnapshot(` - Array [ - "[level:20] compileAndCacheResult(): cache miss - ", - "[level:20] compileFn(): compiling using language service - ", - "[level:20] updateMemoryCache(): update memory cache for language service - ", - "[level:20] visitSourceFileNode(): hoisting - ", - "[level:20] compileFn(): computing diagnostics for test-cache.ts using language service - ", - "[level:20] compileAndCacheResult(): writing caches - ", - ] - `) - - logTarget.clear() - const compiled2 = compiler.compile(source, fileName) - - expect(logTarget.lines).toMatchInlineSnapshot(` - Array [ - "[level:20] compileAndCacheResult(): cache hit - ", - ] - `) - - expect(new ProcessedSource(compiled1, fileName)).toMatchSnapshot() - expect(compiled2).toBe(compiled1) - - removeSync(fileName) - }) - it('should get compile result from referenced project when there is a built reference project', () => { const tmp = tempDir('compiler') const compiler = makeCompiler({ diff --git a/src/compiler/language-service.ts b/src/compiler/language-service.ts index b0483e518f..5fa511e6f2 100644 --- a/src/compiler/language-service.ts +++ b/src/compiler/language-service.ts @@ -12,6 +12,7 @@ import { cacheResolvedModules, getAndCacheProjectReference, getCompileResultFromReferencedProject, + hasOwn, isTestFile, } from './compiler-utils' @@ -50,21 +51,21 @@ export const initializeLanguageServiceInstance = ( logger.debug({ fileName }, `updateMemoryCache(): update memory cache for language service`) let shouldIncrementProjectVersion = false - const isFileInCache = memoryCache.files.has(fileName) && memoryCache.files.get(fileName)!.version !== 0 + const isFileInCache = hasOwn.call(memoryCache.files, fileName) && memoryCache.files[fileName].version !== 0 if (!isFileInCache) { - memoryCache.files.set(fileName, { + memoryCache.files[fileName] = { text: contents, version: 1, - }) + } shouldIncrementProjectVersion = true } else { - const previousContents = memoryCache.files.get(fileName)!.text + const previousContents = memoryCache.files[fileName].text // Avoid incrementing cache when nothing has changed. if (previousContents !== contents) { - memoryCache.files.set(fileName, { - version: memoryCache.files.get(fileName)!.version + 1, + memoryCache.files[fileName] = { text: contents, - }) + version: memoryCache.files[fileName].version + 1, + } // Only bump project version when file is modified in cache, not when discovered for the first time if (isFileInCache) shouldIncrementProjectVersion = true } @@ -82,10 +83,10 @@ export const initializeLanguageServiceInstance = ( const serviceHost: _ts.LanguageServiceHost = { getProjectVersion: () => String(projectVersion), getProjectReferences: () => projectReferences, - getScriptFileNames: () => [...memoryCache.files.keys()], + getScriptFileNames: () => Object.keys(memoryCache.files), getScriptVersion: (fileName: string) => { const normalizedFileName = normalize(fileName) - const version = memoryCache.files.get(normalizedFileName)!.version + const version = memoryCache.files[normalizedFileName].version // We need to return `undefined` and not a string here because TypeScript will use // `getScriptVersion` and compare against their own version - which can be `undefined`. @@ -96,18 +97,19 @@ export const initializeLanguageServiceInstance = ( }, getScriptSnapshot(fileName: string) { const normalizedFileName = normalize(fileName) - const hit = memoryCache.files.has(normalizedFileName) && memoryCache.files.get(normalizedFileName)!.version !== 0 + const hit = + hasOwn.call(memoryCache.files, normalizedFileName) && memoryCache.files[normalizedFileName].version !== 0 logger.trace({ normalizedFileName, cacheHit: hit }, `getScriptSnapshot():`, 'cache', hit ? 'hit' : 'miss') // Read contents from TypeScript memory cache. if (!hit) { - memoryCache.files.set(normalizedFileName, { - version: 1, + memoryCache.files[normalizedFileName] = { text: ts.sys.readFile(normalizedFileName), - }) + version: 1, + } } - const contents = memoryCache.files.get(normalizedFileName)!.text + const contents = memoryCache.files[normalizedFileName]?.text if (contents === undefined) return @@ -126,7 +128,7 @@ export const initializeLanguageServiceInstance = ( getCustomTransformers: () => configs.tsCustomTransformers, } - logger.debug('compileUsingLanguageService(): creating language service') + logger.debug('initializeLanguageServiceInstance(): creating language service') const service: _ts.LanguageService = ts.createLanguageService(serviceHost, ts.createDocumentRegistry()) @@ -149,7 +151,7 @@ export const initializeLanguageServiceInstance = ( } else { const output: _ts.EmitOutput = service.getEmitOutput(fileName) // Do type checking by getting TypeScript diagnostics - logger.debug(`compileFn(): computing diagnostics for ${fileName} using language service`) + logger.debug({ fileName }, `compileFn(): computing diagnostics using language service`) doTypeChecking(configs, fileName, service, logger) /** @@ -168,16 +170,19 @@ export const initializeLanguageServiceInstance = ( * test file for 1st time run after clearing cache because */ return ( - entry[1].modulePaths.find(modulePath => modulePath === fileName) && !memoryCache.files.has(entry[0]) + entry[1].modulePaths.find(modulePath => modulePath === fileName) && + !hasOwn.call(memoryCache.files, entry[0]) ) }) .forEach(entry => { + const testFileName = entry[0] + const testFileContent = entry[1].testFileContent logger.debug( - `compileFn(): computing diagnostics for test file that imports ${fileName} using language service`, + { fileName }, + `compileFn(): computing diagnostics for test file that imports this module using language service`, ) - const testFileName = entry[0] - updateMemoryCache(entry[1].testFileContent, testFileName) + updateMemoryCache(testFileContent, testFileName) doTypeChecking(configs, testFileName, service, logger) }) } diff --git a/src/compiler/transpiler.spec.ts b/src/compiler/transpiler.spec.ts index 3e6066781a..dc00ed9fcd 100644 --- a/src/compiler/transpiler.spec.ts +++ b/src/compiler/transpiler.spec.ts @@ -1,48 +1,17 @@ -import { LogLevels } from 'bs-logger' import { removeSync, writeFileSync } from 'fs-extra' import * as _ts from 'typescript' import { makeCompiler } from '../__helpers__/fakers' -import { logTargetMock } from '../__helpers__/mocks' import ProcessedSource from '../__helpers__/processed-source' import { TS_JEST_OUT_DIR } from '../config/config-set' import * as compilerUtils from './compiler-utils' -const logTarget = logTargetMock() - describe('Transpiler', () => { const baseTsJestConfig = { isolatedModules: true, } - beforeEach(() => { - logTarget.clear() - }) - - it('should compile using transpileModule and not use cache', () => { - const compiler = makeCompiler({ tsJestConfig: { ...baseTsJestConfig, tsConfig: false } }) - const spy = jest.spyOn(_ts, 'transpileModule') - - logTarget.clear() - const compiled = compiler.compile('export default 42', __filename) - - expect(new ProcessedSource(compiled, __filename)).toMatchSnapshot() - expect(spy).toHaveBeenCalled() - expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchInlineSnapshot(` - Array [ - "[level:20] compileAndCacheResult(): no cache - ", - "[level:20] compileFn(): compiling as isolated module - ", - "[level:20] visitSourceFileNode(): hoisting - ", - ] - `) - - spy.mockRestore() - }) - it( 'should call createProgram() with projectReferences, call getAndCacheProjectReference()' + ' and getCompileResultFromReferenceProject() when there are projectReferences from tsconfig', diff --git a/src/compiler/transpiler.ts b/src/compiler/transpiler.ts index c25b844234..fce084ac87 100644 --- a/src/compiler/transpiler.ts +++ b/src/compiler/transpiler.ts @@ -27,7 +27,7 @@ export const initializeTranspilerInstance = ( : ts.createProgram([], options) /* istanbul ignore next (we leave this for e2e) */ const updateFileInCache = (contents: string, filePath: string) => { - const file = memoryCache.files.get(filePath) + const file = memoryCache.files[filePath] if (file && file.text !== contents) { file.version++ file.text = contents diff --git a/src/types.ts b/src/types.ts index b9966eca2b..3ecd011077 100644 --- a/src/types.ts +++ b/src/types.ts @@ -211,7 +211,9 @@ export interface AstTransformerDesc { factory(cs: ConfigSet): _ts.TransformerFactory<_ts.SourceFile> } /** where key is filepath */ -export type TSFiles = Map +export interface TSFiles { + [filePath: string]: TSFile +} export interface TSFile { text?: string output?: string