Skip to content

Commit

Permalink
feat: Allow suppress workbox-build warnings in dev (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Jun 2, 2023
1 parent 190ad03 commit 15b143f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/vue-router/vite.config.ts
Expand Up @@ -36,6 +36,7 @@ const pwaOptions: Partial<VitePWAOptions> = {
/* when using generateSW the PWA plugin will switch to classic */
type: 'module',
navigateFallback: 'index.html',
suppressWarnings: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/build.ts
@@ -1,4 +1,4 @@
import { execSync } from 'child_process'
import { execSync } from 'node:child_process'
import { commands } from './commands'

for (const command of commands)
Expand Down
2 changes: 1 addition & 1 deletion scripts/dev.ts
@@ -1,4 +1,4 @@
import { spawn } from 'child_process'
import { spawn } from 'node:child_process'
import { commands } from './commands'

for (const command of commands)
Expand Down
2 changes: 1 addition & 1 deletion scripts/run-examples.ts
@@ -1,4 +1,4 @@
import { execSync } from 'child_process'
import { execSync } from 'node:child_process'
import prompts from 'prompts'
import {
blue,
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Expand Up @@ -54,7 +54,7 @@ export async function resolveOptions(options: Partial<VitePWAOptions>, viteConfi
includeManifestIcons = true,
useCredentials = false,
disable = false,
devOptions = { enabled: false, type: 'classic' },
devOptions = { enabled: false, type: 'classic', suppressWarnings: false },
selfDestroying = false,
integration = {},
buildBase,
Expand Down
28 changes: 24 additions & 4 deletions src/plugins/dev.ts
Expand Up @@ -120,6 +120,15 @@ export function DevPlugin(ctx: PWAPluginContext) {

const swDest = resolve(globDirectory, 'sw.js')
if (!swDevOptions.swDevGenerated || !existsSync(swDest)) {
// add empty js file to suppress workbox-build warnings
let suppressWarnings: string | undefined
if (options.devOptions.suppressWarnings === true) {
suppressWarnings = normalizePath(resolve(globDirectory, 'suppress-warnings.js'))
await fs.writeFile(suppressWarnings, '', 'utf-8')
}
const globPatterns = options.devOptions.suppressWarnings === true
? ['*.js']
: options.workbox.globPatterns
// we only need to generate sw on dev-dist folder and then read the content
// the sw precache (self.__SW_MANIFEST) will be empty since we're using `dev-dist` folder
// we only need to add the navigateFallback if configured
Expand All @@ -134,11 +143,16 @@ export function DevPlugin(ctx: PWAPluginContext) {
...options.workbox,
navigateFallbackAllowlist: options.devOptions.navigateFallbackAllowlist ?? [/^\/$/],
runtimeCaching: options.devOptions.disableRuntimeConfig ? undefined : options.workbox.runtimeCaching,
// we only include navigateFallback
additionalManifestEntries: navigateFallback ? [navigateFallback] : undefined,
// we only include navigateFallback: add revision to remove workbox-build warning
additionalManifestEntries: navigateFallback
? [{
url: navigateFallback, revision: Math.random().toString(32),
}]
: undefined,
cleanupOutdatedCaches: true,
globDirectory: globDirectory.replace(/\\/g, '/'),
swDest: swDest.replace(/\\/g, '/'),
globDirectory: normalizePath(globDirectory),
globPatterns,
swDest: normalizePath(swDest),
},
},
),
Expand All @@ -151,6 +165,12 @@ export function DevPlugin(ctx: PWAPluginContext) {
if (name !== 'sw.js')
swDevOptions.workboxPaths.set(normalizePath(`${options.base}${name}`), we)
})
if (suppressWarnings) {
swDevOptions.workboxPaths.set(
normalizePath(`${options.base}${basename(suppressWarnings)}`),
suppressWarnings,
)
}
swDevOptions.swDevGenerated = true
}
return await fs.readFile(swDest, 'utf-8')
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Expand Up @@ -465,6 +465,17 @@ export interface DevOptions {
* Where to store generated service worker in development when using `generateSW` strategy.
*
* Use it with caution, it should be used only by framework integrations.
*
* @default resolve(viteConfig.root, 'dev-dist')
*/
resolveTempFolder?: () => string | Promise<string>
/**
* Suppress workbox-build warnings?.
*
* **WARNING**: this option will only be used when using `generateSW` strategy.
* If enabled, `globPatterns` will be changed to `[*.js]` and a new empty `suppress-warnings.js` file will be created in .
*
* @default false
*/
suppressWarnings?: boolean
}

0 comments on commit 15b143f

Please sign in to comment.