mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 15:50:32 +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 { isAbsolute, join, normalize, relative, resolve } from 'pathe'
|
||||||
import type { Import, Unimport } from 'unimport'
|
import type { Import, Unimport } from 'unimport'
|
||||||
import { createUnimport, scanDirExports } from 'unimport'
|
import { createUnimport, scanDirExports } from 'unimport'
|
||||||
import type { ImportPresetWithDeprecation, ImportsOptions } from 'nuxt/schema'
|
import type { ImportPresetWithDeprecation, ImportsOptions } from 'nuxt/schema'
|
||||||
|
|
||||||
|
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
|
||||||
import { TransformPlugin } from './transform'
|
import { TransformPlugin } from './transform'
|
||||||
import { defaultPresets } from './presets'
|
import { defaultPresets } from './presets'
|
||||||
|
|
||||||
@ -140,19 +141,29 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
|
|||||||
// Remove file extension for benefit of TypeScript
|
// Remove file extension for benefit of TypeScript
|
||||||
const stripExtension = (path: string) => path.replace(/\.[a-z]+$/, '')
|
const stripExtension = (path: string) => path.replace(/\.[a-z]+$/, '')
|
||||||
|
|
||||||
const resolved: Record<string, string> = {}
|
const resolvedImportPathMap = new Map<string, string>()
|
||||||
const r = ({ from }: Import) => {
|
const r = ({ from }: Import) => resolvedImportPathMap.get(from)
|
||||||
if (resolved[from]) {
|
async function cacheImportPaths (imports: Import[]) {
|
||||||
return resolved[from]
|
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)) {
|
if (isAbsolute(path)) {
|
||||||
path = relative(join(nuxt.options.buildDir, 'types'), path)
|
path = relative(join(nuxt.options.buildDir, 'types'), path)
|
||||||
}
|
}
|
||||||
|
|
||||||
path = stripExtension(path)
|
path = stripExtension(path)
|
||||||
resolved[from] = path
|
resolvedImportPathMap.set(i.from, path)
|
||||||
return path
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
@ -162,10 +173,14 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
|
|||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
filename: 'types/imports.d.ts',
|
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
|
options.autoImport
|
||||||
? await ctx.generateTypeDeclarations({ resolvePath: r })
|
? await ctx.generateTypeDeclarations({ resolvePath: r })
|
||||||
: '// Implicit auto importing is disabled, you can use explicitly import from `#imports` instead.'
|
: '// Implicit auto importing is disabled, you can use explicitly import from `#imports` instead.'
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user