Skip to content

Commit 211b29d

Browse files
committed
Fix host.ts problems to speed up compilation
Originally, the TypeScript cache gets constantly invalidated, causing build time to increase non-linearly, bloating into minutes for fairly complicated projects. This commit identified and fixed these problems that caused this problem: - File version is increased unnecessarily when source hasn't changed. - Files in node_modules are added incorrectly into LanguageServiceHost.fileNames, since `setSnapshot` is called indirectly for all dependency files discovered when type checking. `fileNames` represents the entry files for this build, so it shouldn't be updating during the process. - Also creating snapshot when file read in `getScriptSnapshot` is empty (which is still a valid file whatsoever), otherwise it would be marked as 'missing' and invalidate the cache.
1 parent 8743691 commit 211b29d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/host.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
3131
{
3232
fileName = normalize(fileName);
3333

34+
const originalSnapshot = this.snapshots[fileName];
35+
36+
if (originalSnapshot && originalSnapshot.getText(0, originalSnapshot.getLength()) === source) {
37+
return originalSnapshot;
38+
}
39+
3440
const snapshot = tsModule.ScriptSnapshot.fromString(source);
3541
this.snapshots[fileName] = snapshot;
3642
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
37-
this.fileNames.add(fileName);
43+
3844
return snapshot;
3945
}
4046

@@ -46,7 +52,7 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
4652
return this.snapshots[fileName];
4753

4854
const source = tsModule.sys.readFile(fileName);
49-
if (source)
55+
if (source !== undefined)
5056
return this.setSnapshot(fileName, source);
5157

5258
return undefined;

0 commit comments

Comments
 (0)