Skip to content

Commit c4a387a

Browse files
TheLartiansPerryvw
authored andcommitted
Incremental compilation in watch mode (#488)
* emit only affected files * track transpiler errors * return 0 for success * return 0 for declarations * do a full recompile after transpiler error * forgot about lazy evaluation * test if affected file is a SourceFile
1 parent d0c4f72 commit c4a387a

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/Compiler.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,34 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions):
4343
host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
4444
}
4545

46+
let fullRecompile = true;
4647
host.afterProgramCreate = program => {
4748
const transpiler = new LuaTranspiler(program.getProgram());
49+
let status = transpiler.reportErrors();
50+
51+
if (status === 0) {
52+
if (fullRecompile) {
53+
status = transpiler.emitFilesAndReportErrors();
54+
} else {
55+
while (true) {
56+
const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile();
57+
if (!currentFile) { break; }
58+
59+
if ("fileName" in currentFile.affected) { // test if currentFile.affected is `ts.SourceFile`
60+
const fileStatus = transpiler.emitSourceFile(currentFile.affected);
61+
status |= fileStatus;
62+
} else {
63+
for (const sourceFile of currentFile.affected.getSourceFiles()) {
64+
const fileStatus = transpiler.emitSourceFile(sourceFile);
65+
status |= fileStatus;
66+
}
67+
}
68+
}
69+
}
70+
// do a full recompile after transpiler error.
71+
fullRecompile = status !== 0;
72+
}
4873

49-
const status = transpiler.emitFilesAndReportErrors();
5074
const errorDiagnostic: ts.Diagnostic = {
5175
category: undefined,
5276
code: 6194,

src/LuaTranspiler.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class LuaTranspiler {
3838
return options;
3939
}
4040

41-
private reportErrors(): number {
41+
public reportErrors(): number {
4242
// Get all diagnostics, ignore unsupported extension
4343
const diagnostics = ts.getPreEmitDiagnostics(this.program).filter(diag => diag.code !== 6054);
4444
diagnostics.forEach(diag => this.reportDiagnostic(diag));
@@ -65,13 +65,15 @@ export class LuaTranspiler {
6565
}
6666

6767
public emitFilesAndReportErrors(): number {
68-
const error = this.reportErrors();
69-
if (error > 0) {
70-
return error;
68+
let status = this.reportErrors();
69+
70+
if (status > 0) {
71+
return status;
7172
}
7273

7374
this.program.getSourceFiles().forEach(sourceFile => {
74-
this.emitSourceFile(sourceFile);
75+
const sourceStatus = this.emitSourceFile(sourceFile);
76+
status |= sourceStatus;
7577
});
7678

7779
// Copy lualib to target dir
@@ -81,10 +83,10 @@ export class LuaTranspiler {
8183
this.emitLuaLib();
8284
}
8385

84-
return 0;
86+
return status;
8587
}
8688

87-
public emitSourceFile(sourceFile: ts.SourceFile): void {
89+
public emitSourceFile(sourceFile: ts.SourceFile): number {
8890
if (!sourceFile.isDeclarationFile) {
8991
try {
9092
const rootDir = this.options.rootDir;
@@ -119,11 +121,13 @@ export class LuaTranspiler {
119121
// Graciously handle transpilation errors
120122
console.error("Encountered error parsing file: " + exception.message);
121123
console.error(`${sourceFile.fileName} (${1 + pos.line},${pos.character})\n${exception.stack}`);
124+
return 1;
122125
} else {
123126
throw exception;
124127
}
125128
}
126129
}
130+
return 0;
127131
}
128132

129133
public transpileSourceFile(sourceFile: ts.SourceFile): string {

0 commit comments

Comments
 (0)