@@ -25,31 +25,50 @@ export class Output {
2525 // .d.ts files
2626 streamDts : stream . Readable ;
2727
28+ // Number of pending IO operatrions
29+ private pendingIO = 0 ;
30+
2831 writeJs ( base : string , fileName : string , content : string , sourceMapContent : string , cwd : string , original : input . File ) {
2932 const file = new VinylFile ( {
3033 path : fileName ,
31- contents : new Buffer ( content ) ,
34+ contents : Buffer . from ( content ) ,
3235 cwd,
3336 base
3437 } ) ;
35- const appliedSourceMap = this . applySourceMap ( sourceMapContent , original , file ) ;
36- if ( appliedSourceMap ) file . sourceMap = JSON . parse ( appliedSourceMap ) ;
37- this . streamFull . push ( file ) ;
38- this . streamJs . push ( file ) ;
38+
39+ this . pendingIO ++ ;
40+
41+ this . applySourceMap ( sourceMapContent , original , file ) . then ( appliedSourceMap => {
42+ if ( appliedSourceMap ) file . sourceMap = JSON . parse ( appliedSourceMap ) ;
43+ this . streamFull . push ( file ) ;
44+ this . streamJs . push ( file ) ;
45+
46+ this . pendingIO -- ;
47+ this . mightFinish ( ) ;
48+ } ) ;
3949 }
4050
41- writeDts ( base : string , fileName : string , content : string , cwd : string ) {
51+ async writeDts ( base : string , fileName : string , content : string , declarationMapContent : string , cwd : string , original : input . File ) {
4252 const file = new VinylFile ( {
4353 path : fileName ,
44- contents : new Buffer ( content ) ,
54+ contents : Buffer . from ( content ) ,
4555 cwd,
4656 base
4757 } ) ;
48- this . streamFull . push ( file ) ;
49- this . streamDts . push ( file ) ;
58+
59+ this . pendingIO ++ ;
60+
61+ this . applySourceMap ( declarationMapContent , original , file ) . then ( appliedSourceMap => {
62+ if ( appliedSourceMap ) file . sourceMap = JSON . parse ( appliedSourceMap ) ;
63+ this . streamFull . push ( file ) ;
64+ this . streamDts . push ( file ) ;
65+
66+ this . pendingIO -- ;
67+ this . mightFinish ( ) ;
68+ } ) ;
5069 }
5170
52- private applySourceMap ( sourceMapContent : string , original : input . File , output : VinylFile ) {
71+ private async applySourceMap ( sourceMapContent : string , original : input . File , output : VinylFile ) {
5372 if ( sourceMapContent === undefined ) return undefined ;
5473
5574 const map = JSON . parse ( sourceMapContent ) ;
@@ -62,13 +81,13 @@ export class Output {
6281
6382 delete map . sourceRoot ;
6483
65- const generator = sourceMap . SourceMapGenerator . fromSourceMap ( new sourceMap . SourceMapConsumer ( map ) ) ;
84+ const consumer = await new sourceMap . SourceMapConsumer ( map ) ;
85+ const generator = sourceMap . SourceMapGenerator . fromSourceMap ( consumer ) ;
6686
6787 const sourceMapOrigins = this . project . singleOutput
6888 ? this . project . input . getFileNames ( true ) . map ( fName => this . project . input . getFile ( fName ) )
6989 : [ original ] ;
7090
71-
7291 for ( const sourceFile of sourceMapOrigins ) {
7392 if ( ! sourceFile || ! sourceFile . gulp || ! sourceFile . gulp . sourceMap ) continue ;
7493
@@ -78,8 +97,9 @@ export class Output {
7897 // We should only apply the input mappings if the input mapping isn't empty,
7998 // since `generator.applySourceMap` has a really bad performance on big inputs.
8099 if ( inputMap . mappings !== '' ) {
81- const consumer = new sourceMap . SourceMapConsumer ( inputMap ) ;
82- generator . applySourceMap ( consumer ) ;
100+ const inputConsumer = await new sourceMap . SourceMapConsumer ( inputMap ) ;
101+ generator . applySourceMap ( inputConsumer ) ;
102+ inputConsumer . destroy ( ) ;
83103 }
84104
85105 if ( ! inputMap . sources || ! inputMap . sourcesContent ) continue ;
@@ -89,6 +109,7 @@ export class Output {
89109 generator . setSourceContent ( utils . forwardSlashes ( relative ) , inputMap . sourcesContent [ i ] ) ;
90110 }
91111 }
112+ consumer . destroy ( ) ;
92113 return generator . toString ( ) ;
93114
94115 function relativeToOutput ( fileName : string ) {
@@ -99,7 +120,18 @@ export class Output {
99120
100121 finish ( result : reporter . CompilationResult ) {
101122 this . result = result ;
102- if ( this . project . reporter . finish ) this . project . reporter . finish ( result ) ;
123+
124+ this . mightFinish ( ) ;
125+ }
126+
127+ private mightFinish ( ) {
128+ if ( this . result === undefined || this . pendingIO !== 0 ) return ;
129+
130+ if ( this . project . reporter . finish ) this . project . reporter . finish ( this . result ) ;
131+
132+ if ( reporter . countErrors ( this . result ) !== 0 ) {
133+ this . streamFull . emit ( 'error' , new Error ( "TypeScript: Compilation failed" ) ) ;
134+ }
103135
104136 this . streamFull . emit ( 'finish' ) ;
105137 this . streamFull . push ( null ) ;
@@ -122,6 +154,5 @@ export class Output {
122154 // call reporter callback
123155 if ( this . project . reporter . error ) this . project . reporter . error ( < reporter . TypeScriptError > error , this . project . typescript ) ;
124156 // & emit the error on the stream.
125- this . streamFull . emit ( 'error' , error ) ;
126157 }
127158}
0 commit comments