refactor: globalNuxtCtx renamed to nuxtCtx

This commit is contained in:
Julien Huang 2025-01-11 21:36:15 +01:00
parent 982af4e1a2
commit f5be4bbd5a
3 changed files with 11 additions and 10 deletions

View File

@ -5,7 +5,8 @@ import { logger } from './logger'
/** Direct access to the Nuxt context with asyncLocalStorage - see https://github.com/unjs/unctx. */ /** Direct access to the Nuxt context with asyncLocalStorage - see https://github.com/unjs/unctx. */
export const getNuxtCtx = () => getContext<Nuxt>(asyncNameStorage.getStore()!) export const getNuxtCtx = () => getContext<Nuxt>(asyncNameStorage.getStore()!)
export const globalNuxtCtx = getContext<Nuxt>('nuxt-global') /** Global nuxt ctx - see https://github.com/unjs/unctx. */
export const nuxtCtx = getContext<Nuxt>('nuxt-global')
// TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6 // TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6
@ -21,7 +22,7 @@ export const globalNuxtCtx = getContext<Nuxt>('nuxt-global')
export function useNuxt (): Nuxt { export function useNuxt (): Nuxt {
const instance = getNuxtCtx().tryUse() const instance = getNuxtCtx().tryUse()
if (!instance) { if (!instance) {
const fallbackInstance = globalNuxtCtx.tryUse() const fallbackInstance = nuxtCtx.tryUse()
if (fallbackInstance) { if (fallbackInstance) {
logger.warn('Using fallback global Nuxt instance. You may be using a @nuxt/kit composable outside of a Nuxt context, this behavior is deprecated and will be removed in v4.') logger.warn('Using fallback global Nuxt instance. You may be using a @nuxt/kit composable outside of a Nuxt context, this behavior is deprecated and will be removed in v4.')
return fallbackInstance return fallbackInstance
@ -48,7 +49,7 @@ export function tryUseNuxt (): Nuxt | null {
const nuxt = getNuxtCtx().tryUse() const nuxt = getNuxtCtx().tryUse()
if (!nuxt) { if (!nuxt) {
logger.warn('Using fallback global Nuxt instance. You may be using a @nuxt/kit composable outside of a Nuxt context, this behavior is deprecated and will be removed in v4.') logger.warn('Using fallback global Nuxt instance. You may be using a @nuxt/kit composable outside of a Nuxt context, this behavior is deprecated and will be removed in v4.')
return globalNuxtCtx.tryUse() return nuxtCtx.tryUse()
} }
return nuxt return nuxt
} }

View File

@ -18,7 +18,7 @@ export type { ExtendConfigOptions, ExtendViteConfigOptions, ExtendWebpackConfigO
export { assertNuxtCompatibility, checkNuxtCompatibility, getNuxtVersion, hasNuxtCompatibility, isNuxtMajorVersion, normalizeSemanticVersion, isNuxt2, isNuxt3 } from './compatibility' export { assertNuxtCompatibility, checkNuxtCompatibility, getNuxtVersion, hasNuxtCompatibility, isNuxtMajorVersion, normalizeSemanticVersion, isNuxt2, isNuxt3 } from './compatibility'
export { addComponent, addComponentsDir } from './components' export { addComponent, addComponentsDir } from './components'
export type { AddComponentOptions } from './components' export type { AddComponentOptions } from './components'
export { getNuxtCtx as nuxtCtx, tryUseNuxt, useNuxt, globalNuxtCtx } from './context' export { getNuxtCtx, tryUseNuxt, useNuxt, nuxtCtx } from './context'
export { isIgnored, resolveIgnorePatterns } from './ignore' export { isIgnored, resolveIgnorePatterns } from './ignore'
export { addLayout } from './layout' export { addLayout } from './layout'
export { addRouteMiddleware, extendPages, extendRouteRules } from './pages' export { addRouteMiddleware, extendPages, extendRouteRules } from './pages'

View File

@ -4,7 +4,7 @@ import { join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable' import { createDebugger, createHooks } from 'hookable'
import ignore from 'ignore' import ignore from 'ignore'
import type { LoadNuxtOptions } from '@nuxt/kit' import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, asyncNameStorage, globalNuxtCtx, installModule, loadNuxtConfig, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, asyncNameStorage, nuxtCtx, installModule, loadNuxtConfig, getNuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema' import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema'
import type { PackageJson } from 'pkg-types' import type { PackageJson } from 'pkg-types'
import { readPackageJSON } from 'pkg-types' import { readPackageJSON } from 'pkg-types'
@ -175,17 +175,17 @@ async function initNuxt (nuxt: Nuxt) {
} }
} }
}) })
if (!globalNuxtCtx.tryUse()) { if (!nuxtCtx.tryUse()) {
// backward compatibility with 3.x // backward compatibility with 3.x
globalNuxtCtx.set(nuxt) nuxtCtx.set(nuxt)
nuxt.hook('close', () => { nuxt.hook('close', () => {
globalNuxtCtx.unset() nuxtCtx.unset()
}) })
} }
// Set nuxt instance for useNuxt // Set nuxt instance for useNuxt
nuxtCtx().set(nuxt) getNuxtCtx().set(nuxt)
nuxt.hook('close', () => { nuxt.hook('close', () => {
nuxtCtx().unset() getNuxtCtx().unset()
}) })
const coreTypePackages = nuxt.options.typescript.hoist || [] const coreTypePackages = nuxt.options.typescript.hoist || []