From 9155c32ece24f7e8f9e21a39ddd9b76b1a7e806e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sat, 9 Dec 2023 17:04:41 +0100 Subject: [PATCH] chore(pwa): improve DX for `$pwa` (#2498) --- modules/pwa/index.ts | 18 ++++++------ modules/pwa/runtime/pwa-plugin-stub.client.ts | 7 ----- modules/pwa/runtime/pwa-plugin.client.ts | 4 ++- modules/pwa/runtime/types.d.ts | 28 +++++++++++++++++++ 4 files changed, 40 insertions(+), 17 deletions(-) delete mode 100644 modules/pwa/runtime/pwa-plugin-stub.client.ts create mode 100644 modules/pwa/runtime/types.d.ts diff --git a/modules/pwa/index.ts b/modules/pwa/index.ts index 210b56a9..cb3c218c 100644 --- a/modules/pwa/index.ts +++ b/modules/pwa/index.ts @@ -50,15 +50,15 @@ export default defineNuxtModule({ baseURL: '/', maxAge: 0, }) - if (options.disable) { - addPlugin({ src: resolver.resolve('./runtime/pwa-plugin-stub.client') }) - } - else { - // Register PWA types - nuxt.hook('prepare:types', ({ references }) => { - references.push({ types: 'vite-plugin-pwa/info' }) - references.push({ types: 'vite-plugin-pwa/client' }) - }) + + // Register PWA types + nuxt.hook('prepare:types', ({ references }) => { + // TODO: remove this once JetBrains fixes the issue with types: remove also the dts file + references.push({ path: resolver.resolve('runtime/types') }) + references.push({ types: 'vite-plugin-pwa/info' }) + references.push({ types: 'vite-plugin-pwa/vue' }) + }) + if (!options.disable) { // Inject $pwa helper throughout app addPlugin({ src: resolver.resolve('./runtime/pwa-plugin.client') }) } diff --git a/modules/pwa/runtime/pwa-plugin-stub.client.ts b/modules/pwa/runtime/pwa-plugin-stub.client.ts deleted file mode 100644 index 243a34ef..00000000 --- a/modules/pwa/runtime/pwa-plugin-stub.client.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default defineNuxtPlugin(() => { - return { - provide: { - pwa: {}, - }, - } -}) diff --git a/modules/pwa/runtime/pwa-plugin.client.ts b/modules/pwa/runtime/pwa-plugin.client.ts index 912ed0fc..562894e3 100644 --- a/modules/pwa/runtime/pwa-plugin.client.ts +++ b/modules/pwa/runtime/pwa-plugin.client.ts @@ -1,4 +1,6 @@ import { useRegisterSW } from 'virtual:pwa-register/vue' +import type { UnwrapNestedRefs } from 'vue' +import type { PwaInjection } from './types' import { STORAGE_KEY_PWA_HIDE_INSTALL } from '~/constants' export default defineNuxtPlugin(() => { @@ -115,7 +117,7 @@ export default defineNuxtPlugin(() => { needRefresh, updateServiceWorker, close, - }), + }) satisfies UnwrapNestedRefs, }, } }) diff --git a/modules/pwa/runtime/types.d.ts b/modules/pwa/runtime/types.d.ts new file mode 100644 index 00000000..449e50e6 --- /dev/null +++ b/modules/pwa/runtime/types.d.ts @@ -0,0 +1,28 @@ +import type { Ref } from 'vue' +import type { UnwrapNestedRefs } from 'vue' + +export interface PwaInjection { + isInstalled: boolean + showInstallPrompt: Ref + cancelInstall: () => void + install: () => Promise + swActivated: Ref + registrationError: Ref + needRefresh: Ref + updateServiceWorker: (reloadPage?: boolean | undefined) => Promise + close: () => Promise +} + +declare module '#app' { + interface NuxtApp { + $pwa?: UnwrapNestedRefs + } +} + +declare module '@vue/runtime-core' { + interface ComponentCustomProperties { + $pwa?: UnwrapNestedRefs + } +} + +export {}