fix(nuxi): watch dist and register restart hook after nuxt is ready (#19736)

This commit is contained in:
Daniel Roe 2023-03-18 13:39:06 +00:00 committed by GitHub
parent 2c642328c9
commit 9d0d4e207d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -101,27 +101,10 @@ export default defineNuxtCommand({
} }
}) })
currentNuxt.hooks.hookOnce('restart', async (options) => {
if (options?.hard && process.send) {
await listener.close().catch(() => {})
await currentNuxt.close().catch(() => {})
await watcher.close().catch(() => {})
await distWatcher.close().catch(() => {})
process.send({ type: 'nuxt:restart' })
} else {
await load(true)
}
})
if (!isRestart) { if (!isRestart) {
showURL() showURL()
} }
distWatcher = chokidar.watch(resolve(currentNuxt.options.buildDir, 'dist'), { ignoreInitial: true, depth: 0 })
distWatcher.on('unlinkDir', () => {
dLoad(true, '.nuxt/dist directory has been removed')
})
// Write manifest and also check if we need cache invalidation // Write manifest and also check if we need cache invalidation
if (!isRestart) { if (!isRestart) {
const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir) const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir)
@ -133,6 +116,24 @@ export default defineNuxtCommand({
await currentNuxt.ready() await currentNuxt.ready()
distWatcher = chokidar.watch(resolve(currentNuxt.options.buildDir, 'dist'), { ignoreInitial: true, depth: 0 })
distWatcher.on('unlinkDir', () => {
dLoad(true, '.nuxt/dist directory has been removed')
})
const unsub = currentNuxt.hooks.hook('restart', async (options) => {
unsub() // we use this instead of `hookOnce` for Nuxt Bridge support
if (options?.hard && process.send) {
await listener.close().catch(() => {})
await currentNuxt.close().catch(() => {})
await watcher.close().catch(() => {})
await distWatcher.close().catch(() => {})
process.send({ type: 'nuxt:restart' })
} else {
await load(true)
}
})
await currentNuxt.hooks.callHook('listen', listener.server, listener) await currentNuxt.hooks.callHook('listen', listener.server, listener)
const address = (listener.server.address() || {}) as AddressInfo const address = (listener.server.address() || {}) as AddressInfo
currentNuxt.options.devServer.url = listener.url currentNuxt.options.devServer.url = listener.url