fix(nuxt): don't resolve relative import type paths for deps (#29069)

This commit is contained in:
Daniel Roe 2024-09-19 13:18:28 +01:00 committed by GitHub
parent b67f13cd6b
commit f6ecf9a202
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 3 deletions

View File

@ -179,13 +179,13 @@ async function initNuxt (nuxt: Nuxt) {
const coreTypePackages = nuxt.options.typescript.hoist || [] const coreTypePackages = nuxt.options.typescript.hoist || []
const packageJSON = await readPackageJSON(nuxt.options.rootDir).catch(() => ({}) as PackageJson) const packageJSON = await readPackageJSON(nuxt.options.rootDir).catch(() => ({}) as PackageJson)
const dependencies = new Set([...Object.keys(packageJSON.dependencies || {}), ...Object.keys(packageJSON.devDependencies || {})]) nuxt._dependencies = new Set([...Object.keys(packageJSON.dependencies || {}), ...Object.keys(packageJSON.devDependencies || {})])
const paths = Object.fromEntries(await Promise.all(coreTypePackages.map(async (pkg) => { const paths = Object.fromEntries(await Promise.all(coreTypePackages.map(async (pkg) => {
const [_pkg = pkg, _subpath] = /^[^@]+\//.test(pkg) ? pkg.split('/') : [pkg] const [_pkg = pkg, _subpath] = /^[^@]+\//.test(pkg) ? pkg.split('/') : [pkg]
const subpath = _subpath ? '/' + _subpath : '' const subpath = _subpath ? '/' + _subpath : ''
// ignore packages that exist in `package.json` as these can be resolved by TypeScript // ignore packages that exist in `package.json` as these can be resolved by TypeScript
if (dependencies.has(_pkg) && !(_pkg in nightlies)) { return [] } if (nuxt._dependencies?.has(_pkg) && !(_pkg in nightlies)) { return [] }
// deduplicate types for nightly releases // deduplicate types for nightly releases
if (_pkg in nightlies) { if (_pkg in nightlies) {

View File

@ -166,8 +166,9 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
async function cacheImportPaths (imports: Import[]) { async function cacheImportPaths (imports: Import[]) {
const importSource = Array.from(new Set(imports.map(i => i.from))) const importSource = Array.from(new Set(imports.map(i => i.from)))
// skip relative import paths for node_modules that are explicitly installed
await Promise.all(importSource.map(async (from) => { await Promise.all(importSource.map(async (from) => {
if (resolvedImportPathMap.has(from)) { if (resolvedImportPathMap.has(from) || nuxt._dependencies?.has(from)) {
return return
} }
let path = resolveAlias(from) let path = resolveAlias(from)
@ -176,6 +177,8 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
if (!r) { return r } if (!r) { return r }
const { dir, name } = parseNodeModulePath(r) const { dir, name } = parseNodeModulePath(r)
if (name && nuxt._dependencies?.has(name)) { return from }
if (!dir || !name) { return r } if (!dir || !name) { return r }
const subpath = await lookupNodeModuleSubpath(r) const subpath = await lookupNodeModuleSubpath(r)
return join(dir, name, subpath || '') return join(dir, name, subpath || '')

View File

@ -76,6 +76,7 @@ export interface Nuxt {
// Private fields. // Private fields.
_version: string _version: string
_ignore?: Ignore _ignore?: Ignore
_dependencies?: Set<string>
/** The resolved Nuxt configuration. */ /** The resolved Nuxt configuration. */
options: NuxtOptions options: NuxtOptions