1
- import { fork , ChildProcess } from 'child_process' ;
2
- import { Compiler , WebpackPluginInstance , Compilation } from 'webpack' ;
1
+ import { ChildProcess , fork } from 'child_process' ;
2
+ import { Compilation , Compiler , WebpackPluginInstance } from 'webpack' ;
3
3
4
4
export type RunScriptWebpackPluginOptions = {
5
5
autoRestart ?: boolean ;
@@ -48,21 +48,18 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
48
48
process . stdin . setEncoding ( 'utf8' ) ;
49
49
process . stdin . on ( 'data' , ( data : string ) => {
50
50
if ( data . trim ( ) === 'rs' ) {
51
- this . _restartServer ( )
51
+ this . _restartServer ( ) ;
52
52
}
53
53
} ) ;
54
54
}
55
55
}
56
56
57
- private _restartServer ( ) :void {
57
+ private _restartServer ( ) : void {
58
58
console . log ( 'Restarting app...' ) ;
59
- if ( this . worker ?. pid ) {
60
- const signal = getSignal ( this . options . signal ) ;
61
- process . kill ( this . worker . pid , signal ) ;
62
- }
63
- this . _startServer ( ( worker ) => {
64
- this . worker = worker ;
65
- } ) ;
59
+
60
+ this . _stopServer ( ) ;
61
+
62
+ this . _startServer ( ) ;
66
63
}
67
64
68
65
private afterEmit = ( compilation : Compilation , cb : ( ) => void ) : void => {
@@ -72,10 +69,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
72
69
cb ( ) ;
73
70
return ;
74
71
}
75
- const signal = getSignal ( this . options . signal ) ;
76
- if ( signal ) {
77
- process . kill ( this . worker . pid , signal ) ;
78
- }
72
+ this . _stopServer ( ) ;
79
73
cb ( ) ;
80
74
return ;
81
75
}
@@ -86,7 +80,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
86
80
apply = ( compiler : Compiler ) : void => {
87
81
compiler . hooks . afterEmit . tapAsync (
88
82
{ name : 'RunScriptPlugin' } ,
89
- this . afterEmit
83
+ this . afterEmit ,
90
84
) ;
91
85
} ;
92
86
@@ -99,16 +93,16 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
99
93
name = options . name ;
100
94
if ( ! assets [ name ] ) {
101
95
console . error (
102
- `Entry ${ name } not found. Try one of: ${ names . join ( ' ' ) } `
96
+ `Entry ${ name } not found. Try one of: ${ names . join ( ' ' ) } ` ,
103
97
) ;
104
98
}
105
99
} else {
106
100
name = names [ 0 ] ;
107
101
if ( names . length > 1 ) {
108
102
console . log (
109
103
`More than one entry built, selected ${ name } . All names: ${ names . join (
110
- ' '
111
- ) } `
104
+ ' ' ,
105
+ ) } `,
112
106
) ;
113
107
}
114
108
}
@@ -117,13 +111,10 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
117
111
}
118
112
119
113
this . _entrypoint = `${ compiler . options . output . path } /${ name } ` ;
120
- this . _startServer ( ( worker ) => {
121
- this . worker = worker ;
122
- cb ( ) ;
123
- } ) ;
114
+ this . _startServer ( cb ) ;
124
115
} ;
125
116
126
- private _startServer ( cb : ( arg0 : ChildProcess ) => void ) : void {
117
+ private _startServer ( cb ? : ( ) => void ) : void {
127
118
const { args, nodeArgs, cwd, env } = this . options ;
128
119
if ( ! this . _entrypoint ) throw new Error ( 'run-script-webpack-plugin requires an entrypoint.' ) ;
129
120
@@ -133,6 +124,17 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
133
124
cwd,
134
125
env,
135
126
} ) ;
136
- setTimeout ( ( ) => cb ( child ) , 0 ) ;
127
+
128
+ setTimeout ( ( ) => {
129
+ this . worker = child ;
130
+ cb ?.( )
131
+ } , 0 ) ;
137
132
}
133
+
134
+ private _stopServer ( ) {
135
+ const signal = getSignal ( this . options . signal ) ;
136
+ if ( signal && ( this . worker ?. pid ) ) {
137
+ process . kill ( this . worker . pid , signal ) ;
138
+ }
139
+ } ;
138
140
}
0 commit comments