Skip to content

Commit d3be118

Browse files
committed
Add sample for incremental compiling
1 parent 2c7140d commit d3be118

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

Diff for: Using-the-Compiler-API.md

+74
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,80 @@ function reportWatchStatusChanged(diagnostic: ts.Diagnostic) {
283283
watchMain();
284284
```
285285

286+
## A minimal incremental compiler
287+
288+
To compile incrementally, we need to create a builder program that reads the older program by reading tsbuildinfo file. This is as simple as calling `createIncrementalProgram`. `createIncrementalProgram` abstracts any interaction with the underlying system in the `IncrementalCompilerHost` interface. The `IncrementalCompilerHost` allows the compiler to read and write files, get the current directory, ensure that files and directories exist, and query some of the underlying system properties such as case sensitivity and new line characters. For convenience, we expose a function to create a default host using `createIncrementalCompilerHost`.
289+
290+
```TypeScript
291+
import * as ts from "typescript";
292+
293+
function incrementalCompile(): void {
294+
const configPath = ts.findConfigFile(
295+
/*searchPath*/ "./",
296+
ts.sys.fileExists,
297+
"tsconfig.json"
298+
);
299+
if (!configPath) {
300+
throw new Error("Could not find a valid 'tsconfig.json'.");
301+
}
302+
303+
const config = ts.getParsedCommandLineOfConfigFile(
304+
configPath,
305+
/*optionsToExtend*/ { incremental: true },
306+
/*host*/ {
307+
...ts.sys,
308+
onUnRecoverableConfigFileDiagnostic: d => console.error(ts.flattenDiagnosticMessageText(d, "\n"));
309+
}
310+
);
311+
if (!config) {
312+
throw new Error("Could not parse 'tsconfig.json'.");
313+
}
314+
315+
const program = ts.createIncrementalProgram({
316+
rootName: config.fileNames,
317+
options: config.options,
318+
configFileParsingDiagnostics: ts.getConfigFileParsingDiagnostics(config),
319+
projectReferences: config.projectReferences
320+
// createProgram can be passed in here to choose strategy for incremental compiler just like when creating incremental watcher program.
321+
// Default is ts.createSemanticDiagnosticsBuilderProgram
322+
});
323+
const diagnostics = [
324+
...program.getConfigFileParsingDiagnostics(),
325+
...program.getSyntacticDiagnostics(),
326+
...program.getOptionsDiagnostics(),
327+
...program.getGlobalDiagnostics(),
328+
...program.getSemanticDiagnostics() // Get the diagnostics before emit to cache them in the buildInfo file.
329+
];
330+
const emitResult = program.emit();
331+
const allDiagnostics = diagnostics.concat(emitResult.diagnostics);
332+
333+
allDiagnostics.forEach(diagnostic => {
334+
if (diagnostic.file) {
335+
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
336+
diagnostic.start!
337+
);
338+
let message = ts.flattenDiagnosticMessageText(
339+
diagnostic.messageText,
340+
"\n"
341+
);
342+
console.log(
343+
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
344+
);
345+
} else {
346+
console.log(
347+
`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`
348+
);
349+
}
350+
});
351+
352+
let exitCode = emitResult.emitSkipped ? 1 : 0;
353+
console.log(`Process exiting with code '${exitCode}'.`);
354+
process.exit(exitCode);
355+
}
356+
357+
incrementalCompile();
358+
```
359+
286360
## Incremental build support using the language services
287361

288362
> Please refer to the [[Using the Language Service API]] page for more details.

0 commit comments

Comments
 (0)