|
| 1 | +importtype{Nuxt}from'@nuxt/schema' |
| 2 | +import{existsSync}from'node:fs' |
| 3 | +import{pathToFileURL}from'node:url' |
| 4 | +import{addTypeTemplate,getNuxtVersion,resolvePathasresolveNuxtPath}from'@nuxt/kit' |
| 5 | +import{dirname}from'pathe' |
| 6 | + |
| 7 | +typeNitroRuntimeCompatibility |
| 8 | +=|{_tag: 'nitro-v2'} |
| 9 | +|{ |
| 10 | +_tag: 'nitro-v3' |
| 11 | +app: string |
| 12 | +cache: string |
| 13 | +h3: string |
| 14 | +runtimeConfig: string |
| 15 | +} |
| 16 | + |
| 17 | +typeResolveNitroImport=(id: string)=>Promise<string> |
| 18 | + |
| 19 | +interfaceNitroCompatibilityOptions{ |
| 20 | +alias?: Record<string,string> |
| 21 | +virtual?: Record<string,string> |
| 22 | +} |
| 23 | + |
| 24 | +interfaceNitroCompatibilityDependencies{ |
| 25 | +addTypeTemplate: typeofaddTypeTemplate |
| 26 | +getNuxtVersion: typeofgetNuxtVersion |
| 27 | +resolveNitroImport?: ResolveNitroImport |
| 28 | +} |
| 29 | + |
| 30 | +constNITRO_RUNTIME_MODULE='#nuxt-scripts/nitro' |
| 31 | +constH3_RUNTIME_MODULE='#nuxt-scripts/h3' |
| 32 | +constTYPE_TEMPLATE_FILENAME='types/nuxt-scripts-nitro.d.ts' |
| 33 | +constdefaultDependencies: NitroCompatibilityDependencies={ |
| 34 | + addTypeTemplate, |
| 35 | + getNuxtVersion, |
| 36 | +} |
| 37 | + |
| 38 | +constnitroV2Runtime=`export { |
| 39 | + defineCachedFunction, |
| 40 | + useNitroApp, |
| 41 | + useRuntimeConfig, |
| 42 | +} from 'nitropack/runtime' |
| 43 | +` |
| 44 | + |
| 45 | +constnitroV3RuntimeTypes=`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 | +functionindent(value: string,spaces: number): string{ |
| 51 | +constpadding=' '.repeat(spaces) |
| 52 | +returnvalue.split('\n').map(line=>`${padding}${line}`).join('\n') |
| 53 | +} |
| 54 | + |
| 55 | +functionrenderRuntimeDeclarations(compatibility: NitroRuntimeCompatibility): string{ |
| 56 | +constnitroRuntime=compatibility._tag==='nitro-v3' ? nitroV3RuntimeTypes : nitroV2Runtime |
| 57 | +consth3Runtime=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 | +functionrenderNitroV3Runtime(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 | +functionapplyNitroRuntimeCompatibility(nuxt: Nuxt,compatibility: NitroRuntimeCompatibility): void{ |
| 80 | +constnuxtOptions=nuxt.optionsasNuxt['options']&{nitro?: NitroCompatibilityOptions} |
| 81 | +constnitroOptions=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 | +asyncfunctioncreateNuxtNitroImportResolver(): Promise<ResolveNitroImport>{ |
| 91 | +constnuxtDir=dirname(awaitresolveNuxtPath('nuxt/package.json')) |
| 92 | +constnitroDir=dirname(awaitresolveNuxtPath('@nuxt/nitro-server/package.json',{cwd: nuxtDir})) |
| 93 | + |
| 94 | +returnasync(id: string)=>{ |
| 95 | +constresolved=awaitresolveNuxtPath(id,{cwd: nitroDir}) |
| 96 | +if(!existsSync(resolved)) |
| 97 | +thrownewError(`[nuxt-scripts] Could not resolve Nitro runtime helper "${id}" from "${nitroDir}".`) |
| 98 | +returnpathToFileURL(resolved).href |
| 99 | +} |
| 100 | +} |
| 101 | + |
| 102 | +asyncfunctionresolveNitroV3Compatibility(resolveNitroImport: ResolveNitroImport): Promise<NitroRuntimeCompatibility>{ |
| 103 | +const[app,cache,h3,runtimeConfig]=awaitPromise.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 | +exportasyncfunctionsetupNitroRuntimeCompatibility( |
| 114 | +nuxt: Nuxt, |
| 115 | +dependencies: NitroCompatibilityDependencies=defaultDependencies, |
| 116 | +): Promise<void>{ |
| 117 | +constcompatibility: NitroRuntimeCompatibility=Number.parseInt(dependencies.getNuxtVersion(nuxt),10)>=5 |
| 118 | + ? awaitresolveNitroV3Compatibility(dependencies.resolveNitroImport||awaitcreateNuxtNitroImportResolver()) |
| 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