fix(nuxt): support hoisting types of subpath imports (#27720)

This commit is contained in:
Daniel Roe 2024-06-19 18:14:12 +01:00
parent 31255a14db
commit 12fec89939
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 25 additions and 8 deletions

View File

@ -102,18 +102,34 @@ async function initNuxt (nuxt: Nuxt) {
// ignore packages that exist in `package.json` as these can be resolved by TypeScript
if (dependencies.has(pkg) && !(pkg in nightlies)) { return [] }
// deduplicate types for nightly releases
if (pkg in nightlies) {
const nightly = nightlies[pkg as keyof typeof nightlies]
const path = await _resolvePath(nightly, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null)
if (path) {
return [[pkg, [dirname(path)]], [nightly, [dirname(path)]]]
const [_pkg = pkg, _subpath] = /^[^@]+\//.test(pkg) ? pkg.split('/') : [pkg]
const subpath = _subpath ? '/' + _subpath : ''
async function resolveTypePath (path: string) {
try {
const r = await _resolvePath(path, { url: nuxt.options.modulesDir, conditions: ['types', 'import', 'require'] })
if (subpath) {
return r.replace(/(?:\.d)?\.[mc]?[jt]s$/, '')
}
const rootPath = await resolvePackageJSON(r)
return dirname(rootPath)
} catch {
return null
}
}
const path = await _resolvePath(pkg, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null)
// deduplicate types for nightly releases
if (_pkg in nightlies) {
const nightly = nightlies[pkg as keyof typeof nightlies]
const path = await resolveTypePath(nightly + subpath)
if (path) {
return [[pkg, [path]], [nightly + subpath, [path]]]
}
}
const path = await resolveTypePath(pkg + subpath)
if (path) {
return [[pkg, [dirname(path)]]]
return [[pkg, [path]]]
}
return []

View File

@ -47,6 +47,7 @@ export default defineUntypedSchema({
'@vue/compiler-sfc',
'@vue/runtime-dom',
'vue-router',
'vue-router/auto',
'@nuxt/schema',
'nuxt',
]