Skip to content

Commit

Permalink
fix!: use nuxt plugin template for pwa icons plugin (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Jul 3, 2024
1 parent 94d083f commit a5f401f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
34 changes: 0 additions & 34 deletions src/runtime/plugins/pwa-icons.mjs

This file was deleted.

5 changes: 2 additions & 3 deletions src/utils/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { PwaModuleOptions } from '../types'
import { configurePWAOptions } from './config'
import { regeneratePWA, writeWebManifest } from './utils'
import { registerPwaIconsTypes } from './pwa-icons-types'
import { addPWAIconsPluginTemplate } from './pwa-icons-helper'

export async function doSetup(options: PwaModuleOptions, nuxt: Nuxt) {
const resolver = createResolver(import.meta.url)
Expand Down Expand Up @@ -52,9 +53,7 @@ export async function doSetup(options: PwaModuleOptions, nuxt: Nuxt) {
})
}

addPlugin({
src: resolver.resolve(runtimeDir, 'plugins/pwa-icons.mjs'),
})
addPWAIconsPluginTemplate()

await Promise.all([
addComponent({
Expand Down
53 changes: 52 additions & 1 deletion src/utils/pwa-icons-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addTypeTemplate } from '@nuxt/kit'
import { addPluginTemplate, addTypeTemplate } from '@nuxt/kit'

export interface DtsInfo {
dts?: string
Expand Down Expand Up @@ -104,3 +104,54 @@ export default _default
function generateTypes(types?: string[]) {
return types?.length ? types.map(name => `'${name}'`).join(' | ') : 'string'
}

export function addPWAIconsPluginTemplate() {
addPluginTemplate({
filename: 'pwa-icons-plugin.ts',
name: 'vite-pwa:nuxt:pwa-icons-plugin',
write: true,
getContents: () => `// Generated by @vite-pwa/nuxt
import { defineNuxtPlugin } from '#imports'
import { pwaAssetsIcons } from 'virtual:pwa-assets/icons'
import type { PWAAssetIcon, PWAIcons } from '#build/pwa-icons/pwa-icons'
export default defineNuxtPlugin(() => {
const pwaIcons = {
transparent: {},
maskable: {},
favicon: {},
apple: {},
appleSplashScreen: {}
} satisfies PWAIcons
configureEntries(pwaIcons, 'transparent')
configureEntries(pwaIcons, 'maskable')
configureEntries(pwaIcons, 'favicon')
configureEntries(pwaIcons, 'apple')
configureEntries(pwaIcons, 'appleSplashScreen')
return {
provide: {
pwaIcons
}
}
})
function configureEntries(pwaIcons: PWAIcons, key: keyof PWAIcons) {
pwaIcons[key] = Object.values(pwaAssetsIcons[key] ?? {}).reduce((acc, icon) => {
const entry: PWAAssetIcon<any> = {
...icon,
asImage: {
src: icon.url,
key: \`\${key}-\${icon.name}\`
}
}
if (icon.width && icon.height) {
entry.asImage.width = icon.width
entry.asImage.height = icon.height
}
acc[icon.name] = entry
return acc
}, {})
}
`,
})
}

0 comments on commit a5f401f

Please sign in to comment.