UNPKG

2.2 kBPlain TextView Raw
1import Compiler from "./compiler";
2import DiagnosticsHandler from "./diagnostics-handler";
3import { toAbsolutePath } from "./fs/path-utils";
4import { BroccoliPlugin, heimdall } from "./helpers";
5import { NormalizedOptions, TypescriptCompilerOptions } from "./interfaces";
6import normalizeOptions from "./normalize-options";
7
8/**
9 * Returns a Broccoli plugin instance that compiles
10 * the files in the tsconfig.
11 *
12 * It is rooted to the inputNode's outputPath, all
13 * files it imports must be resolvable from its input
14 * except for the default library file.
15 *
16 * Errors are logged and it will try to emit whatever
17 * it could successfully compile.
18 *
19 * It will only emit based on the root source files
20 * you give it, by default it will look for all .ts
21 * files, but if you specify a files or filesGlob
22 * it will use these as entry points and only compile
23 * the files and files they reference from the input.
24 */
25export function typescript(
26 inputNode: any,
27 options?: TypescriptCompilerOptions
28) {
29 return new TypescriptCompiler(inputNode, options);
30}
31
32/**
33 * TypeScript Broccoli plugin class.
34 */
35export class TypescriptCompiler extends BroccoliPlugin {
36 private compiler: Compiler | undefined;
37 private diagnosticHandler: DiagnosticsHandler;
38 private options: NormalizedOptions;
39
40 constructor(inputNode: any, options?: TypescriptCompilerOptions) {
41 super([inputNode], {
42 annotation: options && options.annotation,
43 name: "broccoli-typescript-compiler",
44 persistentOutput: true,
45 });
46 const normalizedOptions = normalizeOptions(options || {});
47 this.options = normalizedOptions;
48 this.diagnosticHandler = new DiagnosticsHandler(normalizedOptions);
49 }
50
51 public build() {
52 const token = heimdall.start("TypeScript:compile");
53 let compiler = this.compiler;
54 if (!compiler) {
55 compiler = this.compiler = new Compiler(
56 toAbsolutePath(this.inputPaths[0]),
57 toAbsolutePath(this.outputPath),
58 this.options,
59 this.diagnosticHandler
60 );
61 }
62 compiler.compile();
63 heimdall.stop(token);
64 }
65
66 public setDiagnosticWriter(write: (message: string) => void) {
67 this.diagnosticHandler.setWrite(write);
68 }
69}