Skip to content

Commit

Permalink
refactor: comment restarter setup
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Aug 21, 2024
1 parent 60b224b commit 4135595
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions packages/vike-node/src/plugin/plugins/devServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { logViteInfo } from '../utils/logVite.js'

let viteDevServer: ViteDevServer
const VITE_HMR_PATH = '/__vite_hmr'
const RESTART_EXIT_CODE = 33
const IS_RESTARTER_SETUP = '__VIKE__IS_RESTARTER_SETUP'

export function devServerPlugin(): Plugin {
let resolvedConfig: ConfigVikeNodeResolved
Expand All @@ -21,7 +23,7 @@ export function devServerPlugin(): Plugin {
apply: 'serve',
enforce: 'pre',
config: async () => {
await setupReloader()
await setupProcessRestarter()

if (isBun) {
return {
Expand Down Expand Up @@ -148,26 +150,37 @@ function setupErrorHandler(vite: ViteDevServer) {
process.on('uncaughtException', onError)
}

async function setupReloader() {
const isReloaderSetup = process.env.VIKE_NODE_RELOADER_SETUP === 'true'
if (!isReloaderSetup) {
process.env.VIKE_NODE_RELOADER_SETUP = 'true'
function start() {
const cp = fork(process.argv[1]!, process.argv.slice(2), { stdio: 'inherit' })
cp.on('exit', (code) => {
if (code === 33) {
start()
} else {
process.exit(code)
}
})
}
start()
await new Promise(() => {})
// The CLI root process is blocked and, instead, it orchestrates server restarts.
// The same CLI is called as a child process which does the actual work.
async function setupProcessRestarter() {
if (isRestarterSetup()) return
process.env[IS_RESTARTER_SETUP] = 'true'

function start() {
const cliEntry = process.argv[1]!
const cliArgs = process.argv.slice(2)
// Re-run the exact same CLI
const clone = fork(cliEntry, cliArgs, { stdio: 'inherit' })
clone.on('exit', (code) => {
if (code === RESTART_EXIT_CODE) {
start()
} else {
process.exit(code)
}
})
}
start()

// Trick: never resolve promise in order to block the CLI root process
await new Promise(() => {})
}

function isRestarterSetup() {
return process.env[IS_RESTARTER_SETUP] === 'true'
}

function restartProcess() {
logViteInfo('Restarting server...')
process.exit(33)
assert(isRestarterSetup())
process.exit(RESTART_EXIT_CODE)
}

0 comments on commit 4135595

Please sign in to comment.