fix: ensure debounced/async handlers run in order (#3656)

This commit is contained in:
Daniel Roe 2022-03-15 10:56:16 +00:00 committed by GitHub
parent a7ce6d53b7
commit 14b32258e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 16 deletions

View File

@ -118,7 +118,7 @@ export async function buildServer (ctx: ViteBuildContext) {
consola.info(`Server built in ${time}ms`) consola.info(`Server built in ${time}ms`)
await onBuild() await onBuild()
} }
const doBuild = pDebounce(_doBuild, 300) const doBuild = pDebounce(pDebounce.promise(_doBuild), 300)
// Initial build // Initial build
await _doBuild() await _doBuild()

View File

@ -128,7 +128,7 @@ export function createDevServer (nitroContext: NitroContext) {
let watcher: FSWatcher let watcher: FSWatcher
function watch () { function watch () {
if (watcher) { return } 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([ watcher = chokidar.watch([
resolve(nitroContext.output.serverDir, pattern), resolve(nitroContext.output.serverDir, pattern),
resolve(nitroContext._nuxt.buildDir, 'dist/server', pattern) resolve(nitroContext._nuxt.buildDir, 'dist/server', pattern)

View File

@ -3,6 +3,7 @@ import chokidar from 'chokidar'
import debounce from 'p-debounce' import debounce from 'p-debounce'
import type { Nuxt } from '@nuxt/schema' import type { Nuxt } from '@nuxt/schema'
import consola from 'consola' import consola from 'consola'
import { withTrailingSlash } from 'ufo'
import { createServer, createLoadingHandler } from '../utils/server' import { createServer, createLoadingHandler } from '../utils/server'
import { showBanner } from '../utils/banner' import { showBanner } from '../utils/banner'
import { writeTypes } from '../utils/prepare' import { writeTypes } from '../utils/prepare'
@ -46,12 +47,13 @@ export default defineNuxtCommand({
if (currentNuxt) { if (currentNuxt) {
await currentNuxt.close() await currentNuxt.close()
} }
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false }) currentNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
await clearDir(newNuxt.options.buildDir) await clearDir(currentNuxt.options.buildDir)
currentNuxt = newNuxt
await currentNuxt.ready() await currentNuxt.ready()
writeTypes(currentNuxt).catch(console.error) await Promise.all([
await buildNuxt(currentNuxt) writeTypes(currentNuxt).catch(console.error),
buildNuxt(currentNuxt)
])
server.setApp(currentNuxt.server.app) server.setApp(currentNuxt.server.app)
if (isRestart && args.clear !== false) { if (isRestart && args.clear !== false) {
showBanner() showBanner()
@ -67,12 +69,12 @@ export default defineNuxtCommand({
// Watch for config changes // Watch for config changes
// TODO: Watcher service, modules, and requireTree // 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 }) const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
watcher.on('all', (event, file) => { watcher.on('all', (event, file) => {
if (!currentNuxt) { return } if (!currentNuxt) { return }
if (file.startsWith(currentNuxt.options.buildDir)) { return } if (file.startsWith(withTrailingSlash(currentNuxt.options.buildDir))) { return }
if (file.match(/(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore)$/)) { if (file.match(/(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.env|\.nuxtrc)$/)) {
dLoad(true, `${relative(rootDir, file)} updated`) dLoad(true, `${relative(rootDir, file)} updated`)
} }

View File

@ -1,11 +1,13 @@
import chokidar from 'chokidar' import chokidar from 'chokidar'
import type { Nuxt } from '@nuxt/schema' import type { Nuxt } from '@nuxt/schema'
import { isIgnored, tryImportModule } from '@nuxt/kit' 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) { export async function build (nuxt: Nuxt) {
const app = createApp(nuxt) const app = createApp(nuxt)
await generateApp(nuxt, app) const generateApp = debounce(debounce.promise(() => _generateApp(nuxt, app)), 1)
await generateApp()
if (nuxt.options.dev) { if (nuxt.options.dev) {
watch(nuxt) watch(nuxt)
@ -17,10 +19,10 @@ export async function build (nuxt: Nuxt) {
if (path.match(/error/i)) { if (path.match(/error/i)) {
app.errorComponent = null 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) await nuxt.callHook('build:before', { nuxt }, nuxt.options.build)
@ -45,7 +47,8 @@ function watch (nuxt: Nuxt) {
'node_modules' '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) watcher.on('all', watchHook)
nuxt.hook('close', () => watcher.close()) nuxt.hook('close', () => watcher.close())
return watcher return watcher

View File

@ -154,7 +154,7 @@ export async function buildServer (ctx: ViteBuildContext) {
logger.success(`Vite server built in ${time}ms`) logger.success(`Vite server built in ${time}ms`)
await onBuild() await onBuild()
} }
const doBuild = pDebounce(_doBuild, 100) const doBuild = pDebounce(pDebounce.promise(_doBuild), 100)
// Initial build // Initial build
await _doBuild() await _doBuild()