Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw error when maximumFileSizeToCacheInBytes found in sw build warnings #747

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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