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