-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
watch-program.ts
80 lines (73 loc) · 2.38 KB
/
watch-program.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type * as ts from 'typescript';
import { getConfigFilePathFromBuilderProgram, getParsedConfig } from '../config';
import { getDependencies } from '../dependencies';
import { updateDiagnostics, getDiagnosticsOfProgram } from '../diagnostics';
import { emitDtsIfNeeded } from '../emit';
import { createWatchCompilerHost } from '../host/watch-compiler-host';
import { startTracingIfNeeded, stopTracingIfNeeded } from '../tracing';
import { emitTsBuildInfoIfNeeded } from '../tsbuildinfo';
import { typescript } from '../typescript';
let watchCompilerHost:
| ts.WatchCompilerHostOfFilesAndCompilerOptions<ts.SemanticDiagnosticsBuilderProgram>
| undefined;
let watchProgram:
| ts.WatchOfFilesAndCompilerOptions<ts.SemanticDiagnosticsBuilderProgram>
| undefined;
let shouldUpdateRootFiles = false;
export function useWatchProgram() {
if (!watchCompilerHost) {
const parsedConfig = getParsedConfig();
watchCompilerHost = createWatchCompilerHost(
parsedConfig,
(
rootNames,
compilerOptions,
host,
oldProgram,
configFileParsingDiagnostics,
projectReferences
) => {
if (compilerOptions) {
startTracingIfNeeded(compilerOptions);
}
return typescript.createSemanticDiagnosticsBuilderProgram(
rootNames,
compilerOptions,
host,
oldProgram,
configFileParsingDiagnostics,
projectReferences
);
},
undefined,
undefined,
(builderProgram) => {
updateDiagnostics(
getConfigFilePathFromBuilderProgram(builderProgram),
getDiagnosticsOfProgram(builderProgram)
);
emitDtsIfNeeded(builderProgram);
emitTsBuildInfoIfNeeded(builderProgram);
stopTracingIfNeeded(builderProgram);
}
);
watchProgram = undefined;
}
if (!watchProgram) {
watchProgram = typescript.createWatchProgram(watchCompilerHost);
}
if (shouldUpdateRootFiles) {
// we have to update root files manually as don't use config file as a program input
watchProgram.updateRootFileNames(getDependencies().files);
shouldUpdateRootFiles = false;
}
}
export function invalidateWatchProgram(withHost = false) {
if (withHost) {
watchCompilerHost = undefined;
}
watchProgram = undefined;
}
export function invalidateWatchProgramRootFileNames() {
shouldUpdateRootFiles = true;
}