fix(nuxt): overwrite #app/defaults rather than augmenting (#27567)

This commit is contained in:
Daniel Roe 2024-06-12 17:32:53 +01:00 committed by GitHub
parent 47aa6a1212
commit fbb66c43be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 10 deletions

View File

@ -125,6 +125,7 @@ async function initNuxt (nuxt: Nuxt) {
// Add nuxt types // Add nuxt types
nuxt.hook('prepare:types', (opts) => { nuxt.hook('prepare:types', (opts) => {
opts.references.push({ types: 'nuxt' }) opts.references.push({ types: 'nuxt' })
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/app-defaults.d.ts') })
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/plugins.d.ts') }) opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/plugins.d.ts') })
// Add vue shim // Add vue shim
if (nuxt.options.typescript.shim) { if (nuxt.options.typescript.shim) {

View File

@ -7,7 +7,7 @@ import escapeRE from 'escape-string-regexp'
import { hash } from 'ohash' import { hash } from 'ohash'
import { camelCase } from 'scule' import { camelCase } from 'scule'
import { filename } from 'pathe/utils' import { filename } from 'pathe/utils'
import type { NuxtTemplate } from 'nuxt/schema' import type { NuxtTemplate, NuxtTypeTemplate } from 'nuxt/schema'
import { annotatePlugins, checkForCircularDependencies } from './app' import { annotatePlugins, checkForCircularDependencies } from './app'
@ -96,6 +96,20 @@ export const serverPluginTemplate: NuxtTemplate = {
}, },
} }
export const appDefaults: NuxtTypeTemplate = {
filename: 'types/app-defaults.d.ts',
getContents: (ctx) => {
const isV4 = ctx.nuxt.options.future.compatibilityVersion === 4
return `
declare module '#app/defaults' {
type DefaultAsyncDataErrorValue = ${isV4 ? 'undefined' : 'null'}
type DefaultAsyncDataValue = ${isV4 ? 'undefined' : 'null'}
type DefaultErrorValue = ${isV4 ? 'undefined' : 'null'}
type DedupeOption = ${isV4 ? '\'cancel\' | \'defer\'' : 'boolean | \'cancel\' | \'defer\''}
}`
},
}
export const pluginsDeclaration: NuxtTemplate = { export const pluginsDeclaration: NuxtTemplate = {
filename: 'types/plugins.d.ts', filename: 'types/plugins.d.ts',
getContents: async (ctx) => { getContents: async (ctx) => {
@ -112,8 +126,6 @@ export const pluginsDeclaration: NuxtTemplate = {
const pluginsName = (await annotatePlugins(ctx.nuxt, ctx.app.plugins)).filter(p => p.name).map(p => `'${p.name}'`) const pluginsName = (await annotatePlugins(ctx.nuxt, ctx.app.plugins)).filter(p => p.name).map(p => `'${p.name}'`)
const isV4 = ctx.nuxt.options.future.compatibilityVersion === 4
return `// Generated by Nuxt' return `// Generated by Nuxt'
import type { Plugin } from '#app' import type { Plugin } from '#app'
@ -132,13 +144,6 @@ declare module '#app' {
} }
} }
declare module '#app/defaults' {
type DefaultAsyncDataErrorValue = ${isV4 ? 'undefined' : 'null'}
type DefaultAsyncDataValue = ${isV4 ? 'undefined' : 'null'}
type DefaultErrorValue = ${isV4 ? 'undefined' : 'null'}
type DedupeOption = ${isV4 ? '\'cancel\' | \'defer\'' : 'boolean | \'cancel\' | \'defer\''}
}
declare module 'vue' { declare module 'vue' {
interface ComponentCustomProperties extends NuxtAppInjections { } interface ComponentCustomProperties extends NuxtAppInjections { }
} }