fix(nuxt): augment nitro config within server context as well (#21724)

This commit is contained in:
Daniel Roe 2023-06-25 17:40:53 +01:00 committed by GitHub
parent 1cd48cbef9
commit 5b8370414a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 9 deletions

View File

@ -1,7 +1,7 @@
import { join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addTemplate, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule } from '@nuxt/kit'
import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema'
import escapeRE from 'escape-string-regexp'
@ -154,14 +154,6 @@ async function initNuxt (nuxt: Nuxt) {
// Transpile #app if it is imported directly from subpath export
nuxt.options.build.transpile.push('nuxt/app')
// This is currently a placeholder for future augmentations that need to be applied in Nitro context
addTemplate({
filename: 'types/nitro-nuxt.d.ts',
getContents: () => {
return 'export {}'
}
})
// Transpile layers within node_modules
nuxt.options.build.transpile.push(
...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string)

View File

@ -199,6 +199,39 @@ export const middlewareTemplate: NuxtTemplate<TemplateContext> = {
}
}
export const nitroSchemaTemplate: NuxtTemplate = {
filename: 'types/nitro-nuxt.d.ts',
getContents: () => {
return /* typescript */`
/// <reference path="./schema.d.ts" />
import type { RuntimeConfig } from 'nuxt/schema'
import type { H3Event } from 'h3'
import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from 'nuxt/dist/core/runtime/nitro/renderer'
declare module 'nitropack' {
interface NitroRuntimeConfigApp {
buildAssetsDir: string
cdnURL: string
}
interface NitroRuntimeConfig extends RuntimeConfig {}
interface NitroRouteConfig {
ssr?: boolean
experimentalNoScripts?: boolean
}
interface NitroRouteRules {
ssr?: boolean
experimentalNoScripts?: boolean
}
interface NitroRuntimeHooks {
'render:html': (htmlContext: NuxtRenderHTMLContext, context: { event: H3Event }) => void | Promise<void>
'render:island': (islandResponse: NuxtIslandResponse, context: { event: H3Event, islandContext: NuxtIslandContext }) => void | Promise<void>
}
}
`
}
}
export const clientConfigTemplate: NuxtTemplate = {
filename: 'nitro.client.mjs',
getContents: () => `

View File

@ -2,6 +2,7 @@
export * from './dist/index'
import type { SchemaDefinition, RuntimeConfig } from 'nuxt/schema'
import type { H3Event } from 'h3'
import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from './dist/core/runtime/nitro/renderer'
declare global {
@ -9,6 +10,7 @@ declare global {
const defineNuxtSchema: (schema: SchemaDefinition) => SchemaDefinition
}
// Note: Keep in sync with packages/nuxt/src/core/templates.ts
declare module 'nitropack' {
interface NitroRuntimeConfigApp {
buildAssetsDir: string