1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-27 06:18:07 +09:00

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(() => {
// 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)

View File

@ -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<VitePWANuxtOptions>({
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<VitePWANuxtOptions>({
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

View File

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

View File

@ -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 || {}

View File

@ -8,7 +8,7 @@ definePageMeta({
useWebShareTarget()
const pwaIsInstalled = process.server ? false : useNuxtApp().$pwa.isInstalled
const pwaIsInstalled = process.client && !!useNuxtApp().$pwa?.isInstalled
</script>
<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 {
namespace NodeJS {
interface Process {