Skip to content

Commit

Permalink
feat: throw error when maximumFileSizeToCacheInBytes found in sw bu…
Browse files Browse the repository at this point in the history
…ild warnings (#747)

* feat: throw error when `maximumFileSizeToCacheInBytes` found in sw build warnings

* chore: update error format
  • Loading branch information
userquin authored Aug 29, 2024
1 parent d27e46d commit 687a634
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/vue-router/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const pwaOptions: Partial<VitePWAOptions> = {
},
],
},
// showMaximumFileSizeToCacheInBytesWarning: true,
// workbox: {
// maximumFileSizeToCacheInBytes: 12000,
// },
devOptions: {
enabled: process.env.SW_DEV === 'true',
/* when using generateSW the PWA plugin will switch to classic */
Expand Down Expand Up @@ -81,6 +85,7 @@ if (process.env.SW === 'true') {
buildPlugins: {
vite: [virtualMessagePlugin()],
},
// maximumFileSizeToCacheInBytes: 1000,
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 6 additions & 1 deletion src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
integration = {},
buildBase,
pwaAssets,
showMaximumFileSizeToCacheInBytesWarning = false,
} = options

const basePath = resolveBasePath(base)
Expand Down Expand Up @@ -224,6 +225,7 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
envPrefix,
},
pwaAssets: resolvePWAAssetsOptions(pwaAssets),
throwMaximumFileSizeToCacheInBytes: !showMaximumFileSizeToCacheInBytesWarning,
}

// calculate hash only when required
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ export interface VitePWAOptions {
* @experimental
*/
pwaAssets?: PWAAssetsOptions

/**
* From version `0.20.2`, the plugin will throw an error if the `maximumFileSizeToCacheInBytes` warning is present when building the service worker.
*
* If you want the old behavior when building the service worker, set this option to `true`.
*
* @default false
*/
showMaximumFileSizeToCacheInBytesWarning?: boolean
}

export interface ResolvedServiceWorkerOptions {
Expand All @@ -401,7 +410,7 @@ export interface ResolvedServiceWorkerOptions {
rollupOptions: RollupOptions
}

export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'pwaAssets'>> {
export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'pwaAssets' | 'showMaximumFileSizeToCacheInBytesWarning'>> {
swSrc: string
swDest: string
workbox: GenerateSWOptions
Expand All @@ -421,6 +430,7 @@ export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'p
envPrefix: ResolvedConfig['envPrefix']
}
pwaAssets: false | ResolvedPWAAssetsOptions
throwMaximumFileSizeToCacheInBytes: boolean
}

export interface ShareTargetFiles {
Expand Down
8 changes: 7 additions & 1 deletion src/vite-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ export async function buildSW(
// inject the manifest
const buildResult = await injectManifest(injectManifestOptions)
// log workbox result
logWorkboxResult('injectManifest', buildResult, viteOptions, format)
logWorkboxResult(
options.throwMaximumFileSizeToCacheInBytes,
'injectManifest',
buildResult,
viteOptions,
format,
)
}

function prepareViteBuild(
Expand Down

0 comments on commit 687a634

Please sign in to comment.