@@ -48,33 +48,57 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
4848 process . stdin . setEncoding ( 'utf8' ) ;
4949 process . stdin . on ( 'data' , ( data : string ) => {
5050 if ( data . trim ( ) === 'rs' ) {
51- this . _restartServer ( ) ;
51+ this . restartServer ( ) ;
5252 }
5353 } ) ;
5454 }
5555 }
5656
57- private _restartServer ( ) : void {
57+ public restartServer ( ) : void {
5858 console . log ( 'Restarting app...' ) ;
5959
60- this . _stopServer ( ) ;
60+ this . stopServer ( ) ;
6161
62- this . _startServer ( ) ;
62+ this . startServer ( ) ;
6363 }
6464
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+
6589 private afterEmit = ( compilation : Compilation , cb : ( ) => void ) : void => {
6690 if ( this . worker && this . worker . connected && this . worker ?. pid ) {
6791 if ( this . options . autoRestart ) {
68- this . _restartServer ( ) ;
92+ this . restartServer ( ) ;
6993 cb ( ) ;
7094 return ;
7195 }
72- this . _stopServer ( ) ;
96+ this . stopServer ( ) ;
7397 cb ( ) ;
7498 return ;
7599 }
76100
77- this . startServer ( compilation , cb ) ;
101+ this . _startServer ( compilation , cb ) ;
78102 } ;
79103
80104 apply = ( compiler : Compiler ) : void => {
@@ -84,7 +108,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
84108 ) ;
85109 } ;
86110
87- private startServer = ( compilation : Compilation , cb : ( ) => void ) : void => {
111+ private _startServer = ( compilation : Compilation , cb : ( ) => void ) : void => {
88112 const { assets, compiler } = compilation ;
89113 const { options } = this ;
90114 let name ;
@@ -100,9 +124,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
100124 name = names [ 0 ] ;
101125 if ( names . length > 1 ) {
102126 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 ( ' ' , ) } ` ,
106128 ) ;
107129 }
108130 }
@@ -111,30 +133,6 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
111133 }
112134
113135 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 ) ;
139137 } ;
140138}
0 commit comments