fix(kit): ensure useNuxt returns nuxt instance (#4059)

This commit is contained in:
pooya parsa 2022-04-04 11:41:48 +02:00 committed by GitHub
parent 45d5a878ad
commit 8a3d445bf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 8 deletions

View File

@ -4,12 +4,39 @@ import type { Nuxt } from '@nuxt/schema'
/** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */ /** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */
export const nuxtCtx = getContext<Nuxt>('nuxt') export const nuxtCtx = getContext<Nuxt>('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 * @example
* ```js * ```js
* const nuxt = useNuxt() * 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()
}

View File

@ -1,13 +1,18 @@
import { existsSync, readFileSync } from 'fs' import { existsSync, readFileSync } from 'fs'
import ignore from 'ignore' import ignore from 'ignore'
import { join, relative } from 'pathe' import { join, relative } from 'pathe'
import { useNuxt } from './context' import { tryUseNuxt } from './context'
/** /**
* Return a filter function to filter an array of paths * Return a filter function to filter an array of paths
*/ */
export function isIgnored (pathname: string): boolean { export function isIgnored (pathname: string): boolean {
const nuxt = useNuxt() const nuxt = tryUseNuxt()
// Happens with CLI reloads
if (!nuxt) {
return null
}
if (!nuxt._ignore) { if (!nuxt._ignore) {
nuxt._ignore = ignore(nuxt.options.ignoreOptions) nuxt._ignore = ignore(nuxt.options.ignoreOptions)

View File

@ -4,7 +4,7 @@ import { applyDefaults } from 'untyped'
import { dirname } from 'pathe' import { dirname } from 'pathe'
import type { Nuxt, NuxtTemplate, NuxtModule, ModuleOptions, ModuleDefinition } from '@nuxt/schema' import type { Nuxt, NuxtTemplate, NuxtModule, ModuleOptions, ModuleDefinition } from '@nuxt/schema'
import { logger } from '../logger' import { logger } from '../logger'
import { useNuxt, nuxtCtx } from '../context' import { useNuxt, nuxtCtx, tryUseNuxt } from '../context'
import { isNuxt2, checkNuxtCompatibility } from '../compatibility' import { isNuxt2, checkNuxtCompatibility } from '../compatibility'
import { templateUtils, compileTemplate } from '../internal/template' import { templateUtils, compileTemplate } from '../internal/template'
@ -43,7 +43,7 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (definition: Mo
// Module format is always a simple function // Module format is always a simple function
async function normalizedModule (inlineOptions: OptionsT, nuxt: Nuxt) { async function normalizedModule (inlineOptions: OptionsT, nuxt: Nuxt) {
if (!nuxt) { if (!nuxt) {
nuxt = useNuxt() || this.nuxt /* invoked by nuxt 2 */ nuxt = tryUseNuxt() || this.nuxt /* invoked by nuxt 2 */
} }
// Avoid duplicate installs // Avoid duplicate installs

View File

@ -2,7 +2,7 @@ import { promises as fsp, existsSync } from 'fs'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { basename, dirname, resolve, join, normalize, isAbsolute } from 'pathe' import { basename, dirname, resolve, join, normalize, isAbsolute } from 'pathe'
import { globby } from 'globby' import { globby } from 'globby'
import { useNuxt } from './context' import { tryUseNuxt, useNuxt } from './context'
import { tryResolveModule } from './internal/cjs' import { tryResolveModule } from './internal/cjs'
import { isIgnored } from './ignore' 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, string>): string { export function resolveAlias (path: string, alias?: Record<string, string>): string {
if (!alias) { if (!alias) {
alias = useNuxt()?.options.alias || {} alias = tryUseNuxt()?.options.alias || {}
} }
for (const key in alias) { for (const key in alias) {
if (key === '@' && !path.startsWith('@/')) { continue } // Don't resolve @foo/bar if (key === '@' && !path.startsWith('@/')) { continue } // Don't resolve @foo/bar