refactor: inject pwa client plugin from module (#1758)

This commit is contained in:
Daniel Roe 2023-02-16 09:23:26 +00:00 committed by GitHub
parent 523578ba7b
commit 436489461c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 7 deletions

View file

@ -4,7 +4,7 @@ export function useWebShareTarget(listener?: (message: MessageEvent) => void) {
onBeforeMount(() => { onBeforeMount(() => {
// PWA must be installed to use share target // PWA must be installed to use share target
if (useNuxtApp().$pwa.isInstalled && 'serviceWorker' in navigator) { if (useNuxtApp().$pwa?.isInstalled && 'serviceWorker' in navigator) {
if (listener) if (listener)
navigator.serviceWorker.addEventListener('message', listener) navigator.serviceWorker.addEventListener('message', listener)

View file

@ -1,5 +1,5 @@
import { mkdir, writeFile } from 'node:fs/promises' 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 type { VitePluginPWAAPI } from 'vite-plugin-pwa'
import { VitePWA } from 'vite-plugin-pwa' import { VitePWA } from 'vite-plugin-pwa'
import type { Plugin } from 'vite' import type { Plugin } from 'vite'
@ -19,6 +19,8 @@ export default defineNuxtModule<VitePWANuxtOptions>({
scope: nuxt.options.app.baseURL, scope: nuxt.options.app.baseURL,
}), }),
async setup(options, nuxt) { async setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
let vitePwaClientPlugin: Plugin | undefined let vitePwaClientPlugin: Plugin | undefined
const resolveVitePluginPWAAPI = (): VitePluginPWAAPI | undefined => { const resolveVitePluginPWAAPI = (): VitePluginPWAAPI | undefined => {
return vitePwaClientPlugin?.api return vitePwaClientPlugin?.api
@ -35,6 +37,19 @@ export default defineNuxtModule<VitePWANuxtOptions>({
baseURL: '/', baseURL: '/',
maxAge: 0, 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? // TODO: combine with configurePWAOptions?
nuxt.hook('nitro:init', (nitro) => { nuxt.hook('nitro:init', (nitro) => {
options.outDir = nitro.options.output.publicDir options.outDir = nitro.options.output.publicDir

View file

@ -0,0 +1,7 @@
export default defineNuxtPlugin(() => {
return {
provide: {
pwa: {},
},
}
})

View file

@ -133,6 +133,9 @@ export default defineNuxtConfig({
}, },
sourcemap: isDevelopment, sourcemap: isDevelopment,
hooks: { hooks: {
'prepare:types': function ({ references }) {
references.push({ types: '@types/wicg-file-system-access' })
},
'nitro:config': function (config) { 'nitro:config': function (config) {
const nuxt = useNuxt() const nuxt = useNuxt()
config.virtual = config.virtual || {} config.virtual = config.virtual || {}

View file

@ -8,7 +8,7 @@ definePageMeta({
useWebShareTarget() useWebShareTarget()
const pwaIsInstalled = process.server ? false : useNuxtApp().$pwa.isInstalled const pwaIsInstalled = process.client && !!useNuxtApp().$pwa?.isInstalled
</script> </script>
<template> <template>

4
shims.d.ts vendored
View file

@ -1,7 +1,3 @@
/// <reference types="@types/wicg-file-system-access" />
/// <reference types="vite-plugin-pwa/info" />
/// <reference types="vite-plugin-pwa/client" />
declare global { declare global {
namespace NodeJS { namespace NodeJS {
interface Process { interface Process {