@@ -48,33 +48,57 @@ 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
+ public restartServer ( ) : void {
58
58
console . log ( 'Restarting app...' ) ;
59
59
60
- this . _stopServer ( ) ;
60
+ this . stopServer ( ) ;
61
61
62
- this . _startServer ( ) ;
62
+ this . startServer ( ) ;
63
63
}
64
64
65
+ public startServer ( cb ?: ( ) => void ) : void {
66
+ const { args, nodeArgs, cwd, env } = this . options ;
67
+ if ( ! this . _entrypoint ) throw new Error ( 'run-script-webpack-plugin requires an entrypoint.' ) ;
68
+
69
+ const child = fork ( this . _entrypoint , args , {
70
+ execArgv : nodeArgs ,
71
+ stdio : 'inherit' ,
72
+ cwd,
73
+ env,
74
+ } ) ;
75
+
76
+ setTimeout ( ( ) => {
77
+ this . worker = child ;
78
+ cb ?.( )
79
+ } , 0 ) ;
80
+ }
81
+
82
+ public stopServer ( options :Partial < { force : boolean } > = { } ) {
83
+ const signal = getSignal ( this . options . signal ) ;
84
+ if ( ( signal || options . force ) && ( this . worker ?. pid ) ) {
85
+ process . kill ( this . worker . pid , signal ) ;
86
+ }
87
+ } ;
88
+
65
89
private afterEmit = ( compilation : Compilation , cb : ( ) => void ) : void => {
66
90
if ( this . worker && this . worker . connected && this . worker ?. pid ) {
67
91
if ( this . options . autoRestart ) {
68
- this . _restartServer ( ) ;
92
+ this . restartServer ( ) ;
69
93
cb ( ) ;
70
94
return ;
71
95
}
72
- this . _stopServer ( ) ;
96
+ this . stopServer ( ) ;
73
97
cb ( ) ;
74
98
return ;
75
99
}
76
100
77
- this . startServer ( compilation , cb ) ;
101
+ this . _startServer ( compilation , cb ) ;
78
102
} ;
79
103
80
104
apply = ( compiler : Compiler ) : void => {
@@ -84,7 +108,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
84
108
) ;
85
109
} ;
86
110
87
- private startServer = ( compilation : Compilation , cb : ( ) => void ) : void => {
111
+ private _startServer = ( compilation : Compilation , cb : ( ) => void ) : void => {
88
112
const { assets, compiler } = compilation ;
89
113
const { options } = this ;
90
114
let name ;
@@ -100,9 +124,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
100
124
name = names [ 0 ] ;
101
125
if ( names . length > 1 ) {
102
126
console . log (
103
- `More than one entry built, selected ${ name } . All names: ${ names . join (
104
- ' ' ,
105
- ) } `,
127
+ `More than one entry built, selected ${ name } . All names: ${ names . join ( ' ' , ) } ` ,
106
128
) ;
107
129
}
108
130
}
@@ -111,30 +133,6 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
111
133
}
112
134
113
135
this . _entrypoint = `${ compiler . options . output . path } /${ name } ` ;
114
- this . _startServer ( cb ) ;
115
- } ;
116
-
117
- private _startServer ( cb ?: ( ) => void ) : void {
118
- const { args, nodeArgs, cwd, env } = this . options ;
119
- if ( ! this . _entrypoint ) throw new Error ( 'run-script-webpack-plugin requires an entrypoint.' ) ;
120
-
121
- const child = fork ( this . _entrypoint , args , {
122
- execArgv : nodeArgs ,
123
- stdio : 'inherit' ,
124
- cwd,
125
- env,
126
- } ) ;
127
-
128
- setTimeout ( ( ) => {
129
- this . worker = child ;
130
- cb ?.( )
131
- } , 0 ) ;
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
- }
136
+ this . startServer ( cb ) ;
139
137
} ;
140
138
}
0 commit comments