mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
fix(nuxt): update auto imports after other templates (#26249)
This commit is contained in:
parent
177980865a
commit
5dc96de8a9
@ -3,7 +3,7 @@ import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnor
|
|||||||
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
|
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
|
||||||
import type { Import, Unimport } from 'unimport'
|
import type { Import, Unimport } from 'unimport'
|
||||||
import { createUnimport, scanDirExports, toExports } 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 { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
|
||||||
import { isDirectory } from '../utils'
|
import { isDirectory } from '../utils'
|
||||||
@ -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)
|
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 () => {
|
const regenerateImports = async () => {
|
||||||
await ctx.modifyDynamicImports(async (imports) => {
|
await ctx.modifyDynamicImports(async (imports) => {
|
||||||
// Clear old imports
|
// Clear old imports
|
||||||
@ -105,6 +113,10 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
await nuxt.callHook('imports:extend', imports)
|
await nuxt.callHook('imports:extend', imports)
|
||||||
return imports
|
return imports
|
||||||
})
|
})
|
||||||
|
|
||||||
|
await updateTemplates({
|
||||||
|
filter: isImportsTemplate
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
await regenerateImports()
|
await regenerateImports()
|
||||||
@ -119,22 +131,19 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Watch composables/ directory
|
// Watch composables/ directory
|
||||||
const templates = [
|
|
||||||
'types/imports.d.ts',
|
|
||||||
'imports.d.ts',
|
|
||||||
'imports.mjs'
|
|
||||||
]
|
|
||||||
nuxt.hook('builder:watch', async (_, relativePath) => {
|
nuxt.hook('builder:watch', async (_, relativePath) => {
|
||||||
const path = resolve(nuxt.options.srcDir, relativePath)
|
const path = resolve(nuxt.options.srcDir, relativePath)
|
||||||
if (composablesDirs.some(dir => dir === path || path.startsWith(dir + '/'))) {
|
if (composablesDirs.some(dir => dir === path || path.startsWith(dir + '/'))) {
|
||||||
await updateTemplates({
|
await regenerateImports()
|
||||||
filter: template => templates.includes(template.filename)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
nuxt.hook('app:templatesGenerated', async () => {
|
// Watch for template generation
|
||||||
await regenerateImports()
|
nuxt.hook('app:templatesGenerated', async (_app, templates) => {
|
||||||
|
// Only regenerate when non-imports templates are updated
|
||||||
|
if (templates.some(t => !isImportsTemplate(t))) {
|
||||||
|
await regenerateImports()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user