22/// <reference path="builder.ts" />
33/// <reference path="resolutionCache.ts"/>
44
5- /* @internal */
65namespace ts {
76 export type DiagnosticReporter = ( diagnostic : Diagnostic ) => void ;
87 export type ParseConfigFile = ( configFileName : string , optionsToExtend : CompilerOptions , system : DirectoryStructureHost , reportDiagnostic : DiagnosticReporter , reportWatchDiagnostic : DiagnosticReporter ) => ParsedCommandLine ;
@@ -19,7 +18,7 @@ namespace ts {
1918
2019 // Callbacks to do custom action before creating program and after creating program
2120 beforeCompile ( compilerOptions : CompilerOptions ) : void ;
22- afterCompile ( host : DirectoryStructureHost , program : Program , builder : Builder ) : void ;
21+ afterCompile ( host : DirectoryStructureHost , program : Program ) : void ;
2322 }
2423
2524 const defaultFormatDiagnosticsHost : FormatDiagnosticsHost = sys ? {
@@ -133,6 +132,11 @@ namespace ts {
133132 reportDiagnostic = reportDiagnostic || createDiagnosticReporter ( system , pretty ? reportDiagnosticWithColorAndContext : reportDiagnosticSimply ) ;
134133 reportWatchDiagnostic = reportWatchDiagnostic || createWatchDiagnosticReporter ( system ) ;
135134 parseConfigFile = parseConfigFile || ts . parseConfigFile ;
135+ let builderState : Readonly < BuilderState > | undefined ;
136+ const options : BuilderOptions = {
137+ getCanonicalFileName : createGetCanonicalFileName ( system . useCaseSensitiveFileNames ) ,
138+ computeHash : data => system . createHash ? system . createHash ( data ) : data
139+ } ;
136140 return {
137141 system,
138142 parseConfigFile,
@@ -142,7 +146,9 @@ namespace ts {
142146 afterCompile : compileWatchedProgram ,
143147 } ;
144148
145- function compileWatchedProgram ( host : DirectoryStructureHost , program : Program , builder : Builder ) {
149+ function compileWatchedProgram ( host : DirectoryStructureHost , program : Program ) {
150+ builderState = createBuilderState ( program , options , builderState ) ;
151+
146152 // First get and report any syntactic errors.
147153 const diagnostics = program . getSyntacticDiagnostics ( ) . slice ( ) ;
148154 let reportSemanticDiagnostics = false ;
@@ -163,22 +169,15 @@ namespace ts {
163169 let sourceMaps : SourceMapData [ ] ;
164170 let emitSkipped : boolean ;
165171
166- const result = builder . emitChangedFiles ( program , writeFile ) ;
167- if ( result . length === 0 ) {
168- emitSkipped = true ;
169- }
170- else {
171- for ( const emitOutput of result ) {
172- if ( emitOutput . emitSkipped ) {
173- emitSkipped = true ;
174- }
175- addRange ( diagnostics , emitOutput . diagnostics ) ;
176- sourceMaps = concatenate ( sourceMaps , emitOutput . sourceMaps ) ;
177- }
172+ let affectedEmitResult : AffectedFileEmitResult ;
173+ while ( affectedEmitResult = builderState . emitNextAffectedFile ( program , writeFile ) ) {
174+ emitSkipped = emitSkipped || affectedEmitResult . emitSkipped ;
175+ addRange ( diagnostics , affectedEmitResult . diagnostics ) ;
176+ sourceMaps = addRange ( sourceMaps , affectedEmitResult . sourceMaps ) ;
178177 }
179178
180179 if ( reportSemanticDiagnostics ) {
181- addRange ( diagnostics , builder . getSemanticDiagnostics ( program ) ) ;
180+ addRange ( diagnostics , builderState . getSemanticDiagnostics ( program ) ) ;
182181 }
183182 return handleEmitOutputAndReportErrors ( host , program , emittedFiles , emitSkipped ,
184183 diagnostics , reportDiagnostic ) ;
@@ -299,8 +298,6 @@ namespace ts {
299298 getDirectoryPath ( getNormalizedAbsolutePath ( configFileName , getCurrentDirectory ( ) ) ) :
300299 getCurrentDirectory ( )
301300 ) ;
302- // There is no extra check needed since we can just rely on the program to decide emit
303- const builder = createBuilder ( { getCanonicalFileName, computeHash } ) ;
304301
305302 synchronizeProgram ( ) ;
306303
@@ -334,7 +331,6 @@ namespace ts {
334331 compilerHost . hasChangedAutomaticTypeDirectiveNames = hasChangedAutomaticTypeDirectiveNames ;
335332 program = createProgram ( rootFileNames , compilerOptions , compilerHost , program ) ;
336333 resolutionCache . finishCachingPerDirectoryResolution ( ) ;
337- builder . updateProgram ( program ) ;
338334
339335 // Update watches
340336 updateMissingFilePathsWatch ( program , missingFilesMap || ( missingFilesMap = createMap ( ) ) , watchMissingFilePath ) ;
@@ -356,7 +352,7 @@ namespace ts {
356352 missingFilePathsRequestedForRelease = undefined ;
357353 }
358354
359- afterCompile ( directoryStructureHost , program , builder ) ;
355+ afterCompile ( directoryStructureHost , program ) ;
360356 reportWatchDiagnostic ( createCompilerDiagnostic ( Diagnostics . Compilation_complete_Watching_for_file_changes ) ) ;
361357 }
362358
@@ -640,9 +636,5 @@ namespace ts {
640636 flags
641637 ) ;
642638 }
643-
644- function computeHash ( data : string ) {
645- return system . createHash ? system . createHash ( data ) : data ;
646- }
647639 }
648640}
0 commit comments