Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): auto imports regeneration timing #26249

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions packages/nuxt/src/imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnor
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports, toExports } from 'unimport'
import type { ImportPresetWithDeprecation, ImportsOptions } from 'nuxt/schema'
import type { ImportPresetWithDeprecation, ImportsOptions, ResolvedNuxtTemplate } from 'nuxt/schema'

import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
import { isDirectory } from '../utils'
Expand Down Expand Up @@ -89,6 +89,14 @@ 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)

function isImportsTemplate (template: ResolvedNuxtTemplate) {
return [
'/types/imports.d.ts',
'/imports.d.ts',
'/imports.mjs'
].some(i => template.filename.endsWith(i))
}

const regenerateImports = async () => {
await ctx.modifyDynamicImports(async (imports) => {
// Clear old imports
Expand All @@ -105,6 +113,10 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
await nuxt.callHook('imports:extend', imports)
return imports
})

await updateTemplates({
filter: isImportsTemplate
})
}

await regenerateImports()
Expand All @@ -119,22 +131,19 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
})

// Watch composables/ directory
const templates = [
'types/imports.d.ts',
'imports.d.ts',
'imports.mjs'
]
nuxt.hook('builder:watch', async (_, relativePath) => {
const path = resolve(nuxt.options.srcDir, relativePath)
if (composablesDirs.some(dir => dir === path || path.startsWith(dir + '/'))) {
await updateTemplates({
filter: template => templates.includes(template.filename)
})
await regenerateImports()
}
})

nuxt.hook('app:templatesGenerated', async () => {
await regenerateImports()
Copy link
Member Author

@antfu antfu Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we did regeneration (rescan) after all templates to avoid race conditions in #21934. But since unimport also use templates to generate those dts etc, so it means we missed the chance to update them - until this get triggered again - we are always one "tick" behind.

This PR makes sure regenerateImports always update (only) the unimport templates. Thus we also need some mechanism to dedupe and avoid circular update

// Watch for template generation
nuxt.hook('app:templatesGenerated', async (_app, templates) => {
// Only regenerate when non-imports templates are updated
if (templates.some(t => !isImportsTemplate(t))) {
await regenerateImports()
}
})
}
})
Expand Down