/
compile-typescript-files.ts
69 lines (64 loc) · 2.1 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
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';
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 === 6194) {
await postCompilationCallback();
next(getResult(true));
}
});
} else {
const { success } = compileTypeScript(tscOptions);
await postCompilationCallback();
next(getResult(success));
done();
}
}
);
}