Skip to content

Commit

Permalink
fix(nuxt): resolve defu/h3 paths in type templates (#26085)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 6, 2024
1 parent b4bce57 commit bbf4186
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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' {
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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)}
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {',
Expand All @@ -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'`,
Expand Down

0 comments on commit bbf4186

Please sign in to comment.