Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add --watchStdin option to exit when stdin ends #19092

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ cli
'--force',
`[boolean] force the optimizer to ignore the cache and re-bundle`,
)
.option('--watchStdin', `[boolean] watch stdin and exit on EOF`, {
default: !process.stdin.isTTY,
})
.action(async (root: string, options: ServerOptions & GlobalCLIOptions) => {
filterDuplicateOptions(options)
// output structure is preserved even after bundling so require()
Expand Down Expand Up @@ -364,6 +367,9 @@ cli
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--outDir <dir>', `[string] output directory (default: dist)`)
.option('--watchStdin', `[boolean] watch stdin and exit on EOF`, {
default: !process.stdin.isTTY,
})
.action(
async (
root: string,
Expand All @@ -373,6 +379,7 @@ cli
open?: boolean | string
strictPort?: boolean
outDir?: string
watchStdin?: boolean
} & GlobalCLIOptions,
) => {
filterDuplicateOptions(options)
Expand All @@ -392,6 +399,7 @@ cli
strictPort: options.strictPort,
host: options.host,
open: options.open,
watchStdin: options.watchStdin,
},
})
server.printUrls()
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface CommonServerOptions {
* Specify server response headers.
*/
headers?: HttpServerHeaders
/**
* Watch stdin and exit on EOF
*/
watchStdin?: boolean
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function resolvePreviewOptions(
proxy: preview?.proxy ?? server.proxy,
cors: preview?.cors ?? server.cors,
headers: preview?.headers ?? server.headers,
watchStdin: preview?.watchStdin ?? server.watchStdin,
}
}

Expand Down Expand Up @@ -153,7 +154,7 @@ export async function preview(
middlewares: app,
httpServer,
async close() {
teardownSIGTERMListener(closeServerAndExit)
teardownSIGTERMListener(config.preview.watchStdin, closeServerAndExit)
await closeHttpServer()
server.resolvedUrls = null
},
Expand All @@ -179,7 +180,7 @@ export async function preview(
}
}

setupSIGTERMListener(closeServerAndExit)
setupSIGTERMListener(config.preview.watchStdin, closeServerAndExit)

// apply server hooks from plugins
const postHooks: ((() => void) | void)[] = []
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ export async function _createServer(
},
async close() {
if (!middlewareMode) {
teardownSIGTERMListener(closeServerAndExit)
teardownSIGTERMListener(config.server.watchStdin, closeServerAndExit)
}

await Promise.allSettled([
Expand Down Expand Up @@ -757,7 +757,7 @@ export async function _createServer(
}

if (!middlewareMode) {
setupSIGTERMListener(closeServerAndExit)
setupSIGTERMListener(config.server.watchStdin, closeServerAndExit)
}

const onHMRUpdate = async (
Expand Down Expand Up @@ -1036,6 +1036,7 @@ export const serverConfigDefaults = Object.freeze({
host: 'localhost',
https: undefined,
open: false,
watchStdin: false,
proxy: undefined,
cors: true,
headers: {},
Expand Down
10 changes: 7 additions & 3 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,19 +1530,23 @@ export function partialEncodeURIPath(uri: string): string {
}

export const setupSIGTERMListener = (
watchStdin: boolean,
callback: (signal?: 'SIGTERM', exitCode?: number) => Promise<void>,
): void => {
process.once('SIGTERM', callback)
if (process.env.CI !== 'true') {
if (watchStdin) {
process.stdin.on('end', callback)
// resume stdin to allow the server to exit on EOF
process.stdin.resume()
}
}

export const teardownSIGTERMListener = (
callback: Parameters<typeof setupSIGTERMListener>[0],
watchStdin: boolean,
callback: Parameters<typeof setupSIGTERMListener>[1],
): void => {
process.off('SIGTERM', callback)
if (process.env.CI !== 'true') {
if (watchStdin) {
process.stdin.off('end', callback)
}
}
Loading