mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
feat!(nuxt3): extend auto imports on generateApp
hook (#3480)
This commit is contained in:
parent
33ffd8be0a
commit
120ee4f795
@ -6,10 +6,9 @@ import { AutoImport } from '@nuxt/schema'
|
||||
import { resolveFiles } from '@nuxt/kit'
|
||||
import { filterInPlace } from './utils'
|
||||
|
||||
export async function scanForComposables (dir: string, autoImports: AutoImport[]) {
|
||||
if (!existsSync(dir)) { return }
|
||||
|
||||
const files = await resolveFiles(dir, [
|
||||
export async function scanForComposables (dir: string | string[], autoImports: AutoImport[]) {
|
||||
const performScan = async (entry: string) => {
|
||||
const files = await resolveFiles(entry, [
|
||||
'*.{ts,js,mjs,cjs,mts,cts}',
|
||||
'*/index.{ts,js,mjs,cjs,mts,cts}'
|
||||
])
|
||||
@ -41,4 +40,11 @@ export async function scanForComposables (dir: string, autoImports: AutoImport[]
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
for (const entry of Array.isArray(dir) ? dir : [dir]) {
|
||||
if (!existsSync(entry)) { continue }
|
||||
|
||||
await performScan(entry)
|
||||
}
|
||||
}
|
||||
|
@ -72,9 +72,7 @@ export default defineNuxtModule<AutoImportsOptions>({
|
||||
: { name: importName.name, as: importName.as || importName.name, from: source.from }
|
||||
))
|
||||
// Scan composables/
|
||||
for (const composablesDir of composablesDirs) {
|
||||
await scanForComposables(composablesDir, ctx.autoImports)
|
||||
}
|
||||
await scanForComposables(composablesDirs, ctx.autoImports)
|
||||
// Allow modules extending
|
||||
await nuxt.callHook('autoImports:extend', ctx.autoImports)
|
||||
// Update context
|
||||
@ -96,10 +94,13 @@ export default defineNuxtModule<AutoImportsOptions>({
|
||||
nuxt.hook('builder:watch', async (_, path) => {
|
||||
const _resolved = resolve(nuxt.options.srcDir, path)
|
||||
if (composablesDirs.find(dir => _resolved.startsWith(dir))) {
|
||||
await regenerateAutoImports()
|
||||
await nuxt.callHook('builder:generateApp')
|
||||
}
|
||||
})
|
||||
|
||||
nuxt.hook('builder:generateApp', async () => {
|
||||
await regenerateAutoImports()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user