From 436489461c1c49b196e6a5b43271e4cf8bebb591 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 16 Feb 2023 09:23:26 +0000 Subject: [PATCH] refactor: inject pwa client plugin from module (#1758) --- composables/web-share-target.ts | 2 +- modules/pwa/index.ts | 17 ++++++++++++++++- modules/pwa/runtime/pwa-plugin-stub.client.ts | 7 +++++++ .../pwa/runtime/pwa-plugin.client.ts | 0 nuxt.config.ts | 3 +++ pages/share-target.vue | 2 +- shims.d.ts | 4 ---- 7 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 modules/pwa/runtime/pwa-plugin-stub.client.ts rename plugins/pwa.client.ts => modules/pwa/runtime/pwa-plugin.client.ts (100%) diff --git a/composables/web-share-target.ts b/composables/web-share-target.ts index c3adaca7..70772fc5 100644 --- a/composables/web-share-target.ts +++ b/composables/web-share-target.ts @@ -4,7 +4,7 @@ export function useWebShareTarget(listener?: (message: MessageEvent) => void) { onBeforeMount(() => { // PWA must be installed to use share target - if (useNuxtApp().$pwa.isInstalled && 'serviceWorker' in navigator) { + if (useNuxtApp().$pwa?.isInstalled && 'serviceWorker' in navigator) { if (listener) navigator.serviceWorker.addEventListener('message', listener) diff --git a/modules/pwa/index.ts b/modules/pwa/index.ts index f68b4fe6..1e76c32e 100644 --- a/modules/pwa/index.ts +++ b/modules/pwa/index.ts @@ -1,5 +1,5 @@ import { mkdir, writeFile } from 'node:fs/promises' -import { defineNuxtModule } from '@nuxt/kit' +import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit' import type { VitePluginPWAAPI } from 'vite-plugin-pwa' import { VitePWA } from 'vite-plugin-pwa' import type { Plugin } from 'vite' @@ -19,6 +19,8 @@ export default defineNuxtModule({ scope: nuxt.options.app.baseURL, }), async setup(options, nuxt) { + const resolver = createResolver(import.meta.url) + let vitePwaClientPlugin: Plugin | undefined const resolveVitePluginPWAAPI = (): VitePluginPWAAPI | undefined => { return vitePwaClientPlugin?.api @@ -35,6 +37,19 @@ 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' }) + }) + // Inject $pwa helper throughout app + addPlugin({ src: resolver.resolve('./runtime/pwa-plugin.client') }) + } + // TODO: combine with configurePWAOptions? nuxt.hook('nitro:init', (nitro) => { options.outDir = nitro.options.output.publicDir diff --git a/modules/pwa/runtime/pwa-plugin-stub.client.ts b/modules/pwa/runtime/pwa-plugin-stub.client.ts new file mode 100644 index 00000000..243a34ef --- /dev/null +++ b/modules/pwa/runtime/pwa-plugin-stub.client.ts @@ -0,0 +1,7 @@ +export default defineNuxtPlugin(() => { + return { + provide: { + pwa: {}, + }, + } +}) diff --git a/plugins/pwa.client.ts b/modules/pwa/runtime/pwa-plugin.client.ts similarity index 100% rename from plugins/pwa.client.ts rename to modules/pwa/runtime/pwa-plugin.client.ts diff --git a/nuxt.config.ts b/nuxt.config.ts index 6c0a2c16..f7aeaa5f 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -133,6 +133,9 @@ export default defineNuxtConfig({ }, sourcemap: isDevelopment, hooks: { + 'prepare:types': function ({ references }) { + references.push({ types: '@types/wicg-file-system-access' }) + }, 'nitro:config': function (config) { const nuxt = useNuxt() config.virtual = config.virtual || {} diff --git a/pages/share-target.vue b/pages/share-target.vue index 13b15425..0f7223b4 100644 --- a/pages/share-target.vue +++ b/pages/share-target.vue @@ -8,7 +8,7 @@ definePageMeta({ useWebShareTarget() -const pwaIsInstalled = process.server ? false : useNuxtApp().$pwa.isInstalled +const pwaIsInstalled = process.client && !!useNuxtApp().$pwa?.isInstalled