From 8334c9b50fec01d9094b90b71a0913d4b87f94e1 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Tue, 5 Jul 2022 01:04:13 -0400 Subject: [PATCH] fix(cache): invalidate `codeCache` in most cases when imports change (#369) * fix(cache): invalidate declarations when imports change - previously, the `checkImports` flag was set to `true` for type-checking, but `false` for compilation - I _believe_ it is `false` because the compiled JS shouldn't change if an import changes - though I'm not sure if that was the original intent behind the code - problematically though, compilation results can include declarations, and those _can_ change if imports change - for instance, the types of an import can change the declaration that is output - so now, only set it to `false` for compilation if declarations are _not_ needed * add `!isolatedModules` check as well - ezolenko gave a good example of enums, which can indeed cause the compiled JS code to change based on imports - so also check `!isolatedModules` as well - I thought it might be the case that the code wouldn't handle other edge cases, but couldn't think of one off the top of my head - ironically, I compared it to Babel, which transpiles per file, and Babel _requires_ `isolatedModules` to work --- src/tscache.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tscache.ts b/src/tscache.ts index 14756af..617f9ad 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -213,7 +213,8 @@ export class TsCache public getCompiled(id: string, snapshot: tsTypes.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined { this.context.info(`${blue("transpiling")} '${id}'`); - return this.getCached(this.codeCache, id, snapshot, false, transform); + // if !isolatedModules, compiled JS code can change if its imports do (e.g. enums). also, declarations can change based on imports as well + return this.getCached(this.codeCache, id, snapshot, Boolean(!this.options.isolatedModules || this.options.declaration), transform); } public getSyntacticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]