Skip to content
Merged
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
6 changes: 3 additions & 3 deletions packages/vite/src/node/__tests__/plugins/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const createPreviewServerWithPlugin = async (plugin: Plugin) => {
name: 'mock-preview',
configurePreviewServer({ httpServer }) {
// NOTE: make httpServer.listen no-op to avoid starting a server
httpServer.listen = (...args: unknown[]) => {
const listener = args.at(-1) as () => void
listener()
httpServer.listen = () => {
const lastListener = httpServer.listeners('listening').at(-1)!
lastListener.call(httpServer)
return httpServer as any
}
},
Expand Down
16 changes: 11 additions & 5 deletions packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,20 @@ async function tryBindServer(
> {
return new Promise((resolve) => {
const onError = (e: Error & { code?: string }) => {
httpServer.removeListener('error', onError)
httpServer.off('error', onError)
httpServer.off('listening', onListening)
resolve({ success: false, error: e })
}
httpServer.on('error', onError)
httpServer.listen(port, host, () => {
httpServer.removeListener('error', onError)
const onListening = () => {
httpServer.off('error', onError)
httpServer.off('listening', onListening)
resolve({ success: true })
})
}

httpServer.on('error', onError)
httpServer.on('listening', onListening)

httpServer.listen(port, host)
})
}

Expand Down
Loading