diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index f149fac30d53d5..2ff3984515a292 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -168,10 +168,11 @@ export async function preview( }, } - const closeServerAndExit = async () => { + const closeServerAndExit = async (_: unknown, exitCode?: number) => { try { await server.close() } finally { + process.exitCode ??= exitCode ? 128 + exitCode : undefined process.exit() } } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index c7b4df4922a7e7..f8fb4386d89dfc 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -729,10 +729,11 @@ export async function _createServer( }, }) - const closeServerAndExit = async () => { + const closeServerAndExit = async (_: unknown, exitCode?: number) => { try { await server.close() } finally { + process.exitCode ??= exitCode ? 128 + exitCode : undefined process.exit() } } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 0a9d1e37e3f31b..794a333276782f 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1524,7 +1524,9 @@ export function partialEncodeURIPath(uri: string): string { return filePath.replaceAll('%', '%25') + postfix } -export const setupSIGTERMListener = (callback: () => Promise): void => { +export const setupSIGTERMListener = ( + callback: (signal?: 'SIGTERM', exitCode?: number) => Promise, +): void => { process.once('SIGTERM', callback) if (process.env.CI !== 'true') { process.stdin.on('end', callback) @@ -1532,7 +1534,7 @@ export const setupSIGTERMListener = (callback: () => Promise): void => { } export const teardownSIGTERMListener = ( - callback: () => Promise, + callback: Parameters[0], ): void => { process.off('SIGTERM', callback) if (process.env.CI !== 'true') {