-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
compile-typescript-files.ts
80 lines (73 loc) · 2.68 KB
/
compile-typescript-files.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 { ExecutorContext } from '@nrwl/devkit';
import {
compileTypeScript,
compileTypeScriptWatcher,
} from '@nrwl/workspace/src/utilities/typescript/compilation';
import type {
CustomTransformers,
Diagnostic,
Program,
SourceFile,
TransformerFactory,
} from 'typescript';
import { createAsyncIterable } from '../create-async-iterable/create-async-iteratable';
import { NormalizedExecutorOptions } from '../schema';
import { loadTsTransformers } from './load-ts-transformers';
const TYPESCRIPT_FOUND_N_ERRORS_WATCHING_FOR_FILE_CHANGES = 6194;
// Typescript diagnostic message for 6194: Found {0} errors. Watching for file changes.
// https://github.com/microsoft/TypeScript/blob/d45012c5e2ab122919ee4777a7887307c5f4a1e0/src/compiler/diagnosticMessages.json#L4763-L4766
const ERROR_COUNT_REGEX = /Found (\d+) errors/;
function getErrorCountFromMessage(messageText: string) {
return Number.parseInt(ERROR_COUNT_REGEX.exec(messageText)[1]);
}
export async function* compileTypeScriptFiles(
normalizedOptions: NormalizedExecutorOptions,
context: ExecutorContext,
postCompilationCallback: () => void | Promise<void>
) {
const getResult = (success: boolean) => ({
success,
outfile: normalizedOptions.mainOutputPath,
});
const { compilerPluginHooks } = loadTsTransformers(
normalizedOptions.transformers
);
const getCustomTransformers = (program: Program): CustomTransformers => ({
before: compilerPluginHooks.beforeHooks.map(
(hook) => hook(program) as TransformerFactory<SourceFile>
),
after: compilerPluginHooks.afterHooks.map(
(hook) => hook(program) as TransformerFactory<SourceFile>
),
afterDeclarations: compilerPluginHooks.afterDeclarationsHooks.map(
(hook) => hook(program) as TransformerFactory<SourceFile>
),
});
const tscOptions = {
outputPath: normalizedOptions.outputPath,
projectName: context.projectName,
projectRoot: normalizedOptions.projectRoot,
tsConfig: normalizedOptions.tsConfig,
watch: normalizedOptions.watch,
getCustomTransformers,
};
return yield* createAsyncIterable<{ success: boolean; outfile: string }>(
async ({ next, done }) => {
if (normalizedOptions.watch) {
compileTypeScriptWatcher(tscOptions, async (d: Diagnostic) => {
if (d.code === TYPESCRIPT_FOUND_N_ERRORS_WATCHING_FOR_FILE_CHANGES) {
await postCompilationCallback();
next(
getResult(getErrorCountFromMessage(d.messageText as string) === 0)
);
}
});
} else {
const { success } = compileTypeScript(tscOptions);
await postCompilationCallback();
next(getResult(success));
done();
}
}
);
}