From 7ffeb440a7a403561b258910f3fa3257d1313ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Thu, 14 Mar 2024 12:53:59 +0100 Subject: [PATCH] fix: pwa assets disabled when using inlined configuration (#685) * fix: pwa assets disabled when using inlined configuration * chore: fix lint --- examples/assets-generator/package.json | 10 ++++++++-- examples/assets-generator/vite.config.ts | 15 +++++++++++---- examples/vanilla-js-custom-sw/package.json | 4 ++-- package.json | 22 +++++++++++----------- src/pwa-assets/options.ts | 12 +++++++----- 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/examples/assets-generator/package.json b/examples/assets-generator/package.json index 6d649d33..18d7ec1b 100644 --- a/examples/assets-generator/package.json +++ b/examples/assets-generator/package.json @@ -9,16 +9,22 @@ "preview": "vite preview", "dev:png": "PNG=true vite", "build:png": "PNG=true vite build", - "preview:png": "PNG=true vite preview" + "preview:png": "PNG=true vite preview", + "dev:inline:svg": "INLINE_PWA_ASSETS=true vite", + "build:inline:svg": "INLINE_PWA_ASSETS=true vite build", + "preview:inline:svg": "INLINE_PWA_ASSETS=true vite preview", + "dev:inline:png": "INLINE_PWA_ASSETS=true PNG=true vite", + "build:inline:png": "INLINE_PWA_ASSETS=true PNG=true vite build", + "preview:inline:png": "INLINE_PWA_ASSETS=true PNG=true vite preview" }, "devDependencies": { + "@vite-pwa/assets-generator": "^0.2.4", "rimraf": "^5.0.5", "sharp": "^0.32.6", "sharp-ico": "^0.1.5", "typescript": "^5.3.3", "vite": "^5.0.10", "vite-plugin-pwa": "workspace:*", - "@vite-pwa/assets-generator": "^0.2.4", "workbox-window": "^7.0.0" } } diff --git a/examples/assets-generator/vite.config.ts b/examples/assets-generator/vite.config.ts index 67ab148c..d1bdab5c 100644 --- a/examples/assets-generator/vite.config.ts +++ b/examples/assets-generator/vite.config.ts @@ -1,6 +1,16 @@ import { defineConfig } from 'vite' +import type { PWAAssetsOptions } from 'vite-plugin-pwa' import { VitePWA } from 'vite-plugin-pwa' +const pwaAssets: PWAAssetsOptions = process.env.INLINE_PWA_ASSETS + ? { + image: process.env.PNG ? 'public/source-test.png' : 'public/favicon.svg', + } + : { + config: true, + overrideManifestIcons: true, + } + export default defineConfig({ logLevel: 'info', define: { @@ -22,10 +32,7 @@ export default defineConfig({ short_name: 'Vite PWA', theme_color: '#ffffff', }, - pwaAssets: { - config: true, - overrideManifestIcons: true, - }, + pwaAssets, workbox: { globPatterns: ['**/*.{js,css,html,png,svg,ico}'], cleanupOutdatedCaches: true, diff --git a/examples/vanilla-js-custom-sw/package.json b/examples/vanilla-js-custom-sw/package.json index 47bb882c..1043b24a 100644 --- a/examples/vanilla-js-custom-sw/package.json +++ b/examples/vanilla-js-custom-sw/package.json @@ -1,8 +1,8 @@ { "name": "vanilla-js-custom-sw", - "private": true, - "version": "0.0.0", "type": "module", + "version": "0.0.0", + "private": true, "scripts": { "dev": "vite", "build": "vite build", diff --git a/package.json b/package.json index 4b989e0a..a0ab0953 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,17 @@ "test": "nr test-vue && nr test-react && nr test-preact && nr test-solid && nr test-svelte && nr test-typescript", "test:vite-ecosystem-ci": "nr test-typescript" }, + "peerDependencies": { + "@vite-pwa/assets-generator": "^0.2.4", + "vite": "^3.1.0 || ^4.0.0 || ^5.0.0", + "workbox-build": "^7.0.0", + "workbox-window": "^7.0.0" + }, + "peerDependenciesMeta": { + "@vite-pwa/assets-generator": { + "optional": true + } + }, "dependencies": { "debug": "^4.3.4", "fast-glob": "^3.3.2", @@ -141,16 +152,5 @@ "vite": "^5.0.12", "vitest": "1.0.0-beta.4", "vue": "^3.3.8" - }, - "peerDependencies": { - "@vite-pwa/assets-generator": "^0.2.4", - "vite": "^3.1.0 || ^4.0.0 || ^5.0.0", - "workbox-build": "^7.0.0", - "workbox-window": "^7.0.0" - }, - "peerDependenciesMeta": { - "@vite-pwa/assets-generator": { - "optional": true - } } } diff --git a/src/pwa-assets/options.ts b/src/pwa-assets/options.ts index 4b9a2959..1aaba88e 100644 --- a/src/pwa-assets/options.ts +++ b/src/pwa-assets/options.ts @@ -8,8 +8,6 @@ export function resolvePWAAssetsOptions( const { disabled: useDisabled, - config, - preset, image = 'public/favicon.svg', htmlPreset = '2023', overrideManifestIcons = false, @@ -18,12 +16,16 @@ export function resolvePWAAssetsOptions( integration, } = options ?? {} - const disabled = useDisabled || (!config && !preset) + const configIncluded = 'config' in options && options.config !== undefined && options.config + const presetIncluded = 'preset' in options && options.preset !== undefined && options.preset + const usePreset = !configIncluded && !presetIncluded ? 'minimal-2023' : false + + const disabled = useDisabled || (!configIncluded && !usePreset) return { disabled, - config: disabled || !config ? false : config, - preset: disabled || config ? false : preset ?? 'minimal-2023', + config: disabled || !configIncluded ? false : configIncluded, + preset: disabled || configIncluded ? false : usePreset, images: [image], htmlPreset, overrideManifestIcons,