From 687a634259ad3f30a2ab02043dae1b42a8474773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Thu, 29 Aug 2024 17:48:42 +0200 Subject: [PATCH] feat: throw error when `maximumFileSizeToCacheInBytes` found in sw build warnings (#747) * feat: throw error when `maximumFileSizeToCacheInBytes` found in sw build warnings * chore: update error format --- examples/vue-router/vite.config.ts | 5 +++++ src/log.ts | 7 +++++++ src/modules.ts | 7 ++++++- src/options.ts | 2 ++ src/types.ts | 12 +++++++++++- src/vite-build.ts | 8 +++++++- 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/examples/vue-router/vite.config.ts b/examples/vue-router/vite.config.ts index 00719547..c7913d1d 100644 --- a/examples/vue-router/vite.config.ts +++ b/examples/vue-router/vite.config.ts @@ -35,6 +35,10 @@ const pwaOptions: Partial = { }, ], }, + // showMaximumFileSizeToCacheInBytesWarning: true, + // workbox: { + // maximumFileSizeToCacheInBytes: 12000, + // }, devOptions: { enabled: process.env.SW_DEV === 'true', /* when using generateSW the PWA plugin will switch to classic */ @@ -81,6 +85,7 @@ if (process.env.SW === 'true') { buildPlugins: { vite: [virtualMessagePlugin()], }, + // maximumFileSizeToCacheInBytes: 1000, } } diff --git a/src/log.ts b/src/log.ts index 692f9fe1..64218f0f 100644 --- a/src/log.ts +++ b/src/log.ts @@ -26,11 +26,18 @@ export function logSWViteBuild( } export function logWorkboxResult( + throwMaximumFileSizeToCacheInBytes: boolean, strategy: ResolvedVitePWAOptions['strategies'], buildResult: BuildResult, viteOptions: ResolvedConfig, format: 'es' | 'iife' | 'none' = 'none', ) { + if (throwMaximumFileSizeToCacheInBytes) { + const entries = buildResult.warnings.filter(w => w.includes('maximumFileSizeToCacheInBytes')) + if (entries.length) + throw new Error(`\n${entries.map(w => ` - ${w}`).join('\n')}`) + } + const { root, logLevel = 'info' } = viteOptions if (logLevel === 'silent') diff --git a/src/modules.ts b/src/modules.ts index 34431b90..61e69373 100644 --- a/src/modules.ts +++ b/src/modules.ts @@ -82,7 +82,12 @@ self.addEventListener('activate', (e) => { // generate the service worker const buildResult = await generateSW(options.workbox) // log workbox result - logWorkboxResult('generateSW', buildResult, viteOptions) + logWorkboxResult( + options.throwMaximumFileSizeToCacheInBytes, + 'generateSW', + buildResult, + viteOptions, + ) return buildResult } diff --git a/src/options.ts b/src/options.ts index d50faea6..454774f5 100644 --- a/src/options.ts +++ b/src/options.ts @@ -62,6 +62,7 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise> { +export interface ResolvedVitePWAOptions extends Required> { swSrc: string swDest: string workbox: GenerateSWOptions @@ -421,6 +430,7 @@ export interface ResolvedVitePWAOptions extends Required