fix(nuxt): resolve defu/h3 paths in type templates (#26085)

This commit is contained in:
Daniel Roe 2024-03-06 12:44:33 +00:00 committed by GitHub
parent b4bce578c6
commit bbf4186507
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 8 deletions

View File

@ -8,6 +8,8 @@ import { hash } from 'ohash'
import { camelCase } from 'scule'
import { filename } from 'pathe/utils'
import type { NuxtTemplate } from 'nuxt/schema'
import { tryResolveModule } from '@nuxt/kit'
import { annotatePlugins, checkForCircularDependencies } from './app'
export const vueShim: NuxtTemplate = {
@ -221,12 +223,13 @@ export const middlewareTemplate: NuxtTemplate = {
export const nitroSchemaTemplate: NuxtTemplate = {
filename: 'types/nitro-nuxt.d.ts',
getContents: () => {
async getContents ({ nuxt }) {
const localH3 = await tryResolveModule('h3', nuxt.options.modulesDir) || 'h3'
return /* typescript */`
/// <reference path="./schema.d.ts" />
import type { RuntimeConfig } from 'nuxt/schema'
import type { H3Event } from 'h3'
import type { H3Event } from '${localH3}'
import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from 'nuxt/dist/core/runtime/nitro/renderer'
declare module 'nitropack' {
@ -261,10 +264,11 @@ export const useRuntimeConfig = () => window?.__NUXT__?.config || {}
export const appConfigDeclarationTemplate: NuxtTemplate = {
filename: 'types/app.config.d.ts',
getContents: ({ app, nuxt }) => {
async getContents ({ app, nuxt }) {
const localDefu = await tryResolveModule('defu', nuxt.options.modulesDir) || 'defu'
return `
import type { CustomAppConfig } from 'nuxt/schema'
import type { Defu } from 'defu'
import type { Defu } from '${localDefu}'
${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from ${JSON.stringify(id.replace(/(?<=\w)\.\w+$/g, ''))}`).join('\n')}
declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
@ -298,10 +302,11 @@ declare module '@nuxt/schema' {
export const appConfigTemplate: NuxtTemplate = {
filename: 'app.config.mjs',
write: true,
getContents ({ app, nuxt }) {
async getContents ({ app, nuxt }) {
const localDefu = await tryResolveModule('defu', nuxt.options.modulesDir) || 'defu'
return `
import { updateAppConfig } from '#app/config'
import { defuFn } from 'defu'
import { defuFn } from '${localDefu}'
const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}

View File

@ -479,7 +479,7 @@ export default defineNuxtModule({
getContents: ({ nuxt, app }: { nuxt: Nuxt, app: NuxtApp }) => {
const composablesFile = relative(join(nuxt.options.buildDir, 'types'), resolve(runtimeDir, 'composables'))
return [
'import { ComputedRef, MaybeRef } from \'vue\'',
'import type { ComputedRef, MaybeRef } from \'vue\'',
`export type LayoutKey = ${Object.keys(app.layouts).map(name => genString(name)).join(' | ') || 'string'}`,
`declare module ${genString(composablesFile)} {`,
' interface PageMeta {',
@ -499,7 +499,7 @@ export default defineNuxtModule({
const runtimeDir = resolve(distDir, 'pages/runtime')
const composablesFile = relative(join(nuxt.options.buildDir, 'types'), resolve(runtimeDir, 'composables'))
return [
'import { ComputedRef, MaybeRef } from \'vue\'',
'import type { ComputedRef, MaybeRef } from \'vue\'',
`declare module ${genString(composablesFile)} {`,
' interface PageMeta {',
` viewTransition?: boolean | 'always'`,