2022-12-18 02:29:16 +03:00
|
|
|
import { defineNuxtModule } from '@nuxt/kit'
|
2023-01-06 15:48:43 +03:00
|
|
|
import type { VitePluginPWAAPI } from 'vite-plugin-pwa'
|
2022-12-18 02:29:16 +03:00
|
|
|
import { VitePWA } from 'vite-plugin-pwa'
|
|
|
|
import type { Plugin } from 'vite'
|
|
|
|
import type { VitePWANuxtOptions } from './types'
|
|
|
|
import { configurePWAOptions } from './config'
|
2023-01-06 15:48:43 +03:00
|
|
|
import { type LocalizedWebManifest, createI18n, pwaLocales } from './i18n'
|
2022-12-18 02:29:16 +03:00
|
|
|
|
|
|
|
export * from './types'
|
|
|
|
export default defineNuxtModule<VitePWANuxtOptions>({
|
|
|
|
meta: {
|
|
|
|
name: 'pwa',
|
|
|
|
configKey: 'pwa',
|
|
|
|
},
|
|
|
|
defaults: nuxt => ({
|
|
|
|
base: nuxt.options.app.baseURL,
|
|
|
|
scope: nuxt.options.app.baseURL,
|
|
|
|
}),
|
|
|
|
async setup(options, nuxt) {
|
|
|
|
let vitePwaClientPlugin: Plugin | undefined
|
|
|
|
const resolveVitePluginPWAAPI = (): VitePluginPWAAPI | undefined => {
|
|
|
|
return vitePwaClientPlugin?.api
|
|
|
|
}
|
2023-01-06 15:48:43 +03:00
|
|
|
let webmanifests: LocalizedWebManifest | undefined
|
2022-12-18 02:29:16 +03:00
|
|
|
|
2023-01-29 17:18:27 +03:00
|
|
|
nuxt.options.appConfig = nuxt.options.appConfig || {}
|
|
|
|
nuxt.options.appConfig.pwaEnabled = !options.disable
|
|
|
|
|
2022-12-18 02:29:16 +03:00
|
|
|
// TODO: combine with configurePWAOptions?
|
|
|
|
nuxt.hook('nitro:init', (nitro) => {
|
|
|
|
options.outDir = nitro.options.output.publicDir
|
|
|
|
options.injectManifest = options.injectManifest || {}
|
|
|
|
options.injectManifest.globDirectory = nitro.options.output.publicDir
|
|
|
|
})
|
|
|
|
nuxt.hook('vite:extend', ({ config }) => {
|
|
|
|
const plugin = config.plugins?.find(p => p && typeof p === 'object' && 'name' in p && p.name === 'vite-plugin-pwa')
|
|
|
|
if (plugin)
|
|
|
|
throw new Error('Remove vite-plugin-pwa plugin from Vite Plugins entry in Nuxt config file!')
|
|
|
|
})
|
2023-01-04 16:26:30 +03:00
|
|
|
nuxt.hook('vite:extendConfig', async (viteInlineConfig, { isClient }) => {
|
2022-12-18 02:29:16 +03:00
|
|
|
viteInlineConfig.plugins = viteInlineConfig.plugins || []
|
|
|
|
const plugin = viteInlineConfig.plugins.find(p => p && typeof p === 'object' && 'name' in p && p.name === 'vite-plugin-pwa')
|
|
|
|
if (plugin)
|
|
|
|
throw new Error('Remove vite-plugin-pwa plugin from Vite Plugins entry in Nuxt config file!')
|
2023-01-06 15:48:43 +03:00
|
|
|
|
|
|
|
webmanifests = await createI18n()
|
2023-01-10 23:35:34 +03:00
|
|
|
const generateManifest = (entry: string) => {
|
|
|
|
const manifest = webmanifests![entry]
|
2023-01-06 15:48:43 +03:00
|
|
|
if (!manifest)
|
2023-01-10 23:35:34 +03:00
|
|
|
throw new Error(`No webmanifest found for locale/theme ${entry}`)
|
2023-01-06 15:48:43 +03:00
|
|
|
return JSON.stringify(manifest)
|
2023-01-04 16:26:30 +03:00
|
|
|
}
|
2023-01-06 15:48:43 +03:00
|
|
|
viteInlineConfig.plugins.push({
|
|
|
|
name: 'elk:pwa:locales:build',
|
|
|
|
apply: 'build',
|
|
|
|
generateBundle(_, bundle) {
|
|
|
|
if (options.disable || !bundle)
|
|
|
|
return
|
|
|
|
|
2023-01-10 23:35:34 +03:00
|
|
|
Object.keys(webmanifests!).map(wm => [wm, `manifest-${wm}.webmanifest`]).forEach(([wm, fileName]) => {
|
2023-01-06 15:48:43 +03:00
|
|
|
bundle[fileName] = {
|
2023-01-24 22:05:50 +03:00
|
|
|
isAsset: true,
|
2023-01-06 15:48:43 +03:00
|
|
|
type: 'asset',
|
|
|
|
name: undefined,
|
2023-01-10 23:35:34 +03:00
|
|
|
source: generateManifest(wm),
|
2023-01-06 15:48:43 +03:00
|
|
|
fileName,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
viteInlineConfig.plugins.push({
|
|
|
|
name: 'elk:pwa:locales:dev',
|
|
|
|
apply: 'serve',
|
|
|
|
configureServer(server) {
|
|
|
|
const localeMatcher = new RegExp(`^${nuxt.options.app.baseURL}manifest-(.*).webmanifest$`)
|
|
|
|
server.middlewares.use((req, res, next) => {
|
|
|
|
const match = req.url?.match(localeMatcher)
|
|
|
|
const entry = match && webmanifests![match[1]]
|
|
|
|
if (entry) {
|
|
|
|
res.statusCode = 200
|
|
|
|
res.setHeader('Content-Type', 'application/manifest+json')
|
|
|
|
res.write(JSON.stringify(entry), 'utf-8')
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
configurePWAOptions(options, nuxt)
|
|
|
|
const plugins = VitePWA(options)
|
2022-12-18 02:29:16 +03:00
|
|
|
viteInlineConfig.plugins.push(plugins)
|
|
|
|
if (isClient)
|
|
|
|
vitePwaClientPlugin = plugins.find(p => p.name === 'vite-plugin-pwa') as Plugin
|
|
|
|
})
|
|
|
|
|
|
|
|
if (nuxt.options.dev) {
|
|
|
|
const webManifest = `${nuxt.options.app.baseURL}${options.devOptions?.webManifestUrl ?? options.manifestFilename ?? 'manifest.webmanifest'}`
|
|
|
|
const devSw = `${nuxt.options.app.baseURL}dev-sw.js?dev-sw`
|
|
|
|
const workbox = `${nuxt.options.app.baseURL}workbox-`
|
|
|
|
// @ts-expect-error just ignore
|
|
|
|
const emptyHandle = (_req, _res, next) => {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
nuxt.hook('vite:serverCreated', (viteServer, { isServer }) => {
|
|
|
|
if (isServer)
|
|
|
|
return
|
|
|
|
|
|
|
|
viteServer.middlewares.stack.push({ route: webManifest, handle: emptyHandle })
|
2023-01-06 15:48:43 +03:00
|
|
|
if (webmanifests) {
|
2023-01-10 23:35:34 +03:00
|
|
|
Object.keys(webmanifests).forEach((wm) => {
|
2023-01-06 15:48:43 +03:00
|
|
|
viteServer.middlewares.stack.push({
|
2023-01-10 23:35:34 +03:00
|
|
|
route: `${nuxt.options.app.baseURL}manifest-${wm}.webmanifest`,
|
2023-01-06 15:48:43 +03:00
|
|
|
handle: emptyHandle,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-12-18 02:29:16 +03:00
|
|
|
viteServer.middlewares.stack.push({ route: devSw, handle: emptyHandle })
|
|
|
|
})
|
2023-01-06 15:48:43 +03:00
|
|
|
|
2022-12-18 02:29:16 +03:00
|
|
|
if (!options.strategies || options.strategies === 'generateSW') {
|
|
|
|
nuxt.hook('vite:serverCreated', (viteServer, { isServer }) => {
|
|
|
|
if (isServer)
|
|
|
|
return
|
|
|
|
|
|
|
|
viteServer.middlewares.stack.push({ route: workbox, handle: emptyHandle })
|
|
|
|
})
|
|
|
|
nuxt.hook('close', async () => {
|
|
|
|
// todo: cleanup dev-dist folder
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-01-06 15:48:43 +03:00
|
|
|
nuxt.hook('nitro:config', async (nitroConfig) => {
|
|
|
|
nitroConfig.routeRules = nitroConfig.routeRules || {}
|
|
|
|
for (const locale of pwaLocales) {
|
|
|
|
nitroConfig.routeRules![`/manifest-${locale.code}.webmanifest`] = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/manifest+json',
|
|
|
|
},
|
|
|
|
}
|
2023-01-10 23:35:34 +03:00
|
|
|
nitroConfig.routeRules![`/manifest-${locale.code}-dark.webmanifest`] = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/manifest+json',
|
|
|
|
},
|
|
|
|
}
|
2023-01-06 15:48:43 +03:00
|
|
|
}
|
|
|
|
})
|
2022-12-18 02:29:16 +03:00
|
|
|
nuxt.hook('nitro:init', (nitro) => {
|
|
|
|
nitro.hooks.hook('rollup:before', async () => {
|
|
|
|
await resolveVitePluginPWAAPI()?.generateSW()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|