Skip to content

Commit 687a634

Browse files
authored
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
1 parent d27e46d commit 687a634

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

examples/vue-router/vite.config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const pwaOptions: Partial<VitePWAOptions> = {
3535
},
3636
],
3737
},
38+
// showMaximumFileSizeToCacheInBytesWarning: true,
39+
// workbox: {
40+
// maximumFileSizeToCacheInBytes: 12000,
41+
// },
3842
devOptions: {
3943
enabled: process.env.SW_DEV === 'true',
4044
/* when using generateSW the PWA plugin will switch to classic */
@@ -81,6 +85,7 @@ if (process.env.SW === 'true') {
8185
buildPlugins: {
8286
vite: [virtualMessagePlugin()],
8387
},
88+
// maximumFileSizeToCacheInBytes: 1000,
8489
}
8590
}
8691

src/log.ts

+7
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ export function logSWViteBuild(
2626
}
2727

2828
export function logWorkboxResult(
29+
throwMaximumFileSizeToCacheInBytes: boolean,
2930
strategy: ResolvedVitePWAOptions['strategies'],
3031
buildResult: BuildResult,
3132
viteOptions: ResolvedConfig,
3233
format: 'es' | 'iife' | 'none' = 'none',
3334
) {
35+
if (throwMaximumFileSizeToCacheInBytes) {
36+
const entries = buildResult.warnings.filter(w => w.includes('maximumFileSizeToCacheInBytes'))
37+
if (entries.length)
38+
throw new Error(`\n${entries.map(w => ` - ${w}`).join('\n')}`)
39+
}
40+
3441
const { root, logLevel = 'info' } = viteOptions
3542

3643
if (logLevel === 'silent')

src/modules.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ self.addEventListener('activate', (e) => {
8282
// generate the service worker
8383
const buildResult = await generateSW(options.workbox)
8484
// log workbox result
85-
logWorkboxResult('generateSW', buildResult, viteOptions)
85+
logWorkboxResult(
86+
options.throwMaximumFileSizeToCacheInBytes,
87+
'generateSW',
88+
buildResult,
89+
viteOptions,
90+
)
8691

8792
return buildResult
8893
}

src/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
6262
integration = {},
6363
buildBase,
6464
pwaAssets,
65+
showMaximumFileSizeToCacheInBytesWarning = false,
6566
} = options
6667

6768
const basePath = resolveBasePath(base)
@@ -224,6 +225,7 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
224225
envPrefix,
225226
},
226227
pwaAssets: resolvePWAAssetsOptions(pwaAssets),
228+
throwMaximumFileSizeToCacheInBytes: !showMaximumFileSizeToCacheInBytesWarning,
227229
}
228230

229231
// calculate hash only when required

src/types.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,15 @@ export interface VitePWAOptions {
393393
* @experimental
394394
*/
395395
pwaAssets?: PWAAssetsOptions
396+
397+
/**
398+
* From version `0.20.2`, the plugin will throw an error if the `maximumFileSizeToCacheInBytes` warning is present when building the service worker.
399+
*
400+
* If you want the old behavior when building the service worker, set this option to `true`.
401+
*
402+
* @default false
403+
*/
404+
showMaximumFileSizeToCacheInBytesWarning?: boolean
396405
}
397406

398407
export interface ResolvedServiceWorkerOptions {
@@ -401,7 +410,7 @@ export interface ResolvedServiceWorkerOptions {
401410
rollupOptions: RollupOptions
402411
}
403412

404-
export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'pwaAssets'>> {
413+
export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'pwaAssets' | 'showMaximumFileSizeToCacheInBytesWarning'>> {
405414
swSrc: string
406415
swDest: string
407416
workbox: GenerateSWOptions
@@ -421,6 +430,7 @@ export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'p
421430
envPrefix: ResolvedConfig['envPrefix']
422431
}
423432
pwaAssets: false | ResolvedPWAAssetsOptions
433+
throwMaximumFileSizeToCacheInBytes: boolean
424434
}
425435

426436
export interface ShareTargetFiles {

src/vite-build.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ export async function buildSW(
7272
// inject the manifest
7373
const buildResult = await injectManifest(injectManifestOptions)
7474
// log workbox result
75-
logWorkboxResult('injectManifest', buildResult, viteOptions, format)
75+
logWorkboxResult(
76+
options.throwMaximumFileSizeToCacheInBytes,
77+
'injectManifest',
78+
buildResult,
79+
viteOptions,
80+
format,
81+
)
7682
}
7783

7884
function prepareViteBuild(

0 commit comments

Comments
 (0)