Skip to content

Commit

Permalink
fix: split nuxt pwa icons plugin (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Jul 6, 2024
1 parent 8a7dfe6 commit bff9084
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/utils/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export async function doSetup(options: PwaModuleOptions, nuxt: Nuxt) {
})
}

addPWAIconsPluginTemplate()
const pwaAssetsEnabled = !!options.pwaAssets && options.pwaAssets.disabled !== true

addPWAIconsPluginTemplate(pwaAssetsEnabled)

await Promise.all([
addComponent({
Expand Down
72 changes: 47 additions & 25 deletions src/utils/pwa-icons-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,53 +105,75 @@ 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
export function addPWAIconsPluginTemplate(pwaAssetsEnabled: boolean) {
if (pwaAssetsEnabled) {
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
pwaIcons: {
transparent: configureEntry('transparent'),
maskable: configureEntry('maskable'),
favicon: configureEntry('favicon'),
apple: configureEntry('apple'),
appleSplashScreen: configureEntry('appleSplashScreen')
} satisfies PWAIcons
}
}
})
function configureEntries(pwaIcons: PWAIcons, key: keyof PWAIcons) {
pwaIcons[key] = Object.values(pwaAssetsIcons[key] ?? {}).reduce((acc, icon) => {
function configureEntry<K extends keyof PWAIcons>(key: K) {
return Object.values(pwaAssetsIcons[key] ?? {}).reduce((acc, icon) => {
const entry: PWAAssetIcon<any> = {
...icon,
asImage: {
src: icon.url,
key: \`\${key}-\${icon.name}\`
key: \`\${key}-$\{icon.name}\`
}
}
if (icon.width && icon.height) {
entry.asImage.width = icon.width
entry.asImage.height = icon.height
}
acc[icon.name] = entry
;(acc as unknown as any)[icon.name] = entry
return acc
}, {})
}, {} as PWAIcons[typeof key])
}
`,
})
})
}
else {
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 type { PWAAssetIcon, PWAIcons } from '#build/pwa-icons/pwa-icons'
export default defineNuxtPlugin(() => {
return {
provide: {
pwaIcons: {
transparent: {},
maskable: {},
favicon: {},
apple: {},
appleSplashScreen: {}
} satisfies PWAIcons
}
}
})
`,
})
}
}

0 comments on commit bff9084

Please sign in to comment.