GeezerVison is showing gist.github.com — Vite Workbox injectManifest
Instantly share code, notes, and snippets.
Created
March 6, 2024 11:27
-
Clone this repository at <script src="https://gist.github.com/ncking/01ed0b8d0d9cbf18accddf65d296a1e1.js"></script>
-
Save ncking/01ed0b8d0d9cbf18accddf65d296a1e1 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/ncking/01ed0b8d0d9cbf18accddf65d296a1e1.js"></script>
Save ncking/01ed0b8d0d9cbf18accddf65d296a1e1 to your computer and use it in GitHub Desktop.
Vite Workbox injectManifest
|
import { basename } from 'node:path' |
|
import { injectManifest } from 'workbox-build' |
|
import { build } from 'vite' |
|
|
|
|
|
const publicPath = '/dist'; |
|
const outDir = `./public` |
|
const options = { //https://developer.chrome.com/docs/workbox/modules/workbox-build#type-InjectManifestOptions |
|
swDest: `${outDir}/service-worker.js`, |
|
swSrc: './service-worker/service-worker.js', |
|
injectionPoint: 'SERVICE_WORKER__MANIFEST', |
|
globDirectory: './public/dist' |
|
} |
|
const swName = basename(options.swDest) |
|
const config = { |
|
publicDir: false, |
|
configFile: false, |
|
define: { |
|
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) |
|
}, |
|
build: { |
|
target: 'esnext', |
|
emptyOutDir: false, |
|
manifest: false, |
|
sourcemap: false, |
|
minify: true, |
|
outDir, |
|
lib: { |
|
entry: options.swSrc, |
|
name: 'sw', |
|
formats: ['iife'] |
|
}, |
|
rollupOptions: { |
|
output: { |
|
entryFileNames: swName |
|
}, |
|
}, |
|
}, |
|
}; |
|
|
|
await build(config); |
|
|
|
const manifestTransforms = [(transformedManifest, transformParam) => { |
|
const manifest = transformedManifest.map(entry => { |
|
entry.revision = null // not having this property fills the console with warnings, NULL is OK |
|
delete entry.size |
|
entry.url = `${publicPath}/${entry.url}` |
|
return entry |
|
}) |
|
return { manifest } |
|
}] |
|
|
|
await injectManifest({ ...options, swSrc: options.swDest, manifestTransforms }) // https://developer.chrome.com/docs/workbox/modules/workbox-build#type-WebpackInjectManifestOptions |
|
|