From 9d0d4e207dc7f19bf629db4237d55233909b81f4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 18 Mar 2023 13:39:06 +0000 Subject: [PATCH] fix(nuxi): watch dist and register restart hook after nuxt is ready (#19736) --- packages/nuxi/src/commands/dev.ts | 35 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 0bd2bc2dbf..caabd72ae5 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -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) { 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 if (!isRestart) { const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir) @@ -133,6 +116,24 @@ export default defineNuxtCommand({ 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) const address = (listener.server.address() || {}) as AddressInfo currentNuxt.options.devServer.url = listener.url