Skip to content

Commit

Permalink
fix: print log only when httpServer is exist
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 25, 2021
1 parent c588a42 commit 57e39b5
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,20 @@ function PluginInspect(options: Options = {}): Plugin {
}
})

// hijack httpServer.listen to print the log
const _listen = server.httpServer!.listen
let port = config.server.port || 3000
let timer: any
server.httpServer!.listen = function(this: any, ...args: any) {
port ||= args[0]
clearTimeout(timer)
timer = setTimeout(() => {
if (server.httpServer?.listen) {
// hijack httpServer.listen to print the log
const _listen = server.httpServer.listen
let port = config.server.port || 3000
let timer: any
server.httpServer.listen = function(this: any, ...args: any) {
port ||= args[0]
clearTimeout(timer)
timer = setTimeout(() => {
// eslint-disable-next-line no-console
console.log(` > Inspect: ${yellow(`http://localhost:${port}/__inspect/`)}\n`)
}, 0)
return _listen.apply(this, args)
console.log(` > Inspect: ${yellow(`http://localhost:${port}/__inspect/`)}\n`)
}, 0)
return _listen.apply(this, args)
}
}
}

Expand Down

0 comments on commit 57e39b5

Please sign in to comment.