|
| 1 | +import type { Nuxt } from '@nuxt/schema' |
| 2 | +import { existsSync } from 'node:fs' |
| 3 | +import { pathToFileURL } from 'node:url' |
| 4 | +import { addTypeTemplate, getNuxtVersion, resolvePath as resolveNuxtPath } from '@nuxt/kit' |
| 5 | +import { dirname } from 'pathe' |
| 6 | + |
| 7 | +type NitroRuntimeCompatibility |
| 8 | + = | { _tag: 'nitro-v2' } |
| 9 | + | { |
| 10 | + _tag: 'nitro-v3' |
| 11 | + app: string |
| 12 | + cache: string |
| 13 | + h3: string |
| 14 | + runtimeConfig: string |
| 15 | + } |
| 16 | + |
| 17 | +type ResolveNitroImport = (id: string) => Promise<string> |
| 18 | + |
| 19 | +interface NitroCompatibilityOptions { |
| 20 | + alias?: Record<string, string> |
| 21 | + virtual?: Record<string, string> |
| 22 | +} |
| 23 | + |
| 24 | +interface NitroCompatibilityDependencies { |
| 25 | + addTypeTemplate: typeof addTypeTemplate |
| 26 | + getNuxtVersion: typeof getNuxtVersion |
| 27 | + resolveNitroImport?: ResolveNitroImport |
| 28 | +} |
| 29 | + |
| 30 | +const NITRO_RUNTIME_MODULE = '#nuxt-scripts/nitro' |
| 31 | +const H3_RUNTIME_MODULE = '#nuxt-scripts/h3' |
| 32 | +const TYPE_TEMPLATE_FILENAME = 'types/nuxt-scripts-nitro.d.ts' |
| 33 | +const defaultDependencies: NitroCompatibilityDependencies = { |
| 34 | + addTypeTemplate, |
| 35 | + getNuxtVersion, |
| 36 | +} |
| 37 | + |
| 38 | +const nitroV2Runtime = `export { |
| 39 | + defineCachedFunction, |
| 40 | + useNitroApp, |
| 41 | + useRuntimeConfig, |
| 42 | +} from 'nitropack/runtime' |
| 43 | +` |
| 44 | + |
| 45 | +const nitroV3RuntimeTypes = `export { useNitroApp } from 'nitro/app' |
| 46 | +export { defineCachedFunction } from 'nitro/cache' |
| 47 | +export function useRuntimeConfig(event?: import('nitro/h3').H3Event): ReturnType<typeof import('nitro/runtime-config').useRuntimeConfig> |
| 48 | +` |
| 49 | + |
| 50 | +function indent(value: string, spaces: number): string { |
| 51 | + const padding = ' '.repeat(spaces) |
| 52 | + return value.split('\n').map(line => `${padding}${line}`).join('\n') |
| 53 | +} |
| 54 | + |
| 55 | +function renderRuntimeDeclarations(compatibility: NitroRuntimeCompatibility): string { |
| 56 | + const nitroRuntime = compatibility._tag === 'nitro-v3' ? nitroV3RuntimeTypes : nitroV2Runtime |
| 57 | + const h3Runtime = compatibility._tag === 'nitro-v3' |
| 58 | + ? `export * from 'nitro/h3'\n` |
| 59 | + : `export * from 'h3'\n` |
| 60 | + |
| 61 | + return `declare module '${NITRO_RUNTIME_MODULE}' { |
| 62 | +${indent(nitroRuntime.trim(), 2)} |
| 63 | +} |
| 64 | +
|
| 65 | +declare module '${H3_RUNTIME_MODULE}' { |
| 66 | +${indent(h3Runtime.trim(), 2)} |
| 67 | +} |
| 68 | +` |
| 69 | +} |
| 70 | + |
| 71 | +function renderNitroV3Runtime(compatibility: Extract<NitroRuntimeCompatibility, { _tag: 'nitro-v3' }>): string { |
| 72 | + return `export { useNitroApp } from ${JSON.stringify(compatibility.app)} |
| 73 | +export { defineCachedFunction } from ${JSON.stringify(compatibility.cache)} |
| 74 | +import { useRuntimeConfig as _useRuntimeConfig } from ${JSON.stringify(compatibility.runtimeConfig)} |
| 75 | +export function useRuntimeConfig(_event) { return _useRuntimeConfig() } |
| 76 | +` |
| 77 | +} |
| 78 | + |
| 79 | +function applyNitroRuntimeCompatibility(nuxt: Nuxt, compatibility: NitroRuntimeCompatibility): void { |
| 80 | + const nuxtOptions = nuxt.options as Nuxt['options'] & { nitro?: NitroCompatibilityOptions } |
| 81 | + const nitroOptions = nuxtOptions.nitro ||= {} |
| 82 | + nitroOptions.alias ||= {} |
| 83 | + nitroOptions.virtual ||= {} |
| 84 | + nitroOptions.alias[H3_RUNTIME_MODULE] = compatibility._tag === 'nitro-v3' ? compatibility.h3 : 'h3' |
| 85 | + nitroOptions.virtual[NITRO_RUNTIME_MODULE] = compatibility._tag === 'nitro-v3' |
| 86 | + ? renderNitroV3Runtime(compatibility) |
| 87 | + : nitroV2Runtime |
| 88 | +} |
| 89 | + |
| 90 | +async function createNuxtNitroImportResolver(): Promise<ResolveNitroImport> { |
| 91 | + const nuxtDir = dirname(await resolveNuxtPath('nuxt/package.json')) |
| 92 | + const nitroDir = dirname(await resolveNuxtPath('@nuxt/nitro-server/package.json', { cwd: nuxtDir })) |
| 93 | + |
| 94 | + return async (id: string) => { |
| 95 | + const resolved = await resolveNuxtPath(id, { cwd: nitroDir }) |
| 96 | + if (!existsSync(resolved)) |
| 97 | + throw new Error(`[nuxt-scripts] Could not resolve Nitro runtime helper "${id}" from "${nitroDir}".`) |
| 98 | + return pathToFileURL(resolved).href |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +async function resolveNitroV3Compatibility(resolveNitroImport: ResolveNitroImport): Promise<NitroRuntimeCompatibility> { |
| 103 | + const [app, cache, h3, runtimeConfig] = await Promise.all([ |
| 104 | + resolveNitroImport('nitro/app'), |
| 105 | + resolveNitroImport('nitro/cache'), |
| 106 | + resolveNitroImport('nitro/h3'), |
| 107 | + resolveNitroImport('nitro/runtime-config'), |
| 108 | + ]) |
| 109 | + |
| 110 | + return { _tag: 'nitro-v3', app, cache, h3, runtimeConfig } |
| 111 | +} |
| 112 | + |
| 113 | +export async function setupNitroRuntimeCompatibility( |
| 114 | + nuxt: Nuxt, |
| 115 | + dependencies: NitroCompatibilityDependencies = defaultDependencies, |
| 116 | +): Promise<void> { |
| 117 | + const compatibility: NitroRuntimeCompatibility = Number.parseInt(dependencies.getNuxtVersion(nuxt), 10) >= 5 |
| 118 | + ? await resolveNitroV3Compatibility(dependencies.resolveNitroImport || await createNuxtNitroImportResolver()) |
| 119 | + : { _tag: 'nitro-v2' } |
| 120 | + |
| 121 | + applyNitroRuntimeCompatibility(nuxt, compatibility) |
| 122 | + nuxt.hooks.hookOnce('modules:done', () => applyNitroRuntimeCompatibility(nuxt, compatibility)) |
| 123 | + dependencies.addTypeTemplate({ |
| 124 | + filename: TYPE_TEMPLATE_FILENAME, |
| 125 | + getContents: async () => renderRuntimeDeclarations(compatibility), |
| 126 | + }, { nitro: true, node: true, nuxt: true }) |
| 127 | +} |
0 commit comments