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
This commit is contained in:
Anton Gilgur 2022-07-05 01:04:13 -04:00 committed by GitHub
parent 63a644a762
commit 8334c9b50f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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[]