mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 01:15:58 +00:00
fix(nuxt3): trigger template regeneration (and reset import list) when auto-imports update (#3127)
This commit is contained in:
parent
c02e3683eb
commit
ba522b2034
@ -31,17 +31,6 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
// Create a context to share state between module internals
|
// Create a context to share state between module internals
|
||||||
const ctx = createAutoImportContext(options)
|
const ctx = createAutoImportContext(options)
|
||||||
|
|
||||||
// Resolve autoimports from sources
|
|
||||||
for (const source of options.sources) {
|
|
||||||
for (const importName of source.names) {
|
|
||||||
if (typeof importName === 'string') {
|
|
||||||
ctx.autoImports.push({ name: importName, as: importName, from: source.from })
|
|
||||||
} else {
|
|
||||||
ctx.autoImports.push({ name: importName.name, as: importName.as || importName.name, from: source.from })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// composables/ dirs
|
// composables/ dirs
|
||||||
let composablesDirs = [
|
let composablesDirs = [
|
||||||
join(nuxt.options.srcDir, 'composables'),
|
join(nuxt.options.srcDir, 'composables'),
|
||||||
@ -76,7 +65,13 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
addWebpackPlugin(TransformPlugin.webpack(ctx))
|
addWebpackPlugin(TransformPlugin.webpack(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateAutoImports = async () => {
|
const regenerateAutoImports = async () => {
|
||||||
|
// Resolve autoimports from sources
|
||||||
|
ctx.autoImports = options.sources.flatMap(source => source.names.map(
|
||||||
|
importName => typeof importName === 'string'
|
||||||
|
? { name: importName, as: importName, from: source.from }
|
||||||
|
: { name: importName.name, as: importName.as || importName.name, from: source.from }
|
||||||
|
))
|
||||||
// Scan composables/
|
// Scan composables/
|
||||||
for (const composablesDir of composablesDirs) {
|
for (const composablesDir of composablesDirs) {
|
||||||
await scanForComposables(composablesDir, ctx.autoImports)
|
await scanForComposables(composablesDir, ctx.autoImports)
|
||||||
@ -85,10 +80,12 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
await nuxt.callHook('autoImports:extend', ctx.autoImports)
|
await nuxt.callHook('autoImports:extend', ctx.autoImports)
|
||||||
// Update context
|
// Update context
|
||||||
updateAutoImportContext(ctx)
|
updateAutoImportContext(ctx)
|
||||||
// Generate types
|
|
||||||
generateDts(ctx)
|
|
||||||
}
|
}
|
||||||
await updateAutoImports()
|
|
||||||
|
await regenerateAutoImports()
|
||||||
|
|
||||||
|
// Generate types
|
||||||
|
addDeclarationTemplates(ctx)
|
||||||
|
|
||||||
// Add generated types to `nuxt.d.ts`
|
// Add generated types to `nuxt.d.ts`
|
||||||
nuxt.hook('prepare:types', ({ references }) => {
|
nuxt.hook('prepare:types', ({ references }) => {
|
||||||
@ -100,13 +97,14 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
nuxt.hook('builder:watch', async (_, path) => {
|
nuxt.hook('builder:watch', async (_, path) => {
|
||||||
const _resolved = resolve(nuxt.options.srcDir, path)
|
const _resolved = resolve(nuxt.options.srcDir, path)
|
||||||
if (composablesDirs.find(dir => _resolved.startsWith(dir))) {
|
if (composablesDirs.find(dir => _resolved.startsWith(dir))) {
|
||||||
await updateAutoImports()
|
await regenerateAutoImports()
|
||||||
|
await nuxt.callHook('builder:generateApp')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function generateDts (ctx: AutoImportContext) {
|
function addDeclarationTemplates (ctx: AutoImportContext) {
|
||||||
const nuxt = useNuxt()
|
const nuxt = useNuxt()
|
||||||
|
|
||||||
const resolved = {}
|
const resolved = {}
|
||||||
@ -124,13 +122,11 @@ function generateDts (ctx: AutoImportContext) {
|
|||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
filename: 'types/imports.d.ts',
|
filename: 'types/imports.d.ts',
|
||||||
write: true,
|
|
||||||
getContents: () => toExports(ctx.autoImports)
|
getContents: () => toExports(ctx.autoImports)
|
||||||
})
|
})
|
||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
filename: 'types/auto-imports.d.ts',
|
filename: 'types/auto-imports.d.ts',
|
||||||
write: true,
|
|
||||||
getContents: () => `// Generated by auto imports
|
getContents: () => `// Generated by auto imports
|
||||||
declare global {
|
declare global {
|
||||||
${ctx.autoImports.map(i => ` const ${i.as}: typeof ${genDynamicImport(r(i.from), { wrapper: false })}['${i.name}']`).join('\n')}
|
${ctx.autoImports.map(i => ` const ${i.as}: typeof ${genDynamicImport(r(i.from), { wrapper: false })}['${i.name}']`).join('\n')}
|
||||||
|
Loading…
Reference in New Issue
Block a user