Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c7e68c

Browse files
committed
fix(nuxt): handle nightly releases for hoisted types
1 parent 33ce71d commit 3c7e68c

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

packages/nuxt/src/core/nuxt.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type { LoadNuxtOptions } from '@nuxt/kit'
44
import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
55
import { resolvePath as _resolvePath } from 'mlly'
66
import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema'
7-
import { resolvePackageJSON } from 'pkg-types'
7+
import type { PackageJson } from 'pkg-types'
8+
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
89

910
import escapeRE from 'escape-string-regexp'
1011
import fse from 'fs-extra'
@@ -51,6 +52,14 @@ export function createNuxt (options: NuxtOptions): Nuxt {
5152
return nuxt
5253
}
5354

55+
const nightlies = {
56+
nitropack: 'nitropack-nightly',
57+
h3: 'h3-nightly',
58+
nuxt: 'nuxt-nightly',
59+
'@nuxt/schema': '@nuxt/schema-nightly',
60+
'@nuxt/kit': '@nuxt/kit-nightly'
61+
}
62+
5463
async function initNuxt (nuxt: Nuxt) {
5564
// Register user hooks
5665
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
@@ -64,11 +73,28 @@ async function initNuxt (nuxt: Nuxt) {
6473
nuxt.hook('close', () => nuxtCtx.unset())
6574

6675
const coreTypePackages = nuxt.options.typescript.hoist || []
76+
const packageJSON = await readPackageJSON(nuxt.options.rootDir).catch(() => ({}) as PackageJson)
77+
const dependencies = new Set([...Object.keys(packageJSON.dependencies || {}), ...Object.keys(packageJSON.devDependencies || {})])
6778
const paths = Object.fromEntries(await Promise.all(coreTypePackages.map(async (pkg) => {
79+
// ignore packages that exist in `package.json` as these can be resolved by TypeScript
80+
if (dependencies.has(pkg) && !(pkg in nightlies)) { return [] }
81+
82+
// deduplicate types for nightly releases
83+
if (pkg in nightlies) {
84+
const nightly = nightlies[pkg as keyof typeof nightlies]
85+
const path = await _resolvePath(nightly, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null)
86+
if (path) {
87+
return [[pkg, [dirname(path)]], [nightly, [dirname(path)]]]
88+
}
89+
}
90+
6891
const path = await _resolvePath(pkg, { url: nuxt.options.modulesDir }).then(r => resolvePackageJSON(r)).catch(() => null)
69-
if (!path) { return }
70-
return [pkg, [dirname(path)]]
71-
})).then(r => r.filter(Boolean) as [string, [string]][]))
92+
if (path) {
93+
return [[pkg, [dirname(path)]]]
94+
}
95+
96+
return []
97+
})).then(r => r.flat()))
7298

7399
// Set nitro resolutions for types that might be obscured with shamefully-hoist=false
74100
nuxt.options.nitro.typescript = defu(nuxt.options.nitro.typescript, {

packages/schema/src/config/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default defineUntypedSchema({
3232
*/
3333
hoist: {
3434
$resolve: (val) => {
35-
const defaults = ['nitropack', 'defu', 'h3', '@unhead/vue', 'vue', 'vue-router', '@nuxt/schema']
35+
const defaults = ['nitropack', 'defu', 'h3', '@unhead/vue', 'vue', 'vue-router', '@nuxt/schema', 'nuxt', 'consola']
3636
return val === false ? [] : (Array.isArray(val) ? val.concat(defaults) : defaults)
3737
}
3838
},

0 commit comments

Comments
 (0)