mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +00:00
fix(nuxt): resolve type import paths (#22476)
This commit is contained in:
parent
ae8314b236
commit
f4ee12e6ba
@ -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]
|
||||
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 }
|
||||
|
||||
const { dir, name } = parseNodeModulePath(r)
|
||||
if (!dir || !name) { return r }
|
||||
const subpath = await lookupNodeModuleSubpath(r)
|
||||
return join(dir, name, subpath || '')
|
||||
}) ?? path
|
||||
}
|
||||
let path = resolveAlias(from)
|
||||
if (isAbsolute(path)) {
|
||||
path = relative(join(nuxt.options.buildDir, 'types'), path)
|
||||
}
|
||||
|
||||
path = stripExtension(path)
|
||||
resolved[from] = path
|
||||
return 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' + (
|
||||
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.'
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user