@@ -14,7 +14,8 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
1414 public $hostInfo : IHostInfo ,
1515 private $logger : ILogger ,
1616 private $pluginsService : IPluginsService ,
17- private $mobileHelper : Mobile . IMobileHelper
17+ private $mobileHelper : Mobile . IMobileHelper ,
18+ private $cleanupService : ICleanupService
1819 ) { super ( ) ; }
1920
2021 public async compileWithWatch ( platformData : IPlatformData , projectData : IProjectData , prepareData : IPrepareData ) : Promise < any > {
@@ -70,15 +71,19 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
7071 }
7172 } ) ;
7273
73- childProcess . on ( "close" , ( arg : any ) => {
74+ childProcess . on ( "error" , ( err ) => {
75+ this . $logger . trace ( `Unable to start webpack process in watch mode. Error is: ${ err } ` ) ;
76+ reject ( err ) ;
77+ } ) ;
78+
79+ childProcess . on ( "close" , async ( arg : any ) => {
80+ await this . $cleanupService . removeKillProcess ( childProcess . pid . toString ( ) ) ;
81+
7482 const exitCode = typeof arg === "number" ? arg : arg && arg . code ;
75- if ( exitCode === 0 ) {
76- resolve ( childProcess ) ;
77- } else {
78- const error = new Error ( `Executing webpack failed with exit code ${ exitCode } .` ) ;
79- error . code = exitCode ;
80- reject ( error ) ;
81- }
83+ this . $logger . trace ( `Webpack process exited with code ${ exitCode } when we expected it to be long living with watch.` ) ;
84+ const error = new Error ( `Executing webpack failed with exit code ${ exitCode } .` ) ;
85+ error . code = exitCode ;
86+ reject ( error ) ;
8287 } ) ;
8388 } ) ;
8489 }
@@ -91,7 +96,14 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
9196 }
9297
9398 const childProcess = await this . startWebpackProcess ( platformData , projectData , prepareData ) ;
94- childProcess . on ( "close" , ( arg : any ) => {
99+ childProcess . on ( "error" , ( err ) => {
100+ this . $logger . trace ( `Unable to start webpack process in non-watch mode. Error is: ${ err } ` ) ;
101+ reject ( err ) ;
102+ } ) ;
103+
104+ childProcess . on ( "close" , async ( arg : any ) => {
105+ await this . $cleanupService . removeKillProcess ( childProcess . pid . toString ( ) ) ;
106+
95107 const exitCode = typeof arg === "number" ? arg : arg && arg . code ;
96108 if ( exitCode === 0 ) {
97109 resolve ( ) ;
@@ -104,11 +116,15 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
104116 } ) ;
105117 }
106118
107- public stopWebpackCompiler ( platform : string ) : void {
119+ public async stopWebpackCompiler ( platform : string ) : Promise < void > {
108120 if ( platform ) {
109- this . stopWebpackForPlatform ( platform ) ;
121+ await this . stopWebpackForPlatform ( platform ) ;
110122 } else {
111- Object . keys ( this . webpackProcesses ) . forEach ( pl => this . stopWebpackForPlatform ( pl ) ) ;
123+ const webpackedPlatforms = Object . keys ( this . webpackProcesses ) ;
124+
125+ for ( let i = 0 ; i < webpackedPlatforms . length ; i ++ ) {
126+ await this . stopWebpackForPlatform ( webpackedPlatforms [ i ] ) ;
127+ }
112128 }
113129 }
114130
@@ -136,9 +152,10 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
136152 }
137153
138154 const stdio = prepareData . watch ? [ "inherit" , "inherit" , "inherit" , "ipc" ] : "inherit" ;
139- const childProcess = this . $childProcess . spawn ( "node" , args , { cwd : projectData . projectDir , stdio } ) ;
155+ const childProcess = this . $childProcess . spawn ( process . execPath , args , { cwd : projectData . projectDir , stdio } ) ;
140156
141157 this . webpackProcesses [ platformData . platformNameLowerCase ] = childProcess ;
158+ await this . $cleanupService . addKillProcess ( childProcess . pid . toString ( ) ) ;
142159
143160 return childProcess ;
144161 }
@@ -233,9 +250,10 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
233250 } ;
234251 }
235252
236- private stopWebpackForPlatform ( platform : string ) {
253+ private async stopWebpackForPlatform ( platform : string ) {
237254 this . $logger . trace ( `Stopping webpack watch for platform ${ platform } .` ) ;
238255 const webpackProcess = this . webpackProcesses [ platform ] ;
256+ await this . $cleanupService . removeKillProcess ( webpackProcess . pid . toString ( ) ) ;
239257 if ( webpackProcess ) {
240258 webpackProcess . kill ( "SIGINT" ) ;
241259 delete this . webpackProcesses [ platform ] ;
0 commit comments