diff --git a/packages/kit/src/template.ts b/packages/kit/src/template.ts index 86da4d1a49..1a95ce107c 100644 --- a/packages/kit/src/template.ts +++ b/packages/kit/src/template.ts @@ -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 (_template: NuxtTemplate | 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. diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index a2ab040783..6b74fbc1e0 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -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 diff --git a/packages/schema/src/types/nuxt.ts b/packages/schema/src/types/nuxt.ts index 41130835a6..ab9d1a2eeb 100644 --- a/packages/schema/src/types/nuxt.ts +++ b/packages/schema/src/types/nuxt.ts @@ -42,6 +42,12 @@ export interface NuxtTemplate { write?: boolean } +export interface NuxtServerTemplate { + /** The target filename once the template is copied into the Nuxt buildDir */ + filename: string + getContents: () => string | Promise +} + export interface ResolvedNuxtTemplate extends NuxtTemplate { filename: string dst: string