diff --git a/packages/kit/src/context.ts b/packages/kit/src/context.ts index 05bd70db76..0ddbfdf159 100644 --- a/packages/kit/src/context.ts +++ b/packages/kit/src/context.ts @@ -4,12 +4,39 @@ import type { Nuxt } from '@nuxt/schema' /** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */ export const nuxtCtx = getContext('nuxt') +// TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6 + /** - * Get access to Nuxt (if run within the Nuxt context) - see https://github.com/unjs/unctx. + * Get access to Nuxt instance. + * + * Throws an error if Nuxt instance is unavailable. * * @example * ```js * const nuxt = useNuxt() * ``` */ -export const useNuxt = nuxtCtx.use +export function useNuxt (): Nuxt { + const instance = nuxtCtx.use() + if (!instance) { + throw new Error('Nuxt instance is unavailable!') + } + return instance +} + +/** + * Get access to Nuxt instance. + * + * Returns null if Nuxt instance is unavailable. + * + * @example + * ```js + * const nuxt = tryUseNuxt() + * if (nuxt) { + * // Do something + * } + * ``` + */ +export function tryUseNuxt (): Nuxt | null { + return nuxtCtx.use() +} diff --git a/packages/kit/src/ignore.ts b/packages/kit/src/ignore.ts index 2b348d5d02..022d7db28d 100644 --- a/packages/kit/src/ignore.ts +++ b/packages/kit/src/ignore.ts @@ -1,13 +1,18 @@ import { existsSync, readFileSync } from 'fs' import ignore from 'ignore' import { join, relative } from 'pathe' -import { useNuxt } from './context' +import { tryUseNuxt } from './context' /** * Return a filter function to filter an array of paths */ export function isIgnored (pathname: string): boolean { - const nuxt = useNuxt() + const nuxt = tryUseNuxt() + + // Happens with CLI reloads + if (!nuxt) { + return null + } if (!nuxt._ignore) { nuxt._ignore = ignore(nuxt.options.ignoreOptions) diff --git a/packages/kit/src/module/define.ts b/packages/kit/src/module/define.ts index b8da0c0780..48268f29a1 100644 --- a/packages/kit/src/module/define.ts +++ b/packages/kit/src/module/define.ts @@ -4,7 +4,7 @@ import { applyDefaults } from 'untyped' import { dirname } from 'pathe' import type { Nuxt, NuxtTemplate, NuxtModule, ModuleOptions, ModuleDefinition } from '@nuxt/schema' import { logger } from '../logger' -import { useNuxt, nuxtCtx } from '../context' +import { useNuxt, nuxtCtx, tryUseNuxt } from '../context' import { isNuxt2, checkNuxtCompatibility } from '../compatibility' import { templateUtils, compileTemplate } from '../internal/template' @@ -43,7 +43,7 @@ export function defineNuxtModule (definition: Mo // Module format is always a simple function async function normalizedModule (inlineOptions: OptionsT, nuxt: Nuxt) { if (!nuxt) { - nuxt = useNuxt() || this.nuxt /* invoked by nuxt 2 */ + nuxt = tryUseNuxt() || this.nuxt /* invoked by nuxt 2 */ } // Avoid duplicate installs diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 01b9410976..ee7be7b6ad 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -2,7 +2,7 @@ import { promises as fsp, existsSync } from 'fs' import { fileURLToPath } from 'url' import { basename, dirname, resolve, join, normalize, isAbsolute } from 'pathe' import { globby } from 'globby' -import { useNuxt } from './context' +import { tryUseNuxt, useNuxt } from './context' import { tryResolveModule } from './internal/cjs' import { isIgnored } from './ignore' @@ -103,7 +103,7 @@ export async function findPath (paths: string|string[], opts?: ResolvePathOption */ export function resolveAlias (path: string, alias?: Record): string { if (!alias) { - alias = useNuxt()?.options.alias || {} + alias = tryUseNuxt()?.options.alias || {} } for (const key in alias) { if (key === '@' && !path.startsWith('@/')) { continue } // Don't resolve @foo/bar