fix(nuxt): mock hookable methods on nuxt 2 (#23502)

This commit is contained in:
Daniel Roe 2023-10-02 13:42:34 +01:00 committed by GitHub
parent 770d4c67f7
commit 1e3d989ad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,6 +54,19 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
envConfig: opts.dotenv // TODO: Backward format conversion envConfig: opts.dotenv // TODO: Backward format conversion
}) })
// Mock new hookable methods
nuxt.removeHook ||= nuxt.clearHook.bind(nuxt)
nuxt.removeAllHooks ||= nuxt.clearHooks.bind(nuxt)
nuxt.hookOnce ||= (name: string, fn: (...args: any[]) => any, ...hookArgs: any[]) => {
const unsub = nuxt.hook(name, (...args: any[]) => {
unsub()
return fn(...args)
}, ...hookArgs)
return unsub
}
// https://github.com/nuxt/nuxt/tree/main/packages/kit/src/module/define.ts#L111-L113
nuxt.hooks ||= nuxt
return nuxt as Nuxt return nuxt as Nuxt
} }