fix(nuxt): handle nightly releases for hoisted types

This commit is contained in:
Daniel Roe 2024-03-16 17:18:31 -07:00
parent 33ce71dd12
commit 3c7e68c846
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
2 changed files with 31 additions and 5 deletions

View File

@ -4,7 +4,8 @@ import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import { resolvePath as _resolvePath } from 'mlly' import { resolvePath as _resolvePath } from 'mlly'
import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema' import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema'
import { resolvePackageJSON } from 'pkg-types' import type { PackageJson } from 'pkg-types'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
import fse from 'fs-extra' import fse from 'fs-extra'
@ -51,6 +52,14 @@ export function createNuxt (options: NuxtOptions): Nuxt {
return nuxt return nuxt
} }
const nightlies = {
nitropack: 'nitropack-nightly',
h3: 'h3-nightly',
nuxt: 'nuxt-nightly',
'@nuxt/schema': '@nuxt/schema-nightly',
'@nuxt/kit': '@nuxt/kit-nightly'
}
async function initNuxt (nuxt: Nuxt) { async function initNuxt (nuxt: Nuxt) {
// Register user hooks // Register user hooks
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
@ -64,11 +73,28 @@ async function initNuxt (nuxt: Nuxt) {
nuxt.hook('close', () => nuxtCtx.unset()) nuxt.hook('close', () => nuxtCtx.unset())
const coreTypePackages = nuxt.options.typescript.hoist || [] const coreTypePackages = nuxt.options.typescript.hoist || []
const packageJSON = await readPackageJSON(nuxt.options.rootDir).catch(() => ({}) as PackageJson)
const 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) => {
// 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 path = await _resolvePath(pkg, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null) const path = await _resolvePath(pkg, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null)
if (!path) { return } if (path) {
return [pkg, [dirname(path)]] return [[pkg, [dirname(path)]]]
})).then(r => r.filter(Boolean) as [string, [string]][])) }
return []
})).then(r => r.flat()))
// Set nitro resolutions for types that might be obscured with shamefully-hoist=false // Set nitro resolutions for types that might be obscured with shamefully-hoist=false
nuxt.options.nitro.typescript = defu(nuxt.options.nitro.typescript, { nuxt.options.nitro.typescript = defu(nuxt.options.nitro.typescript, {

View File

@ -32,7 +32,7 @@ export default defineUntypedSchema({
*/ */
hoist: { hoist: {
$resolve: (val) => { $resolve: (val) => {
const defaults = ['nitropack', 'defu', 'h3', '@unhead/vue', 'vue', 'vue-router', '@nuxt/schema'] const defaults = ['nitropack', 'defu', 'h3', '@unhead/vue', 'vue', 'vue-router', '@nuxt/schema', 'nuxt', 'consola']
return val === false ? [] : (Array.isArray(val) ? val.concat(defaults) : defaults) return val === false ? [] : (Array.isArray(val) ? val.concat(defaults) : defaults)
} }
}, },