import { resolve } from 'path' ; import serveStatic from 'serve-static' ; import type { MetaOptions , ManifestOptions , IconOptions , PWAContext , WorkboxOptions } from '../types' ; import { PKG } from './utils' ; import { icon } from './icon' ; import { manifest } from './manifest' ; import { meta , metaRuntime } from './meta' ; import { workbox } from './workbox' ; interface PWAOptions { meta ? : Partial < MetaOptions > | false ; icon ? : Partial < IconOptions > | false ; workbox ? : Partial < WorkboxOptions > | false ; manifest ? : Partial < ManifestOptions > | false ; } export default async function pwa ( moduleOptions ) { const { nuxt } = this ; const moduleContainer = this ; const isBuild = nuxt . options . _build ; const isGenerate = nuxt . options . target === 'static' && ! nuxt . options . dev ; const isRuntime = ! isBuild && ! isGenerate ; if ( isRuntime ) { metaRuntime ( nuxt ) ; return ; } const modules = { icon , manifest , meta , workbox } ; nuxt . options . pwa = { ... ( nuxt . options . pwa || { } ) , ... ( moduleOptions || { } ) } ; const pwa = nuxt . options . pwa ; for ( const name in modules ) { if ( pwa [ name ] === false || nuxt . options [ name ] === false ) { continue ; } if ( pwa [ name ] === undefined ) { pwa [ name ] = { } ; } if ( nuxt . options [ name ] !== undefined ) { pwa [ name ] = { ... nuxt . options [ name ] , ... pwa [ name ] } ; } } for ( const name in modules ) { if ( pwa [ name ] === false ) { continue ; } await modules [ name ] ( nuxt , pwa , moduleContainer ) ; } if ( nuxt . options . dev ) { const clientDir = resolve ( nuxt . options . buildDir , 'dist/client' ) ; nuxt . options . serverMiddleware . push ( { path : nuxt . options . build . publicPath , handler : serveStatic ( clientDir ) } ) ; } } declare module '@nuxt/types/config/index' { interface NuxtOptions { pwa ? : Partial < PWAOptions > ; meta ? : Partial < MetaOptions > | false ; icon ? : Partial < IconOptions > | false ; workbox ? : Partial < WorkboxOptions > | false ; manifest ? : Partial < ManifestOptions > | false ; } } pwa . meta = PKG ;