mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
fix(kit): ensure useNuxt
returns nuxt instance (#4059)
This commit is contained in:
parent
45d5a878ad
commit
8a3d445bf8
@ -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>('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()
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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<OptionsT extends ModuleOptions> (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
|
||||
|
@ -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, string>): 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
|
||||
|
Loading…
Reference in New Issue
Block a user