@@ -43,48 +43,72 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
43
43
}
44
44
}
45
45
46
+ public restartServer ( ) : void {
47
+ console . log ( 'Restarting app...' ) ;
48
+
49
+ this . stopServer ( ) ;
50
+
51
+ this . startServer ( ) ;
52
+ }
53
+
54
+ public startServer ( cb ?: ( ) => void ) : void {
55
+ const { args, nodeArgs, cwd, env } = this . options ;
56
+ if ( ! this . _entrypoint ) throw new Error ( 'run-script-webpack-plugin requires an entrypoint.' ) ;
57
+
58
+ const child = fork ( this . _entrypoint , args , {
59
+ execArgv : nodeArgs ,
60
+ stdio : 'inherit' ,
61
+ cwd,
62
+ env,
63
+ } ) ;
64
+
65
+ setTimeout ( ( ) => {
66
+ this . worker = child ;
67
+ cb ?.( ) ;
68
+ } , 0 ) ;
69
+ }
70
+
71
+ public stopServer ( ) {
72
+ const signal = getSignal ( this . options . signal ) ;
73
+ if ( this . worker ?. pid ) {
74
+ process . kill ( this . worker . pid , signal ) ;
75
+ }
76
+ } ;
77
+
78
+ apply = ( compiler : Compiler ) : void => {
79
+ compiler . hooks . afterEmit . tapAsync (
80
+ { name : 'RunScriptPlugin' } ,
81
+ this . afterEmit ,
82
+ ) ;
83
+ } ;
84
+
46
85
private _enableRestarting ( ) : void {
47
86
if ( this . options . keyboard ) {
48
87
process . stdin . setEncoding ( 'utf8' ) ;
49
88
process . stdin . on ( 'data' , ( data : string ) => {
50
89
if ( data . trim ( ) === 'rs' ) {
51
- this . _restartServer ( ) ;
90
+ this . restartServer ( ) ;
52
91
}
53
92
} ) ;
54
93
}
55
94
}
56
95
57
- private _restartServer ( ) : void {
58
- console . log ( 'Restarting app...' ) ;
59
-
60
- this . _stopServer ( ) ;
61
-
62
- this . _startServer ( ) ;
63
- }
64
-
65
96
private afterEmit = ( compilation : Compilation , cb : ( ) => void ) : void => {
66
97
if ( this . worker && this . worker . connected && this . worker ?. pid ) {
67
98
if ( this . options . autoRestart ) {
68
- this . _restartServer ( ) ;
99
+ this . restartServer ( ) ;
69
100
cb ( ) ;
70
101
return ;
71
102
}
72
- this . _stopServer ( ) ;
103
+ this . stopServer ( ) ;
73
104
cb ( ) ;
74
105
return ;
75
106
}
76
107
77
- this . startServer ( compilation , cb ) ;
108
+ this . _startServer ( compilation , cb ) ;
78
109
} ;
79
110
80
- apply = ( compiler : Compiler ) : void => {
81
- compiler . hooks . afterEmit . tapAsync (
82
- { name : 'RunScriptPlugin' } ,
83
- this . afterEmit ,
84
- ) ;
85
- } ;
86
-
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 ( this . worker ?. pid ) {
137
- process . kill ( this . worker . pid , signal ) ;
138
- }
136
+ this . startServer ( cb ) ;
139
137
} ;
140
138
}
0 commit comments