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)
|
||||||
}
|
}
|
||||||
|
if (isClient) {
|
||||||
viteInlineConfig.plugins.push({
|
viteInlineConfig.plugins.push({
|
||||||
name: 'elk:pwa:locales:build',
|
name: 'elk:pwa:locales:build',
|
||||||
apply: 'build',
|
apply: 'build',
|
||||||
generateBundle(_, bundle) {
|
async writeBundle(_options, bundle) {
|
||||||
if (options.disable || !bundle)
|
if (options.disable || !bundle)
|
||||||
return
|
return
|
||||||
|
await mkdir(manifestDir, { recursive: true })
|
||||||
Object.keys(webmanifests!).map(wm => [wm, `manifest-${wm}.webmanifest`]).forEach(([wm, fileName]) => {
|
for (const wm in webmanifests)
|
||||||
bundle[fileName] = {
|
await writeFile(join(manifestDir, `manifest-${wm}.webmanifest`), generateManifest(wm))
|
||||||
needsCodeReference: false,
|
|
||||||
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