-
Notifications
You must be signed in to change notification settings - Fork 378
/
watch-compiler.ts
146 lines (136 loc) · 4.63 KB
/
watch-compiler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as ts from 'typescript';
import { Configuration } from '../configuration';
import { CLI_ERRORS } from '../ui/errors';
import { getValueOrDefault } from './helpers/get-value-or-default';
import { TsConfigProvider } from './helpers/tsconfig-provider';
import { tsconfigPathsBeforeHookFactory } from './hooks/tsconfig-paths.hook';
import { PluginsLoader } from './plugins-loader';
import { TypeScriptBinaryLoader } from './typescript-loader';
export class WatchCompiler {
constructor(
private readonly pluginsLoader: PluginsLoader,
private readonly tsConfigProvider: TsConfigProvider,
private readonly typescriptLoader: TypeScriptBinaryLoader,
) {}
public run(
configuration: Required<Configuration>,
configFilename: string,
appName: string,
tsCompilerOptions: ts.CompilerOptions,
onSuccess?: () => void,
) {
const tsBin = this.typescriptLoader.load();
const configPath = tsBin.findConfigFile(
process.cwd(),
tsBin.sys.fileExists,
configFilename,
);
if (!configPath) {
throw new Error(CLI_ERRORS.MISSING_TYPESCRIPT(configFilename));
}
const { projectReferences } = this.tsConfigProvider.getByConfigFilename(
configFilename,
);
const createProgram = tsBin.createEmitAndSemanticDiagnosticsBuilderProgram;
const origDiagnosticReporter = (tsBin as any).createDiagnosticReporter(
tsBin.sys,
true,
);
const origWatchStatusReporter = (tsBin as any).createWatchStatusReporter(
tsBin.sys,
true,
);
const host = tsBin.createWatchCompilerHost(
configPath,
tsCompilerOptions,
tsBin.sys,
createProgram,
this.createDiagnosticReporter(origDiagnosticReporter),
this.createWatchStatusChanged(origWatchStatusReporter, onSuccess),
);
const pluginsConfig = getValueOrDefault(
configuration,
'compilerOptions.plugins',
appName,
);
const plugins = this.pluginsLoader.load(pluginsConfig);
const origCreateProgram = host.createProgram;
(host as any).createProgram = (
rootNames: ReadonlyArray<string>,
options: ts.CompilerOptions,
// tslint:disable-next-line:no-shadowed-variable
host: ts.CompilerHost,
oldProgram: ts.EmitAndSemanticDiagnosticsBuilderProgram,
) => {
const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(options);
const program = origCreateProgram(
rootNames,
options,
host,
oldProgram,
undefined,
projectReferences,
);
const origProgramEmit = program.emit;
program.emit = (
targetSourceFile?: ts.SourceFile,
writeFile?: ts.WriteFileCallback,
cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers?: ts.CustomTransformers,
) => {
let transforms = customTransformers;
transforms = typeof transforms !== 'object' ? {} : transforms;
const before = plugins.beforeHooks.map((hook) =>
hook(program.getProgram()),
);
const after = plugins.afterHooks.map((hook) =>
hook(program.getProgram()),
);
const afterDeclarations = plugins.afterDeclarationsHooks.map((hook) =>
hook(program.getProgram()),
);
before.unshift(tsconfigPathsPlugin);
transforms.before = before.concat(transforms.before || []);
transforms.after = after.concat(transforms.after || []);
transforms.afterDeclarations = afterDeclarations.concat(
transforms.afterDeclarations || [],
);
return origProgramEmit(
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
transforms,
);
};
return program as any;
};
tsBin.createWatchProgram(host);
}
private createDiagnosticReporter(
diagnosticReporter: (diagnostic: ts.Diagnostic, ...args: any[]) => any,
) {
return function (this: any, diagnostic: ts.Diagnostic, ...args: any[]) {
return diagnosticReporter.call(this, diagnostic, ...args);
};
}
private createWatchStatusChanged(
watchStatusReporter: (diagnostic: ts.Diagnostic, ...args: any[]) => any,
onSuccess?: () => void,
) {
return function (this: any, diagnostic: ts.Diagnostic, ...args: any[]) {
const messageText = diagnostic && diagnostic.messageText;
const noErrorsMessage = '0 errors';
if (
messageText &&
(messageText as string).includes &&
(messageText as string).includes(noErrorsMessage) &&
onSuccess
) {
onSuccess();
}
return watchStatusReporter.call(this, diagnostic, ...args);
};
}
}