feat!(nuxt3): extend auto imports on generateApp hook (#3480)

This commit is contained in:
Dizzy 2022-03-07 06:39:54 -04:00 committed by Pooya Parsa
parent 33ffd8be0a
commit 120ee4f795
2 changed files with 41 additions and 34 deletions

View File

@ -6,39 +6,45 @@ import { AutoImport } from '@nuxt/schema'
import { resolveFiles } from '@nuxt/kit' import { resolveFiles } from '@nuxt/kit'
import { filterInPlace } from './utils' import { filterInPlace } from './utils'
export async function scanForComposables (dir: string, autoImports: AutoImport[]) { export async function scanForComposables (dir: string | string[], autoImports: AutoImport[]) {
if (!existsSync(dir)) { return } const performScan = async (entry: string) => {
const files = await resolveFiles(entry, [
'*.{ts,js,mjs,cjs,mts,cts}',
'*/index.{ts,js,mjs,cjs,mts,cts}'
])
const files = await resolveFiles(dir, [ await Promise.all(
'*.{ts,js,mjs,cjs,mts,cts}', files.map(async (path) => {
'*/index.{ts,js,mjs,cjs,mts,cts}' // Remove original entries from the same import (for build watcher)
]) filterInPlace(autoImports, i => i.from !== path)
await Promise.all( const code = await fsp.readFile(path, 'utf-8')
files.map(async (path) => { const exports = findExports(code)
// Remove original entries from the same import (for build watcher) const defaultExport = exports.find(i => i.type === 'default')
filterInPlace(autoImports, i => i.from !== path)
const code = await fsp.readFile(path, 'utf-8') if (defaultExport) {
const exports = findExports(code) let name = parsePath(path).name
const defaultExport = exports.find(i => i.type === 'default') if (name === 'index') {
name = parsePath(path.split('/').slice(0, -1).join('/')).name
if (defaultExport) {
let name = parsePath(path).name
if (name === 'index') {
name = parsePath(path.split('/').slice(0, -1).join('/')).name
}
autoImports.push({ name: 'default', as: camelCase(name), from: path })
}
for (const exp of exports) {
if (exp.type === 'named') {
for (const name of exp.names) {
autoImports.push({ name, as: name, from: path })
} }
} else if (exp.type === 'declaration') { autoImports.push({ name: 'default', as: camelCase(name), from: path })
autoImports.push({ name: exp.name, as: exp.name, from: path })
} }
} for (const exp of exports) {
}) if (exp.type === 'named') {
) for (const name of exp.names) {
autoImports.push({ name, as: name, from: path })
}
} else if (exp.type === 'declaration') {
autoImports.push({ name: exp.name, as: exp.name, from: path })
}
}
})
)
}
for (const entry of Array.isArray(dir) ? dir : [dir]) {
if (!existsSync(entry)) { continue }
await performScan(entry)
}
} }

View File

@ -72,9 +72,7 @@ export default defineNuxtModule<AutoImportsOptions>({
: { name: importName.name, as: importName.as || importName.name, from: source.from } : { name: importName.name, as: importName.as || importName.name, from: source.from }
)) ))
// Scan composables/ // Scan composables/
for (const composablesDir of composablesDirs) { await scanForComposables(composablesDirs, ctx.autoImports)
await scanForComposables(composablesDir, ctx.autoImports)
}
// Allow modules extending // Allow modules extending
await nuxt.callHook('autoImports:extend', ctx.autoImports) await nuxt.callHook('autoImports:extend', ctx.autoImports)
// Update context // Update context
@ -96,10 +94,13 @@ 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 regenerateAutoImports()
await nuxt.callHook('builder:generateApp') await nuxt.callHook('builder:generateApp')
} }
}) })
nuxt.hook('builder:generateApp', async () => {
await regenerateAutoImports()
})
} }
}) })