fix: use nitro `publicAssets` to emit manifests
parent
37036b4ca6
commit
54f020b165
|
@ -1,7 +1,9 @@
|
||||||
|
import { mkdir, writeFile } from 'node:fs/promises'
|
||||||
import { defineNuxtModule } from '@nuxt/kit'
|
import { 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'
|
||||||
|
import { join } from 'pathe'
|
||||||
import type { VitePWANuxtOptions } from './types'
|
import type { VitePWANuxtOptions } from './types'
|
||||||
import { configurePWAOptions } from './config'
|
import { configurePWAOptions } from './config'
|
||||||
import { type LocalizedWebManifest, createI18n, pwaLocales } from './i18n'
|
import { type LocalizedWebManifest, createI18n, pwaLocales } from './i18n'
|
||||||
|
@ -26,6 +28,13 @@ export default defineNuxtModule<VitePWANuxtOptions>({
|
||||||
nuxt.options.appConfig = nuxt.options.appConfig || {}
|
nuxt.options.appConfig = nuxt.options.appConfig || {}
|
||||||
nuxt.options.appConfig.pwaEnabled = !options.disable
|
nuxt.options.appConfig.pwaEnabled = !options.disable
|
||||||
|
|
||||||
|
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || []
|
||||||
|
const manifestDir = join(nuxt.options.buildDir, 'manifests')
|
||||||
|
nuxt.options.nitro.publicAssets.push({
|
||||||
|
dir: manifestDir,
|
||||||
|
baseURL: '/',
|
||||||
|
maxAge: 0,
|
||||||
|
})
|
||||||
// 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
|
||||||
|
@ -50,24 +59,19 @@ export default defineNuxtModule<VitePWANuxtOptions>({
|
||||||
throw new Error(`No webmanifest found for locale/theme ${entry}`)
|
throw new Error(`No webmanifest found for locale/theme ${entry}`)
|
||||||
return JSON.stringify(manifest)
|
return JSON.stringify(manifest)
|
||||||
}
|
}
|
||||||
viteInlineConfig.plugins.push({
|
if (isClient) {
|
||||||
name: 'elk:pwa:locales:build',
|
viteInlineConfig.plugins.push({
|
||||||
apply: 'build',
|
name: 'elk:pwa:locales:build',
|
||||||
generateBundle(_, bundle) {
|
apply: 'build',
|
||||||
if (options.disable || !bundle)
|
async writeBundle(_options, bundle) {
|
||||||
return
|
if (options.disable || !bundle)
|
||||||
|
return
|
||||||
Object.keys(webmanifests!).map(wm => [wm, `manifest-${wm}.webmanifest`]).forEach(([wm, fileName]) => {
|
await mkdir(manifestDir, { recursive: true })
|
||||||
bundle[fileName] = {
|
for (const wm in webmanifests)
|
||||||
needsCodeReference: false,
|
await writeFile(join(manifestDir, `manifest-${wm}.webmanifest`), generateManifest(wm))
|
||||||
type: 'asset',
|
},
|
||||||
name: undefined,
|
})
|
||||||
source: generateManifest(wm),
|
}
|
||||||
fileName,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
})
|
|
||||||
viteInlineConfig.plugins.push({
|
viteInlineConfig.plugins.push({
|
||||||
name: 'elk:pwa:locales:dev',
|
name: 'elk:pwa:locales:dev',
|
||||||
apply: 'serve',
|
apply: 'serve',
|
||||||
|
|
Loading…
Reference in New Issue