fix(nuxt): resolve type import paths (#22476)

This commit is contained in:
Daniel Roe 2023-08-07 14:19:32 +01:00 committed by GitHub
parent ae8314b236
commit f4ee12e6ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 18 deletions

View File

@ -1,9 +1,10 @@
import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, resolveAlias, updateTemplates, useNuxt } from '@nuxt/kit'
import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports } from 'unimport'
import type { ImportPresetWithDeprecation, ImportsOptions } from 'nuxt/schema'
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
import { TransformPlugin } from './transform'
import { defaultPresets } from './presets'
@ -140,19 +141,29 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
// Remove file extension for benefit of TypeScript
const stripExtension = (path: string) => path.replace(/\.[a-z]+$/, '')
const resolved: Record<string, string> = {}
const r = ({ from }: Import) => {
if (resolved[from]) {
return resolved[from]
}
let path = resolveAlias(from)
if (isAbsolute(path)) {
path = relative(join(nuxt.options.buildDir, 'types'), path)
}
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)
if (!isAbsolute(path)) {
path = await tryResolveModule(i.from, nuxt.options.modulesDir).then(async (r) => {
if (!r) { return r }
path = stripExtension(path)
resolved[from] = path
return path
const { dir, name } = parseNodeModulePath(r)
if (!dir || !name) { return r }
const subpath = await lookupNodeModuleSubpath(r)
return join(dir, name, subpath || '')
}) ?? path
}
if (isAbsolute(path)) {
path = relative(join(nuxt.options.buildDir, 'types'), path)
}
path = stripExtension(path)
resolvedImportPathMap.set(i.from, path)
}
}
addTemplate({
@ -162,10 +173,14 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
addTemplate({
filename: 'types/imports.d.ts',
getContents: async () => '// Generated by auto imports\n' + (
options.autoImport
? await ctx.generateTypeDeclarations({ resolvePath: r })
: '// Implicit auto importing is disabled, you can use explicitly import from `#imports` instead.'
)
getContents: async () => {
const imports = await ctx.getImports().then(r => r.filter(i => !i.type))
await cacheImportPaths(imports)
return '// Generated by auto imports\n' + (
options.autoImport
? await ctx.generateTypeDeclarations({ resolvePath: r })
: '// Implicit auto importing is disabled, you can use explicitly import from `#imports` instead.'
)
}
})
}