From 1e3d989ad90e672e5bcb1e9f944b27b13fc42f3d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 2 Oct 2023 13:42:34 +0100 Subject: [PATCH] fix(nuxt): mock hookable methods on nuxt 2 (#23502) --- packages/kit/src/loader/nuxt.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/kit/src/loader/nuxt.ts b/packages/kit/src/loader/nuxt.ts index 722bc61eb7..84a46c0c0b 100644 --- a/packages/kit/src/loader/nuxt.ts +++ b/packages/kit/src/loader/nuxt.ts @@ -54,6 +54,19 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { 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 }