From 23bfeb937004d619187a67fb43e4c76b13d00069 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Fri, 28 Mar 2025 14:56:06 +0100 Subject: [PATCH] fix(vue): mock `nuxtApp.hooks` & `useRuntimeHook` --- package.json | 1 + pnpm-lock.yaml | 3 +++ src/runtime/vue/stubs.ts | 17 +++++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6b261462..79ca4952 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "embla-carousel-vue": "^8.5.2", "embla-carousel-wheel-gestures": "^8.0.1", "fuse.js": "^7.1.0", + "hookable": "^5.5.3", "knitwork": "^1.2.0", "magic-string": "^0.30.17", "mlly": "^1.7.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 85b303af..8bb8c974 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -94,6 +94,9 @@ importers: fuse.js: specifier: ^7.1.0 version: 7.1.0 + hookable: + specifier: ^5.5.3 + version: 5.5.3 joi: specifier: ^17.13.0 version: 17.13.3 diff --git a/src/runtime/vue/stubs.ts b/src/runtime/vue/stubs.ts index 5369b554..a2e35db4 100644 --- a/src/runtime/vue/stubs.ts +++ b/src/runtime/vue/stubs.ts @@ -1,5 +1,6 @@ -import { ref } from 'vue' +import { ref, onScopeDispose } from 'vue' import type { Ref, Plugin as VuePlugin } from 'vue' +import { createHooks } from 'hookable' import appConfig from '#build/app.config' import type { NuxtApp } from '#app' @@ -58,13 +59,25 @@ export const useState = (key: string, init: () => T): Ref => { return value as Ref } +const hooks = createHooks() + export function useNuxtApp() { return { isHydrating: true, - payload: { serverRendered: false } + payload: { serverRendered: false }, + hooks, + hook: hooks.hook } } +export function useRuntimeHook(name: string, fn: (...args: any[]) => void): void { + const nuxtApp = useNuxtApp() + + const unregister = nuxtApp.hook(name, fn) + + onScopeDispose(unregister) +} + export function defineNuxtPlugin(plugin: (nuxtApp: NuxtApp) => void) { return { install(app) {