@@ -29,6 +29,10 @@ export class Output {
2929 private pendingIO = 0 ;
3030
3131 writeJs ( base : string , fileName : string , content : string , sourceMapContent : string , cwd : string , original : input . File ) {
32+ this . pipeRejection ( this . writeJsAsync ( base , fileName , content , sourceMapContent , cwd , original ) , this . streamJs ) ;
33+ }
34+
35+ private async writeJsAsync ( base : string , fileName : string , content : string , sourceMapContent : string , cwd : string , original : input . File ) {
3236 const file = new VinylFile ( {
3337 path : fileName ,
3438 contents : Buffer . from ( content ) ,
@@ -38,7 +42,7 @@ export class Output {
3842
3943 this . pendingIO ++ ;
4044
41- this . applySourceMap ( sourceMapContent , original , file ) . then ( appliedSourceMap => {
45+ await this . applySourceMap ( sourceMapContent , original , file ) . then ( appliedSourceMap => {
4246 if ( appliedSourceMap ) file . sourceMap = JSON . parse ( appliedSourceMap ) ;
4347 this . streamFull . push ( file ) ;
4448 this . streamJs . push ( file ) ;
@@ -48,7 +52,11 @@ export class Output {
4852 } ) ;
4953 }
5054
51- async writeDts ( base : string , fileName : string , content : string , declarationMapContent : string , cwd : string , original : input . File ) {
55+ writeDts ( base : string , fileName : string , content : string , declarationMapContent : string , cwd : string , original : input . File ) {
56+ this . pipeRejection ( this . writeDtsAsync ( base , fileName , content , declarationMapContent , cwd , original ) , this . streamDts ) ;
57+ }
58+
59+ private async writeDtsAsync ( base : string , fileName : string , content : string , declarationMapContent : string , cwd : string , original : input . File ) {
5260 const file = new VinylFile ( {
5361 path : fileName ,
5462 contents : Buffer . from ( content ) ,
@@ -58,7 +66,7 @@ export class Output {
5866
5967 this . pendingIO ++ ;
6068
61- this . applySourceMap ( declarationMapContent , original , file ) . then ( appliedSourceMap => {
69+ await this . applySourceMap ( declarationMapContent , original , file ) . then ( appliedSourceMap => {
6270 if ( appliedSourceMap ) file . sourceMap = JSON . parse ( appliedSourceMap ) ;
6371 this . streamFull . push ( file ) ;
6472 this . streamDts . push ( file ) ;
@@ -118,12 +126,20 @@ export class Output {
118126 }
119127 }
120128
129+ // Avoids UnhandledPromiseRejectionWarning in NodeJS
130+ private pipeRejection < T > ( promise : Promise < T > , alternateStream : stream . Readable ) {
131+ promise . catch ( err => {
132+ this . streamFull . emit ( "error" , err ) ;
133+ alternateStream . emit ( "error" , err ) ;
134+ } ) ;
135+ }
136+
121137 finish ( result : reporter . CompilationResult ) {
122138 this . result = result ;
123139
124140 this . mightFinish ( ) ;
125141 }
126-
142+
127143 private mightFinish ( ) {
128144 if ( this . result === undefined || this . pendingIO !== 0 ) return ;
129145
0 commit comments