@@ -12,22 +12,10 @@ import { createCodeframeFormatter } from './formatter/codeframeFormatter';
12
12
import { FsHelper } from './FsHelper' ;
13
13
import { Message } from './Message' ;
14
14
15
- import { AsyncSeriesHook , SyncHook } from 'tapable ' ;
15
+ import { getForkTsCheckerWebpackPluginHooks } from './hooks ' ;
16
16
17
17
const checkerPluginName = 'fork-ts-checker-webpack-plugin' ;
18
18
19
- const customHooks = {
20
- forkTsCheckerServiceBeforeStart : 'fork-ts-checker-service-before-start' ,
21
- forkTsCheckerCancel : 'fork-ts-checker-cancel' ,
22
- forkTsCheckerServiceStartError : 'fork-ts-checker-service-start-error' ,
23
- forkTsCheckerWaiting : 'fork-ts-checker-waiting' ,
24
- forkTsCheckerServiceStart : 'fork-ts-checker-service-start' ,
25
- forkTsCheckerReceive : 'fork-ts-checker-receive' ,
26
- forkTsCheckerServiceOutOfMemory : 'fork-ts-checker-service-out-of-memory' ,
27
- forkTsCheckerEmit : 'fork-ts-checker-emit' ,
28
- forkTsCheckerDone : 'fork-ts-checker-done'
29
- } ;
30
-
31
19
type Formatter = ( message : NormalizedMessage , useColors : boolean ) => string ;
32
20
33
21
interface Logger {
@@ -183,6 +171,10 @@ class ForkTsCheckerWebpackPlugin {
183
171
this . vue = options . vue === true ; // default false
184
172
}
185
173
174
+ private static getCompilerHooks ( compiler : webpack . Compiler ) {
175
+ return getForkTsCheckerWebpackPluginHooks ( compiler ) ;
176
+ }
177
+
186
178
private static createFormatter ( type : 'default' | 'codeframe' , options : any ) {
187
179
switch ( type ) {
188
180
case 'default' :
@@ -219,9 +211,6 @@ class ForkTsCheckerWebpackPlugin {
219
211
}
220
212
221
213
if ( tsconfigOk && tslintOk ) {
222
- if ( 'hooks' in compiler ) {
223
- this . registerCustomHooks ( ) ;
224
- }
225
214
this . pluginStart ( ) ;
226
215
this . pluginStop ( ) ;
227
216
this . pluginCompile ( ) ;
@@ -275,7 +264,7 @@ class ForkTsCheckerWebpackPlugin {
275
264
} ;
276
265
277
266
if ( 'hooks' in this . compiler ) {
278
- // webpack 4
267
+ // webpack 4+
279
268
this . compiler . hooks . run . tapAsync ( checkerPluginName , run ) ;
280
269
this . compiler . hooks . watchRun . tapAsync ( checkerPluginName , watchRun ) ;
281
270
} else {
@@ -297,7 +286,7 @@ class ForkTsCheckerWebpackPlugin {
297
286
} ;
298
287
299
288
if ( 'hooks' in this . compiler ) {
300
- // webpack 4
289
+ // webpack 4+
301
290
this . compiler . hooks . watchClose . tap ( checkerPluginName , watchClose ) ;
302
291
this . compiler . hooks . done . tap ( checkerPluginName , done ) ;
303
292
} else {
@@ -311,88 +300,17 @@ class ForkTsCheckerWebpackPlugin {
311
300
} ) ;
312
301
}
313
302
314
- private registerCustomHooks ( ) {
315
- if (
316
- this . compiler . hooks . forkTsCheckerServiceBeforeStart ||
317
- this . compiler . hooks . forkTsCheckerCancel ||
318
- this . compiler . hooks . forkTsCheckerServiceStartError ||
319
- this . compiler . hooks . forkTsCheckerWaiting ||
320
- this . compiler . hooks . forkTsCheckerServiceStart ||
321
- this . compiler . hooks . forkTsCheckerReceive ||
322
- this . compiler . hooks . forkTsCheckerServiceOutOfMemory ||
323
- this . compiler . hooks . forkTsCheckerDone ||
324
- this . compiler . hooks . forkTsCheckerEmit
325
- ) {
326
- throw new Error (
327
- 'fork-ts-checker-webpack-plugin hooks are already in use'
328
- ) ;
329
- }
330
- this . compiler . hooks . forkTsCheckerServiceBeforeStart = new AsyncSeriesHook (
331
- [ ]
332
- ) ;
333
-
334
- this . compiler . hooks . forkTsCheckerCancel = new SyncHook ( [
335
- 'cancellationToken'
336
- ] ) ;
337
- this . compiler . hooks . forkTsCheckerServiceStartError = new SyncHook ( [
338
- 'error'
339
- ] ) ;
340
- this . compiler . hooks . forkTsCheckerWaiting = new SyncHook ( [ 'hasTsLint' ] ) ;
341
- this . compiler . hooks . forkTsCheckerServiceStart = new SyncHook ( [
342
- 'tsconfigPath' ,
343
- 'tslintPath' ,
344
- 'watchPaths' ,
345
- 'workersNumber' ,
346
- 'memoryLimit'
347
- ] ) ;
348
- this . compiler . hooks . forkTsCheckerReceive = new SyncHook ( [
349
- 'diagnostics' ,
350
- 'lints'
351
- ] ) ;
352
- this . compiler . hooks . forkTsCheckerServiceOutOfMemory = new SyncHook ( [ ] ) ;
353
- this . compiler . hooks . forkTsCheckerEmit = new SyncHook ( [
354
- 'diagnostics' ,
355
- 'lints' ,
356
- 'elapsed'
357
- ] ) ;
358
- this . compiler . hooks . forkTsCheckerDone = new SyncHook ( [
359
- 'diagnostics' ,
360
- 'lints' ,
361
- 'elapsed'
362
- ] ) ;
363
-
364
- // for backwards compatibility
365
- this . compiler . _pluginCompat . tap ( checkerPluginName , ( options : any ) => {
366
- switch ( options . name ) {
367
- case customHooks . forkTsCheckerServiceBeforeStart :
368
- options . async = true ;
369
- break ;
370
- case customHooks . forkTsCheckerCancel :
371
- case customHooks . forkTsCheckerServiceStartError :
372
- case customHooks . forkTsCheckerWaiting :
373
- case customHooks . forkTsCheckerServiceStart :
374
- case customHooks . forkTsCheckerReceive :
375
- case customHooks . forkTsCheckerServiceOutOfMemory :
376
- case customHooks . forkTsCheckerEmit :
377
- case customHooks . forkTsCheckerDone :
378
- return true ;
379
- }
380
- return undefined ;
381
- } ) ;
382
- }
383
-
384
303
private pluginCompile ( ) {
385
304
if ( 'hooks' in this . compiler ) {
386
- // webpack 4
305
+ // webpack 4+
306
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks ( this . compiler ) ;
387
307
this . compiler . hooks . compile . tap ( checkerPluginName , ( ) => {
388
308
this . compilationDone = false ;
389
- this . compiler . hooks . forkTsCheckerServiceBeforeStart . callAsync ( ( ) => {
309
+ hooks . forkTsCheckerServiceBeforeStart . callAsync ( ( ) => {
390
310
if ( this . cancellationToken ) {
391
311
// request cancellation if there is not finished job
392
312
this . cancellationToken . requestCancellation ( ) ;
393
- this . compiler . hooks . forkTsCheckerCancel . call (
394
- this . cancellationToken
395
- ) ;
313
+ hooks . forkTsCheckerCancel . call ( this . cancellationToken ) ;
396
314
}
397
315
this . checkDone = false ;
398
316
@@ -416,7 +334,7 @@ class ForkTsCheckerWebpackPlugin {
416
334
) ;
417
335
}
418
336
419
- this . compiler . hooks . forkTsCheckerServiceStartError . call ( error ) ;
337
+ hooks . forkTsCheckerServiceStartError . call ( error ) ;
420
338
}
421
339
} ) ;
422
340
} ) ;
@@ -488,7 +406,7 @@ class ForkTsCheckerWebpackPlugin {
488
406
} ;
489
407
490
408
if ( 'hooks' in this . compiler ) {
491
- // webpack 4
409
+ // webpack 4+
492
410
this . compiler . hooks . emit . tapAsync ( checkerPluginName , emit ) ;
493
411
} else {
494
412
// webpack 2 / 3
@@ -498,7 +416,8 @@ class ForkTsCheckerWebpackPlugin {
498
416
499
417
private pluginDone ( ) {
500
418
if ( 'hooks' in this . compiler ) {
501
- // webpack 4
419
+ // webpack 4+
420
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks ( this . compiler ) ;
502
421
this . compiler . hooks . done . tap (
503
422
checkerPluginName ,
504
423
( _stats : webpack . Stats ) => {
@@ -510,9 +429,7 @@ class ForkTsCheckerWebpackPlugin {
510
429
this . doneCallback ( ) ;
511
430
} else {
512
431
if ( this . compiler ) {
513
- this . compiler . hooks . forkTsCheckerWaiting . call (
514
- this . tslint !== false
515
- ) ;
432
+ hooks . forkTsCheckerWaiting . call ( this . tslint !== false ) ;
516
433
}
517
434
if ( ! this . silent && this . logger ) {
518
435
this . logger . info (
@@ -584,8 +501,9 @@ class ForkTsCheckerWebpackPlugin {
584
501
) ;
585
502
586
503
if ( 'hooks' in this . compiler ) {
587
- // webpack 4
588
- this . compiler . hooks . forkTsCheckerServiceStart . call (
504
+ // webpack 4+
505
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks ( this . compiler ) ;
506
+ hooks . forkTsCheckerServiceStart . call (
589
507
this . tsconfigPath ,
590
508
this . tslintPath ,
591
509
this . watchPaths ,
@@ -707,11 +625,9 @@ class ForkTsCheckerWebpackPlugin {
707
625
}
708
626
709
627
if ( 'hooks' in this . compiler ) {
710
- // webpack 4
711
- this . compiler . hooks . forkTsCheckerReceive . call (
712
- this . diagnostics ,
713
- this . lints
714
- ) ;
628
+ // webpack 4+
629
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks ( this . compiler ) ;
630
+ hooks . forkTsCheckerReceive . call ( this . diagnostics , this . lints ) ;
715
631
} else {
716
632
// webpack 2 / 3
717
633
this . compiler . applyPlugins (
@@ -733,8 +649,11 @@ class ForkTsCheckerWebpackPlugin {
733
649
// probably out of memory :/
734
650
if ( this . compiler ) {
735
651
if ( 'hooks' in this . compiler ) {
736
- // webpack 4
737
- this . compiler . hooks . forkTsCheckerServiceOutOfMemory . call ( ) ;
652
+ // webpack 4+
653
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks (
654
+ this . compiler
655
+ ) ;
656
+ hooks . forkTsCheckerServiceOutOfMemory . call ( ) ;
738
657
} else {
739
658
// webpack 2 / 3
740
659
this . compiler . applyPlugins ( 'fork-ts-checker-service-out-of-memory' ) ;
@@ -761,12 +680,11 @@ class ForkTsCheckerWebpackPlugin {
761
680
const elapsed = Math . round ( this . elapsed [ 0 ] * 1e9 + this . elapsed [ 1 ] ) ;
762
681
763
682
if ( 'hooks' in this . compiler ) {
764
- // webpack 4
765
- this . compiler . hooks . forkTsCheckerEmit . call (
766
- this . diagnostics ,
767
- this . lints ,
768
- elapsed
683
+ // webpack 4+
684
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks (
685
+ this . compiler
769
686
) ;
687
+ hooks . forkTsCheckerEmit . call ( this . diagnostics , this . lints , elapsed ) ;
770
688
} else {
771
689
// webpack 2 / 3
772
690
this . compiler . applyPlugins (
@@ -819,12 +737,11 @@ class ForkTsCheckerWebpackPlugin {
819
737
820
738
if ( this . compiler ) {
821
739
if ( 'hooks' in this . compiler ) {
822
- // webpack 4
823
- this . compiler . hooks . forkTsCheckerDone . call (
824
- this . diagnostics ,
825
- this . lints ,
826
- elapsed
740
+ // webpack 4+
741
+ const hooks = ForkTsCheckerWebpackPlugin . getCompilerHooks (
742
+ this . compiler
827
743
) ;
744
+ hooks . forkTsCheckerDone . call ( this . diagnostics , this . lints , elapsed ) ;
828
745
} else {
829
746
// webpack 2 / 3
830
747
this . compiler . applyPlugins (
0 commit comments