1
- import * as ts from ' typescript' ;
2
- import * as path from ' path' ;
3
- import * as micromatch from ' micromatch' ;
4
- import * as colors from 'colors' ;
5
- import { findResultFor , toUnix } from ' ../helpers' ;
1
+ import * as ts from " typescript" ;
2
+ import * as path from " path" ;
3
+ import * as micromatch from " micromatch" ;
4
+ import chalk from "chalk" ;
5
+ import { findResultFor , toUnix } from " ../helpers" ;
6
6
import {
7
7
Req ,
8
8
Res ,
@@ -16,54 +16,53 @@ import {
16
16
Files ,
17
17
MessageType ,
18
18
TsConfig
19
- } from ' ./protocol' ;
19
+ } from " ./protocol" ;
20
20
21
- import { CaseInsensitiveMap , MapLike } from ' ./fs' ;
22
- import { isCaseInsensitive } from ' ../helpers' ;
21
+ import { CaseInsensitiveMap , MapLike } from " ./fs" ;
22
+ import { isCaseInsensitive } from " ../helpers" ;
23
23
24
24
const caseInsensitive = isCaseInsensitive ( ) ;
25
25
26
26
if ( ! module . parent ) {
27
- process . on ( ' uncaughtException' , function ( err ) {
27
+ process . on ( " uncaughtException" , function ( err ) {
28
28
console . log ( "UNCAUGHT EXCEPTION in awesome-typescript-loader" ) ;
29
29
console . log ( "[Inside 'uncaughtException' event] " , err . message , err . stack ) ;
30
30
} ) ;
31
31
32
- process . on ( ' disconnect' , function ( ) {
32
+ process . on ( " disconnect" , function ( ) {
33
33
process . exit ( ) ;
34
34
} ) ;
35
35
36
- process . on ( ' exit' , ( ) => {
36
+ process . on ( " exit" , ( ) => {
37
37
// console.log('EXIT RUNTIME');
38
38
} ) ;
39
39
40
- createChecker (
41
- process . on . bind ( process , 'message' ) ,
42
- process . send . bind ( process )
43
- ) ;
40
+ createChecker ( process . on . bind ( process , "message" ) , process . send . bind ( process ) ) ;
44
41
} else {
45
42
module . exports . run = function run ( ) {
46
43
let send : ( msg : Req , cb : ( err ?: Error ) => void ) => void ;
47
- let receive = ( msg ) => { } ;
44
+ let receive = msg => { } ;
48
45
49
46
createChecker (
50
47
( receive : ( msg : Req ) => void ) => {
51
48
send = ( msg : Req , cb : ( err ?: Error ) => void ) => {
52
49
receive ( msg ) ;
53
- if ( cb ) { cb ( ) ; }
50
+ if ( cb ) {
51
+ cb ( ) ;
52
+ }
54
53
} ;
55
54
} ,
56
- ( msg ) => receive ( msg )
55
+ msg => receive ( msg )
57
56
) ;
58
57
59
58
return {
60
59
on : ( type : string , cb ) => {
61
- if ( type === ' message' ) {
60
+ if ( type === " message" ) {
62
61
receive = cb ;
63
62
}
64
63
} ,
65
64
send,
66
- kill : ( ) => { }
65
+ kill : ( ) => { }
67
66
} ;
68
67
} ;
69
68
}
@@ -96,7 +95,9 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
96
95
const file = files . get ( fileName ) ;
97
96
if ( ! file ) {
98
97
const text = compiler . sys . readFile ( fileName ) ;
99
- if ( text == null ) { return ; }
98
+ if ( text == null ) {
99
+ return ;
100
+ }
100
101
files . set ( fileName , {
101
102
fileName : fileName ,
102
103
text,
@@ -169,30 +170,37 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
169
170
constructor ( filesRegex : RegExp ) {
170
171
this . filesRegex = filesRegex ;
171
172
172
- let { getCustomTransformers} = loaderConfig ;
173
+ let { getCustomTransformers } = loaderConfig ;
173
174
174
175
if ( typeof getCustomTransformers === "function" ) {
175
176
this . getCustomTransformers = getCustomTransformers ;
176
177
} else if ( typeof getCustomTransformers === "string" ) {
177
178
try {
178
179
getCustomTransformers = require ( getCustomTransformers ) ;
179
180
} catch ( err ) {
180
- throw new Error ( `Failed to load customTransformers from "${ loaderConfig . getCustomTransformers } ": ${ err . message } ` )
181
- } ;
181
+ throw new Error (
182
+ `Failed to load customTransformers from "${ loaderConfig . getCustomTransformers } ": ${ err . message } `
183
+ ) ;
184
+ }
182
185
183
186
if ( typeof getCustomTransformers !== "function" ) {
184
- throw new Error ( `Custom transformers in "${ loaderConfig . getCustomTransformers } " should export a function, got ${ typeof getCustomTransformers } ` )
185
- } ;
187
+ throw new Error (
188
+ `Custom transformers in "${
189
+ loaderConfig . getCustomTransformers
190
+ } " should export a function, got ${ typeof getCustomTransformers } `
191
+ ) ;
192
+ }
186
193
187
194
this . getCustomTransformers = getCustomTransformers ;
188
- } ;
195
+ }
189
196
}
190
197
191
- getProjectVersion ( ) { return projectVersion . toString ( ) ; }
198
+ getProjectVersion ( ) {
199
+ return projectVersion . toString ( ) ;
200
+ }
192
201
193
202
getScriptFileNames ( ) {
194
- const names = files . map ( file => file . fileName )
195
- . filter ( fileName => this . filesRegex . test ( fileName ) ) ;
203
+ const names = files . map ( file => file . fileName ) . filter ( fileName => this . filesRegex . test ( fileName ) ) ;
196
204
return names ;
197
205
}
198
206
@@ -225,9 +233,11 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
225
233
}
226
234
227
235
resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string ) {
228
- const resolved = typeDirectiveNames . map ( directive =>
229
- compiler . resolveTypeReferenceDirective ( directive , containingFile , compilerOptions , compiler . sys )
230
- . resolvedTypeReferenceDirective ) ;
236
+ const resolved = typeDirectiveNames . map (
237
+ directive =>
238
+ compiler . resolveTypeReferenceDirective ( directive , containingFile , compilerOptions , compiler . sys )
239
+ . resolvedTypeReferenceDirective
240
+ ) ;
231
241
232
242
resolved . forEach ( res => {
233
243
if ( res && res . resolvedFileName ) {
@@ -239,8 +249,11 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
239
249
}
240
250
241
251
resolveModuleNames ( moduleNames : string [ ] , containingFile : string ) {
242
- const resolved = moduleNames . map ( module =>
243
- compiler . resolveModuleName ( module , containingFile , compilerOptions , compiler . sys , cache ) . resolvedModule ) ;
252
+ const resolved = moduleNames . map (
253
+ module =>
254
+ compiler . resolveModuleName ( module , containingFile , compilerOptions , compiler . sys , cache )
255
+ . resolvedModule
256
+ ) ;
244
257
245
258
resolved . forEach ( res => {
246
259
if ( res && res . resolvedFileName ) {
@@ -282,7 +295,6 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
282
295
directoryExists ( path : string ) {
283
296
return compiler . sys . directoryExists ( path ) ;
284
297
}
285
-
286
298
}
287
299
288
300
let normalize : ( f : string ) => string ;
@@ -301,24 +313,20 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
301
313
files = new CaseInsensitiveMap ( ) ;
302
314
303
315
if ( compiler . createModuleResolutionCache ) {
304
- cache = compiler . createModuleResolutionCache (
305
- context ,
306
- normalize
307
- ) ;
316
+ cache = compiler . createModuleResolutionCache ( context , normalize ) ;
308
317
}
309
318
310
- instanceName = loaderConfig . instance || ' at-loader' ;
319
+ instanceName = loaderConfig . instance || " at-loader" ;
311
320
312
- host = new Host ( compilerOptions . allowJs
313
- ? TS_AND_JS_FILES
314
- : TS_FILES
315
- ) ;
321
+ host = new Host ( compilerOptions . allowJs ? TS_AND_JS_FILES : TS_FILES ) ;
316
322
317
323
service = compiler . createLanguageService ( host ) ;
318
324
319
325
compilerConfig . fileNames . forEach ( fileName => {
320
326
const text = compiler . sys . readFile ( fileName ) ;
321
- if ( text == null ) { return ; }
327
+ if ( text == null ) {
328
+ return ;
329
+ }
322
330
files . set ( fileName , {
323
331
fileName,
324
332
text,
@@ -369,7 +377,9 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
369
377
return ;
370
378
}
371
379
}
372
- if ( file . text !== text ) { updated = updated || true ; }
380
+ if ( file . text !== text ) {
381
+ updated = updated || true ;
382
+ }
373
383
if ( ! updated ) {
374
384
return false ;
375
385
}
@@ -419,7 +429,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
419
429
compilerOptions : compilerOptions ,
420
430
fileName,
421
431
reportDiagnostics : false ,
422
- transformers : host . getCustomTransformers ? host . getCustomTransformers ( ) : undefined ,
432
+ transformers : host . getCustomTransformers ? host . getCustomTransformers ( ) : undefined
423
433
} ) ;
424
434
425
435
return {
@@ -447,22 +457,23 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
447
457
448
458
function processFiles ( { seq } : Files . Request ) {
449
459
replyOk ( seq , {
450
- files : service . getProgram ( ) . getSourceFiles ( ) . map ( f => f . fileName )
460
+ files : service
461
+ . getProgram ( )
462
+ . getSourceFiles ( )
463
+ . map ( f => f . fileName )
451
464
} ) ;
452
465
}
453
466
454
467
function processDiagnostics ( { seq } : Diagnostics . Request ) {
455
468
let silent = ! ! loaderConfig . silent ;
456
469
457
470
if ( ! silent ) {
458
- console . log ( colors . cyan ( `\n[${ instanceName } ] Checking started in a separate process...` ) ) ;
471
+ console . log ( chalk . cyan ( `\n[${ instanceName } ] Checking started in a separate process...` ) ) ;
459
472
}
460
473
461
474
const program = service . getProgram ( ) ;
462
475
463
- const allDiagnostics = program
464
- . getOptionsDiagnostics ( )
465
- . concat ( program . getGlobalDiagnostics ( ) ) ;
476
+ const allDiagnostics = program . getOptionsDiagnostics ( ) . concat ( program . getGlobalDiagnostics ( ) ) ;
466
477
467
478
const filters : Filter [ ] = [ ] ;
468
479
@@ -482,9 +493,10 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
482
493
let nativeGetter : typeof program . getSourceFiles ;
483
494
if ( filters . length > 0 ) {
484
495
nativeGetter = program . getSourceFiles ;
485
- program . getSourceFiles = ( ) => nativeGetter ( ) . filter ( file => {
486
- return filters . every ( f => f ( file ) ) ;
487
- } ) ;
496
+ program . getSourceFiles = ( ) =>
497
+ nativeGetter ( ) . filter ( file => {
498
+ return filters . every ( f => f ( file ) ) ;
499
+ } ) ;
488
500
}
489
501
490
502
allDiagnostics . push ( ...program . getSyntacticDiagnostics ( ) ) ;
@@ -498,41 +510,40 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
498
510
program . getSourceFiles = nativeGetter ;
499
511
}
500
512
501
- const processedDiagnostics = allDiagnostics
502
- . filter ( diag => ! ignoreDiagnostics [ diag . code ] )
503
- . map ( diagnostic => {
504
- const message = compiler . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) ;
505
- let fileName = diagnostic . file && path . relative ( context , diagnostic . file . fileName ) ;
506
-
507
- if ( fileName && fileName [ 0 ] !== '.' ) {
508
- fileName = './' + toUnix ( fileName ) ;
509
- }
513
+ const processedDiagnostics = allDiagnostics . filter ( diag => ! ignoreDiagnostics [ diag . code ] ) . map ( diagnostic => {
514
+ const message = compiler . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ;
515
+ let fileName = diagnostic . file && path . relative ( context , diagnostic . file . fileName ) ;
510
516
511
- let pretty = '' ;
512
- let line = 0 ;
513
- let character = 0 ;
514
- let code = diagnostic . code ;
517
+ if ( fileName && fileName [ 0 ] !== "." ) {
518
+ fileName = "./" + toUnix ( fileName ) ;
519
+ }
515
520
516
- if ( diagnostic . file ) {
517
- const pos = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
518
- line = pos . line ;
519
- character = pos . character ;
520
- pretty = ( `[${ instanceName } ] ${ colors . red ( fileName ) } :${ line + 1 } :${ character + 1 } \n TS${ code } : ${ colors . red ( message ) } ` ) ;
521
- } else {
522
- pretty = ( colors . red ( `[${ instanceName } ] TS${ code } : ${ message } ` ) ) ;
523
- }
521
+ let pretty = "" ;
522
+ let line = 0 ;
523
+ let character = 0 ;
524
+ let code = diagnostic . code ;
525
+
526
+ if ( diagnostic . file ) {
527
+ const pos = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
528
+ line = pos . line ;
529
+ character = pos . character ;
530
+ pretty = `[${ instanceName } ] ${ chalk . red ( fileName ) } :${ line + 1 } :${ character +
531
+ 1 } \n TS${ code } : ${ chalk . red ( message ) } `;
532
+ } else {
533
+ pretty = chalk . red ( `[${ instanceName } ] TS${ code } : ${ message } ` ) ;
534
+ }
524
535
525
- return {
526
- category : diagnostic . category ,
527
- code : diagnostic . code ,
528
- fileName,
529
- start : diagnostic . start ,
530
- message,
531
- pretty,
532
- line,
533
- character
534
- } ;
535
- } ) ;
536
+ return {
537
+ category : diagnostic . category ,
538
+ code : diagnostic . code ,
539
+ fileName,
540
+ start : diagnostic . start ,
541
+ message,
542
+ pretty,
543
+ line,
544
+ character
545
+ } ;
546
+ } ) ;
536
547
537
548
replyOk ( seq , processedDiagnostics ) ;
538
549
}
@@ -553,7 +564,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
553
564
} as Res ) ;
554
565
}
555
566
556
- receive ( function ( req : Req ) {
567
+ receive ( function ( req : Req ) {
557
568
try {
558
569
switch ( req . type ) {
559
570
case MessageType . Init :
@@ -574,7 +585,6 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
574
585
case MessageType . Files :
575
586
processFiles ( req ) ;
576
587
break ;
577
-
578
588
}
579
589
} catch ( e ) {
580
590
console . error ( `[${ instanceName } ]: Child process failed to process the request: ` , e ) ;
0 commit comments