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`)
await onBuild()
}
const doBuild = pDebounce(_doBuild, 300)
const doBuild = pDebounce(pDebounce.promise(_doBuild), 300)
// Initial build
await _doBuild()

View File

@ -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)

View File

@ -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`)
}

View File

@ -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

View File

@ -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()