feat(kit,schema): add addServerTemplate utility (#29320)

This commit is contained in:
Daniel Roe 2024-10-08 16:58:21 +02:00
parent 666c8c2fe9
commit da0dfd3054
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
3 changed files with 20 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import { existsSync, promises as fsp } from 'node:fs'
import { basename, isAbsolute, join, parse, relative, resolve } from 'pathe'
import hash from 'hash-sum'
import type { Nuxt, NuxtTemplate, NuxtTypeTemplate, ResolvedNuxtTemplate, TSReference } from '@nuxt/schema'
import type { Nuxt, NuxtServerTemplate, NuxtTemplate, NuxtTypeTemplate, ResolvedNuxtTemplate, TSReference } from '@nuxt/schema'
import { withTrailingSlash } from 'ufo'
import { defu } from 'defu'
import type { TSConfig } from 'pkg-types'
@ -32,6 +32,18 @@ export function addTemplate<T> (_template: NuxtTemplate<T> | string) {
return template
}
/**
* Adds a virtual file that can be used within the Nuxt Nitro server build.
*/
export function addServerTemplate (template: NuxtServerTemplate) {
const nuxt = useNuxt()
nuxt.options.nitro.virtual ||= {}
nuxt.options.nitro.virtual[template.filename] = template.getContents
return template
}
/**
* Renders given types using lodash template during build into the project buildDir
* and register them as types.

View File

@ -6,7 +6,7 @@ export type { GenerateAppOptions, HookResult, ImportPresetWithDeprecation, NuxtA
export type { ImportsOptions } from './types/imports'
export type { AppHeadMetaObject, MetaObject, MetaObjectRaw, HeadAugmentations } from './types/head'
export type { ModuleDefinition, ModuleMeta, ModuleOptions, ModuleSetupReturn, ModuleSetupInstallResult, NuxtModule, ResolvedModuleOptions } from './types/module'
export type { Nuxt, NuxtApp, NuxtPlugin, NuxtPluginTemplate, NuxtTemplate, NuxtTypeTemplate, ResolvedNuxtTemplate } from './types/nuxt'
export type { Nuxt, NuxtApp, NuxtPlugin, NuxtPluginTemplate, NuxtTemplate, NuxtTypeTemplate, NuxtServerTemplate, ResolvedNuxtTemplate } from './types/nuxt'
export type { RouterConfig, RouterConfigSerializable, RouterOptions } from './types/router'
// Schema

View File

@ -42,6 +42,12 @@ export interface NuxtTemplate<Options = TemplateDefaultOptions> {
write?: boolean
}
export interface NuxtServerTemplate {
/** The target filename once the template is copied into the Nuxt buildDir */
filename: string
getContents: () => string | Promise<string>
}
export interface ResolvedNuxtTemplate<Options = TemplateDefaultOptions> extends NuxtTemplate<Options> {
filename: string
dst: string