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 bbcab053bc
commit 18f0724402
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
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 // ignore packages that exist in `package.json` as these can be resolved by TypeScript
if (dependencies.has(pkg) && !(pkg in nightlies)) { return [] } if (dependencies.has(pkg) && !(pkg in nightlies)) { return [] }
// deduplicate types for nightly releases const [_pkg = pkg, _subpath] = /^[^@]+\//.test(pkg) ? pkg.split('/') : [pkg]
if (pkg in nightlies) { const subpath = _subpath ? '/' + _subpath : ''
const nightly = nightlies[pkg as keyof typeof nightlies]
const path = await _resolvePath(nightly, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null) async function resolveTypePath (path: string) {
if (path) { try {
return [[pkg, [dirname(path)]], [nightly, [dirname(path)]]] 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) { if (path) {
return [[pkg, [dirname(path)]]] return [[pkg, [path]], [nightly + subpath, [path]]]
}
}
const path = await resolveTypePath(pkg + subpath)
if (path) {
return [[pkg, [path]]]
} }
return [] return []

View File

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