fix(nuxt): improve path resolve for import.d.ts (#24413)

This commit is contained in:
Anthony Fu 2023-11-23 22:01:26 +01:00 committed by GitHub
parent 36aee52011
commit 338908e0f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -145,12 +145,16 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
const resolvedImportPathMap = new Map<string, string>()
const r = ({ from }: Import) => resolvedImportPathMap.get(from)
async function cacheImportPaths (imports: Import[]) {
for (const i of imports) {
if (resolvedImportPathMap.has(i.from)) { continue }
let path = resolveAlias(i.from)
async function cacheImportPaths(imports: Import[]) {
const importSource = Array.from(new Set(imports.map(i => i.from)))
await Promise.all(importSource.map(async (from) => {
if (resolvedImportPathMap.has(from)) {
return
}
let path = resolveAlias(from)
if (!isAbsolute(path)) {
path = await tryResolveModule(i.from, nuxt.options.modulesDir).then(async (r) => {
path = await tryResolveModule(from, nuxt.options.modulesDir).then(async (r) => {
if (!r) { return r }
const { dir, name } = parseNodeModulePath(r)
@ -164,8 +168,8 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
}
path = stripExtension(path)
resolvedImportPathMap.set(i.from, path)
}
resolvedImportPathMap.set(from, path)
}))
}
addTemplate({
@ -176,7 +180,7 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
addTemplate({
filename: 'types/imports.d.ts',
getContents: async () => {
const imports = await ctx.getImports().then(r => r.filter(i => !i.type))
const imports = await ctx.getImports()
await cacheImportPaths(imports)
return '// Generated by auto imports\n' + (
options.autoImport