You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Using-the-Compiler-API.md
+141
Original file line number
Diff line number
Diff line change
@@ -357,6 +357,147 @@ function incrementalCompile(): void {
357
357
incrementalCompile();
358
358
```
359
359
360
+
## A minimal solution compiler
361
+
362
+
To compile solution that is project and its dependencies the way `tsc --b` does, we need to create a `SolutionBuilder` which iterates through projects to build. This can be achieved by using `createSolutionBuilder` which takes host which is `SolutionBuilderHost` and can be created using `createSolutionBuilderHost`, root solutions to build and buildOptions. To create a solution builder that also watches for changes (similar to `tsc --b --w`), you can use `createSolutionBuilderWithWatch` and host using `createSolutionBuilderWithWatchHost`.
363
+
364
+
```TypeScript
365
+
import*astsfrom"typescript";
366
+
367
+
/**
368
+
* To compile solution similar to tsc --b
369
+
*/
370
+
function compileSolution(rootNames:string, options:ts.BuildOptions):void {
371
+
const host =ts.createSolutionBuilderHost(
372
+
// System like host, default is ts.sys
373
+
/*system*/undefined,
374
+
// createProgram can be passed in here to choose strategy for incremental compiler just like when creating incremental watcher program.
375
+
// Default is ts.createSemanticDiagnosticsBuilderProgram
376
+
/*createProgram*/undefined,
377
+
reportDiagnostic,
378
+
reportSolutionBuilderStatus,
379
+
reportErrorSummary
380
+
);
381
+
382
+
const solution =ts.createSolutionBuilder(
383
+
host,
384
+
rootNames,
385
+
options
386
+
);
387
+
388
+
// Builds the solution
389
+
const exitCode =solution.build();
390
+
console.log(`Process exiting with code '${exitCode}'.`);
391
+
process.exit(exitCode);
392
+
393
+
// // Alternative to build if you want to take custom action for each project,
// console.log(`Working with ${project.project}`);
400
+
//
401
+
// // project.kind has info decides which type of project it is:
402
+
// // ts.InvalidatedProjectKind.Build:: The project needs to be built (create program and emit the files)
403
+
// // ts.InvalidatedProjectKind.UpdateBundle:: The project needs to combine outputs of references and its own files emit (using old output.js and .tsbuildinfo file) to get new output
404
+
// // ts.InvalidatedProjectKind.UpdateOutputFileStamps:: The project needs to update timestamps of existing outputs since output is correct but output timestamp is older than one of its dependency
405
+
//
406
+
// // Finish working with this project to get next project
407
+
// project.done();
408
+
// }
409
+
}
410
+
411
+
/**
412
+
* To compile solution and watch changes similar to tsc --b --w
413
+
*/
414
+
function compileSolutionWithWatch(rootNames:string, options:ts.BuildOptions):void {
0 commit comments