feat: provide name and allow multiple Nuxt App within the same process

This commit is contained in:
Julien Huang 2025-01-08 16:22:31 +01:00
parent 4c66a7994d
commit d7946f92ac
6 changed files with 22 additions and 10 deletions

View File

@ -1,8 +1,9 @@
import { getContext } from 'unctx'
import type { Nuxt } from '@nuxt/schema'
import { asyncNameStorage } from './utils'
/** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */
export const nuxtCtx = getContext<Nuxt>('nuxt')
export const nuxtCtx = () => getContext<Nuxt>(asyncNameStorage.getStore()!)
// TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6
@ -16,7 +17,7 @@ export const nuxtCtx = getContext<Nuxt>('nuxt')
* ```
*/
export function useNuxt (): Nuxt {
const instance = nuxtCtx.tryUse()
const instance = nuxtCtx().tryUse()
if (!instance) {
throw new Error('Nuxt instance is unavailable!')
}
@ -36,5 +37,5 @@ export function useNuxt (): Nuxt {
* ```
*/
export function tryUseNuxt (): Nuxt | null {
return nuxtCtx.tryUse()
return nuxtCtx().tryUse()
}

View File

@ -34,3 +34,4 @@ export { logger, useLogger } from './logger'
// Internal Utils
export { resolveModule, tryResolveModule, importModule, tryImportModule, requireModule, tryRequireModule } from './internal/esm'
export type { ImportModuleOptions, ResolveModuleOptions } from './internal/esm'
export { asyncNameStorage } from './utils'

View File

@ -1,6 +1,10 @@
import { AsyncLocalStorage } from "node:async_hooks"
/** @since 3.9.0 */
export function toArray<T> (value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
export const MODE_RE = /\.(server|client)(\.\w+)*$/
export const asyncNameStorage = new AsyncLocalStorage<string>()

View File

@ -4,7 +4,7 @@ import { join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable'
import ignore from 'ignore'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro, asyncNameStorage } from '@nuxt/kit'
import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema'
import type { PackageJson } from 'pkg-types'
import { readPackageJSON } from 'pkg-types'
@ -47,10 +47,11 @@ import { ComposableKeysPlugin } from './plugins/composable-keys'
import { resolveDeepImportsPlugin } from './plugins/resolve-deep-imports'
import { PrehydrateTransformPlugin } from './plugins/prehydrate'
import { VirtualFSPlugin } from './plugins/virtual'
import { randomUUID } from 'node:crypto'
export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = createHooks<NuxtHooks>()
const name = randomUUID()
const nuxt: Nuxt = {
_version: version,
options,
@ -58,7 +59,7 @@ export function createNuxt (options: NuxtOptions): Nuxt {
callHook: hooks.callHook,
addHooks: hooks.addHooks,
hook: hooks.hook,
ready: () => initNuxt(nuxt),
ready: () => asyncNameStorage.run(name, () => initNuxt(nuxt)) ,
close: () => hooks.callHook('close', nuxt),
vfs: {},
apps: {},
@ -175,8 +176,8 @@ async function initNuxt (nuxt: Nuxt) {
})
// Set nuxt instance for useNuxt
nuxtCtx.set(nuxt)
nuxt.hook('close', () => nuxtCtx.unset())
nuxtCtx().set(nuxt)
nuxt.hook('close', () => nuxtCtx().unset())
const coreTypePackages = nuxt.options.typescript.hoist || []

View File

@ -79,6 +79,11 @@ export interface NuxtApp {
}
export interface Nuxt {
/**
* The name of the Nuxt project, this can be useful for build time debugging and mono-repos.
* Defaults to a randomUUID
*/
name?: string
// Private fields.
_version: string
_ignore?: Ignore