Skip to content

Commit 97cacbf

Browse files
fix: handle correctly close event of webpack process
When we run webpack in watch mode and we receive its close event, it means the process had exited and we are not in watch mode anymore. So in this case we have to reject the current promise.
1 parent 3580ff1 commit 97cacbf

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

lib/services/webpack/webpack-compiler-service.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,19 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
7171
}
7272
});
7373

74-
childProcess.on("close", async (arg: any) => {
75-
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
76-
if (exitCode === 0) {
77-
resolve(childProcess);
78-
} else {
79-
const error = new Error(`Executing webpack failed with exit code ${exitCode}.`);
80-
error.code = exitCode;
81-
reject(error);
82-
}
74+
childProcess.on("error", (err) => {
75+
this.$logger.trace(`Unable to start webpack process in watch mode. Error is: ${err}`);
76+
reject(err);
77+
});
8378

79+
childProcess.on("close", async (arg: any) => {
8480
await this.$cleanupService.removeKillProcess(childProcess.pid.toString());
81+
82+
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
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);
8587
});
8688
});
8789
}
@@ -94,7 +96,14 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
9496
}
9597

9698
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
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+
97104
childProcess.on("close", async (arg: any) => {
105+
await this.$cleanupService.removeKillProcess(childProcess.pid.toString());
106+
98107
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
99108
if (exitCode === 0) {
100109
resolve();
@@ -103,8 +112,6 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
103112
error.code = exitCode;
104113
reject(error);
105114
}
106-
107-
await this.$cleanupService.removeKillProcess(childProcess.pid.toString());
108115
});
109116
});
110117
}
@@ -145,7 +152,7 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
145152
}
146153

147154
const stdio = prepareData.watch ? ["inherit", "inherit", "inherit", "ipc"] : "inherit";
148-
const childProcess = this.$childProcess.spawn("node", args, { cwd: projectData.projectDir, stdio });
155+
const childProcess = this.$childProcess.spawn(process.execPath, args, { cwd: projectData.projectDir, stdio });
149156

150157
this.webpackProcesses[platformData.platformNameLowerCase] = childProcess;
151158
await this.$cleanupService.addKillProcess(childProcess.pid.toString());

0 commit comments

Comments
 (0)