fix(nuxt): resolve race condition regenerating auto imports (#22201)

This commit is contained in:
Anthony Fu 2023-07-18 17:20:06 +02:00 committed by GitHub
parent f4ec04f520
commit f6b64f6a65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,9 +87,10 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length) const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length)
const regenerateImports = async () => { const regenerateImports = async () => {
ctx.clearDynamicImports()
await ctx.modifyDynamicImports(async (imports) => { await ctx.modifyDynamicImports(async (imports) => {
// Scan composables/ // Clear old imports
imports.length = 0
// Scan `composables/`
const composableImports = await scanDirExports(composablesDirs) const composableImports = await scanDirExports(composablesDirs)
for (const i of composableImports) { for (const i of composableImports) {
i.priority = i.priority || priorities.find(([dir]) => i.from.startsWith(dir))?.[1] i.priority = i.priority || priorities.find(([dir]) => i.from.startsWith(dir))?.[1]
@ -97,6 +98,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
imports.push(...composableImports) imports.push(...composableImports)
// Modules extending // Modules extending
await nuxt.callHook('imports:extend', imports) await nuxt.callHook('imports:extend', imports)
return imports
}) })
} }