From 14b32258e8bae9722a905efdaa5306d5f8ef4c7d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 15 Mar 2022 10:56:16 +0000 Subject: [PATCH] fix: ensure debounced/async handlers run in order (#3656) --- packages/bridge/src/vite/server.ts | 2 +- packages/nitro/src/server/dev.ts | 2 +- packages/nuxi/src/commands/dev.ts | 18 ++++++++++-------- packages/nuxt3/src/core/builder.ts | 13 ++++++++----- packages/vite/src/server.ts | 2 +- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/bridge/src/vite/server.ts b/packages/bridge/src/vite/server.ts index 5bfccb4ca7..5b72e49c62 100644 --- a/packages/bridge/src/vite/server.ts +++ b/packages/bridge/src/vite/server.ts @@ -118,7 +118,7 @@ export async function buildServer (ctx: ViteBuildContext) { consola.info(`Server built in ${time}ms`) await onBuild() } - const doBuild = pDebounce(_doBuild, 300) + const doBuild = pDebounce(pDebounce.promise(_doBuild), 300) // Initial build await _doBuild() diff --git a/packages/nitro/src/server/dev.ts b/packages/nitro/src/server/dev.ts index fa54419587..e355e56dbe 100644 --- a/packages/nitro/src/server/dev.ts +++ b/packages/nitro/src/server/dev.ts @@ -128,7 +128,7 @@ export function createDevServer (nitroContext: NitroContext) { let watcher: FSWatcher function watch () { if (watcher) { return } - const dReload = debounce(() => reload().catch(console.warn), 200, { before: true }) + const dReload = debounce(debounce.promise(() => reload().catch(console.warn)), 200, { before: true }) watcher = chokidar.watch([ resolve(nitroContext.output.serverDir, pattern), resolve(nitroContext._nuxt.buildDir, 'dist/server', pattern) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index faf79451fb..a529ff9ec8 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -3,6 +3,7 @@ import chokidar from 'chokidar' import debounce from 'p-debounce' import type { Nuxt } from '@nuxt/schema' import consola from 'consola' +import { withTrailingSlash } from 'ufo' import { createServer, createLoadingHandler } from '../utils/server' import { showBanner } from '../utils/banner' import { writeTypes } from '../utils/prepare' @@ -46,12 +47,13 @@ export default defineNuxtCommand({ if (currentNuxt) { await currentNuxt.close() } - const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false }) - await clearDir(newNuxt.options.buildDir) - currentNuxt = newNuxt + currentNuxt = await loadNuxt({ rootDir, dev: true, ready: false }) + await clearDir(currentNuxt.options.buildDir) await currentNuxt.ready() - writeTypes(currentNuxt).catch(console.error) - await buildNuxt(currentNuxt) + await Promise.all([ + writeTypes(currentNuxt).catch(console.error), + buildNuxt(currentNuxt) + ]) server.setApp(currentNuxt.server.app) if (isRestart && args.clear !== false) { showBanner() @@ -67,12 +69,12 @@ export default defineNuxtCommand({ // Watch for config changes // TODO: Watcher service, modules, and requireTree - const dLoad = debounce(load, 250) + const dLoad = debounce(debounce.promise(load), 250) const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 }) watcher.on('all', (event, file) => { if (!currentNuxt) { return } - if (file.startsWith(currentNuxt.options.buildDir)) { return } - if (file.match(/(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore)$/)) { + if (file.startsWith(withTrailingSlash(currentNuxt.options.buildDir))) { return } + if (file.match(/(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.env|\.nuxtrc)$/)) { dLoad(true, `${relative(rootDir, file)} updated`) } diff --git a/packages/nuxt3/src/core/builder.ts b/packages/nuxt3/src/core/builder.ts index d196af81cf..a1274d68fd 100644 --- a/packages/nuxt3/src/core/builder.ts +++ b/packages/nuxt3/src/core/builder.ts @@ -1,11 +1,13 @@ import chokidar from 'chokidar' import type { Nuxt } from '@nuxt/schema' import { isIgnored, tryImportModule } from '@nuxt/kit' -import { createApp, generateApp } from './app' +import debounce from 'p-debounce' +import { createApp, generateApp as _generateApp } from './app' export async function build (nuxt: Nuxt) { const app = createApp(nuxt) - await generateApp(nuxt, app) + const generateApp = debounce(debounce.promise(() => _generateApp(nuxt, app)), 1) + await generateApp() if (nuxt.options.dev) { watch(nuxt) @@ -17,10 +19,10 @@ export async function build (nuxt: Nuxt) { if (path.match(/error/i)) { app.errorComponent = null } - await generateApp(nuxt, app) + await generateApp() } }) - nuxt.hook('builder:generateApp', () => generateApp(nuxt, app)) + nuxt.hook('builder:generateApp', generateApp) } await nuxt.callHook('build:before', { nuxt }, nuxt.options.build) @@ -45,7 +47,8 @@ function watch (nuxt: Nuxt) { 'node_modules' ] }) - const watchHook = (event, path) => nuxt.callHook('builder:watch', event, path) + + const watchHook = debounce(debounce.promise((event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', path: string) => nuxt.callHook('builder:watch', event, path)), 1) watcher.on('all', watchHook) nuxt.hook('close', () => watcher.close()) return watcher diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index 149e96e7db..9f7b32c267 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -154,7 +154,7 @@ export async function buildServer (ctx: ViteBuildContext) { logger.success(`Vite server built in ${time}ms`) await onBuild() } - const doBuild = pDebounce(_doBuild, 100) + const doBuild = pDebounce(pDebounce.promise(_doBuild), 100) // Initial build await _doBuild()