@@ -13,6 +13,23 @@ import createDefaultFormatter = require('./formatter/defaultFormatter');
13
13
import createCodeframeFormatter = require( './formatter/codeframeFormatter' ) ;
14
14
import Message from './Message' ;
15
15
16
+ const AsyncSeriesHook = require ( "tapable" ) . AsyncSeriesHook ;
17
+ const SyncHook = require ( "tapable" ) . SyncHook ;
18
+
19
+ const checkerPluginName = 'fork-ts-checker-webpack-plugin' ;
20
+
21
+ const customHooks = {
22
+ forkTsCheckerServiceBeforeStart : 'fork-ts-checker-service-before-start' ,
23
+ forkTsCheckerCancel : 'fork-ts-checker-cancel' ,
24
+ forkTsCheckerServiceStartError : 'fork-ts-checker-service-start-error' ,
25
+ forkTsCheckerWaiting : 'fork-ts-checker-waiting' ,
26
+ forkTsCheckerServiceStart : 'fork-ts-checker-service-start' ,
27
+ forkTsCheckerReceive : 'fork-ts-checker-receive' ,
28
+ forkTsCheckerServiceOutOfMemory : 'fork-ts-checker-service-out-of-memory' ,
29
+ forkTsCheckerEmit : 'fork-ts-checker-emit' ,
30
+ forkTsCheckerDone : 'fork-ts-checker-done'
31
+ } ;
32
+
16
33
type Formatter = ( message : NormalizedMessage , useColors : boolean ) => string ;
17
34
18
35
interface Options {
@@ -163,6 +180,7 @@ class ForkTsCheckerWebpackPlugin {
163
180
}
164
181
165
182
if ( tsconfigOk && tslintOk ) {
183
+ this . registerCustomHooks ( ) ;
166
184
this . pluginStart ( ) ;
167
185
this . pluginStop ( ) ;
168
186
this . pluginCompile ( ) ;
@@ -199,40 +217,87 @@ class ForkTsCheckerWebpackPlugin {
199
217
}
200
218
201
219
pluginStart ( ) {
202
- this . compiler . plugin ( 'run' , ( _compiler : webpack . Compiler , callback : ( ) => void ) => {
203
- this . isWatching = false ;
204
- callback ( ) ;
205
- } ) ;
220
+ this . compiler . hooks . run . tapAsync ( checkerPluginName ,
221
+ ( _compiler : webpack . Compiler , callback : ( ) => void ) => {
222
+ this . isWatching = false ;
223
+ callback ( ) ;
224
+ } ) ;
206
225
207
- this . compiler . plugin ( 'watch-run' , ( _watching : webpack . Watching , callback : ( ) => void ) => {
208
- this . isWatching = true ;
209
- callback ( ) ;
210
- } ) ;
226
+ this . compiler . hooks . watchRun . tapAsync ( checkerPluginName ,
227
+ ( _compiler : webpack . Compiler , callback : ( ) => void ) => {
228
+ this . isWatching = true ;
229
+ callback ( ) ;
230
+ } ) ;
211
231
}
212
232
213
233
pluginStop ( ) {
214
- this . compiler . plugin ( 'watch-close' , ( ) => {
215
- this . killService ( ) ;
216
- } ) ;
217
-
218
- this . compiler . plugin ( 'done' , ( ) => {
219
- if ( ! this . isWatching ) {
234
+ this . compiler . hooks . watchClose . tap ( checkerPluginName ,
235
+ ( ) => {
220
236
this . killService ( ) ;
221
- }
222
- } ) ;
237
+ } ) ;
238
+
239
+ this . compiler . hooks . done . tap ( checkerPluginName ,
240
+ ( _stats : webpack . Stats ) => {
241
+ if ( ! this . isWatching ) {
242
+ this . killService ( ) ;
243
+ }
244
+ } ) ;
223
245
224
246
process . on ( 'exit' , ( ) => {
225
247
this . killService ( ) ;
226
248
} ) ;
227
249
}
228
250
251
+ registerCustomHooks ( ) {
252
+ if ( this . compiler . hooks . forkTsCheckerServiceBeforeStart
253
+ || this . compiler . hooks . forkTsCheckerCancel
254
+ || this . compiler . hooks . forkTsCheckerServiceStartError
255
+ || this . compiler . hooks . forkTsCheckerWaiting
256
+ || this . compiler . hooks . forkTsCheckerServiceStart
257
+ || this . compiler . hooks . forkTsCheckerReceive
258
+ || this . compiler . hooks . forkTsCheckerServiceOutOfMemory
259
+ || this . compiler . hooks . forkTsCheckerDone
260
+ || this . compiler . hooks . forkTsCheckerEmit ) {
261
+ throw new Error ( 'fork-ts-checker-webpack-plugin hooks are already in use' ) ;
262
+ }
263
+ this . compiler . hooks . forkTsCheckerServiceBeforeStart = new AsyncSeriesHook ( [ ] ) ;
264
+
265
+ this . compiler . hooks . forkTsCheckerCancel = new SyncHook ( [ ] ) ;
266
+ this . compiler . hooks . forkTsCheckerServiceStartError = new SyncHook ( [ ] ) ;
267
+ this . compiler . hooks . forkTsCheckerWaiting = new SyncHook ( [ ] ) ;
268
+ this . compiler . hooks . forkTsCheckerServiceStart = new SyncHook ( [ ] ) ;
269
+ this . compiler . hooks . forkTsCheckerReceive = new SyncHook ( [ ] ) ;
270
+ this . compiler . hooks . forkTsCheckerServiceOutOfMemory = new SyncHook ( [ ] ) ;
271
+ this . compiler . hooks . forkTsCheckerEmit = new SyncHook ( [ ] ) ;
272
+ this . compiler . hooks . forkTsCheckerDone = new SyncHook ( [ ] ) ;
273
+
274
+ // for backwards compatibility
275
+ this . compiler . _pluginCompat . tap ( checkerPluginName , ( options : any ) => {
276
+ switch ( options . name ) {
277
+ case customHooks . forkTsCheckerServiceBeforeStart :
278
+ options . async = true ;
279
+ break ;
280
+ case customHooks . forkTsCheckerCancel :
281
+ case customHooks . forkTsCheckerServiceStartError :
282
+ case customHooks . forkTsCheckerWaiting :
283
+ case customHooks . forkTsCheckerServiceStart :
284
+ case customHooks . forkTsCheckerReceive :
285
+ case customHooks . forkTsCheckerServiceOutOfMemory :
286
+ case customHooks . forkTsCheckerEmit :
287
+ case customHooks . forkTsCheckerDone :
288
+ return true ;
289
+ }
290
+ return undefined ;
291
+ } ) ;
292
+ }
293
+
229
294
pluginCompile ( ) {
230
- this . compiler . plugin ( ' compile' , ( ) => {
231
- this . compiler . applyPluginsAsync ( 'fork-ts-checker-service-before-start' , ( ) => {
295
+ this . compiler . hooks . compile . tap ( checkerPluginName , ( ) => {
296
+ this . compiler . hooks . forkTsCheckerServiceBeforeStart . callAsync ( ( ) => {
232
297
if ( this . cancellationToken ) {
233
298
// request cancellation if there is not finished job
234
299
this . cancellationToken . requestCancellation ( ) ;
235
- this . compiler . applyPlugins ( 'fork-ts-checker-cancel' , this . cancellationToken ) ;
300
+ this . compiler . hooks . forkTsCheckerCancel . call ( this . cancellationToken ) ;
236
301
}
237
302
this . checkDone = false ;
238
303
this . compilationDone = false ;
@@ -252,14 +317,14 @@ class ForkTsCheckerWebpackPlugin {
252
317
this . logger . error ( this . colors . red ( 'Cannot start checker service: ' + ( error ? error . toString ( ) : 'Unknown error' ) ) ) ;
253
318
}
254
319
255
- this . compiler . applyPlugins ( 'fork-ts-checker-service-start-error' , error ) ;
320
+ this . compiler . hooks . forkTsCheckerServiceStartError . call ( error ) ;
256
321
}
257
322
} ) ;
258
323
} ) ;
259
324
}
260
325
261
326
pluginEmit ( ) {
262
- this . compiler . plugin ( ' emit' , ( compilation : any , callback : ( ) => void ) => {
327
+ this . compiler . hooks . emit . tapAsync ( checkerPluginName , ( compilation : any , callback : ( ) => void ) => {
263
328
if ( this . isWatching && this . async ) {
264
329
callback ( ) ;
265
330
return ;
@@ -276,7 +341,7 @@ class ForkTsCheckerWebpackPlugin {
276
341
}
277
342
278
343
pluginDone ( ) {
279
- this . compiler . plugin ( ' done' , ( ) => {
344
+ this . compiler . hooks . done . tap ( checkerPluginName , ( _stats : webpack . Stats ) => {
280
345
if ( ! this . isWatching || ! this . async ) {
281
346
return ;
282
347
}
@@ -285,10 +350,7 @@ class ForkTsCheckerWebpackPlugin {
285
350
this . doneCallback ( ) ;
286
351
} else {
287
352
if ( this . compiler ) {
288
- this . compiler . applyPlugins (
289
- 'fork-ts-checker-waiting' ,
290
- this . tslint !== false
291
- ) ;
353
+ this . compiler . hooks . forkTsCheckerWaiting . call ( this . tslint !== false ) ;
292
354
}
293
355
if ( ! this . silent && this . logger ) {
294
356
this . logger . info (
@@ -326,8 +388,7 @@ class ForkTsCheckerWebpackPlugin {
326
388
}
327
389
) ;
328
390
329
- this . compiler . applyPlugins (
330
- 'fork-ts-checker-service-start' ,
391
+ this . compiler . hooks . forkTsCheckerServiceStart . call (
331
392
this . tsconfigPath ,
332
393
this . tslintPath ,
333
394
this . watchPaths ,
@@ -398,7 +459,7 @@ class ForkTsCheckerWebpackPlugin {
398
459
) ;
399
460
}
400
461
401
- this . compiler . applyPlugins ( 'fork-ts-checker-receive' , this . diagnostics , this . lints ) ;
462
+ this . compiler . hooks . forkTsCheckerReceive . call ( this . diagnostics , this . lints ) ;
402
463
403
464
if ( this . compilationDone ) {
404
465
( this . isWatching && this . async ) ? this . doneCallback ( ) : this . emitCallback ( ) ;
@@ -409,7 +470,7 @@ class ForkTsCheckerWebpackPlugin {
409
470
if ( signal === 'SIGABRT' ) {
410
471
// probably out of memory :/
411
472
if ( this . compiler ) {
412
- this . compiler . applyPlugins ( 'fork-ts-checker-service-out-of-memory' ) ;
473
+ this . compiler . hooks . forkTsCheckerServiceOutOfMemory . call ( ) ;
413
474
}
414
475
if ( ! this . silent && this . logger ) {
415
476
this . logger . error (
@@ -426,8 +487,7 @@ class ForkTsCheckerWebpackPlugin {
426
487
return function emitCallback ( this : ForkTsCheckerWebpackPlugin ) {
427
488
const elapsed = Math . round ( this . elapsed [ 0 ] * 1E9 + this . elapsed [ 1 ] ) ;
428
489
429
- this . compiler . applyPlugins (
430
- 'fork-ts-checker-emit' ,
490
+ this . compiler . hooks . forkTsCheckerEmit . call (
431
491
this . diagnostics ,
432
492
this . lints ,
433
493
elapsed
@@ -469,8 +529,7 @@ class ForkTsCheckerWebpackPlugin {
469
529
const elapsed = Math . round ( this . elapsed [ 0 ] * 1E9 + this . elapsed [ 1 ] ) ;
470
530
471
531
if ( this . compiler ) {
472
- this . compiler . applyPlugins (
473
- 'fork-ts-checker-done' ,
532
+ this . compiler . hooks . forkTsCheckerDone . call (
474
533
this . diagnostics ,
475
534
this . lints ,
476
535
elapsed
0 commit comments