Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(bridge,nitro,nuxi,nuxt3,vite): ensure debounced/async handlers run in order #3656

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/bridge/src/vite/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/server/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand All @@ -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`)
}

Expand Down
13 changes: 8 additions & 5 deletions packages/nuxt3/src/core/builder.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down