GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All gists
  • Back to GitHub
  • Sign in
  • Sign up
  • Code
  • Revisions 1
  • Terms
  • Privacy
  • Security
  • Status
  • Community
  • Docs
  • Contact

This view

  • Original page
  • New address

GeezerVison is showing gist.github.com — Vite Workbox injectManifest

Instantly share code, notes, and snippets.

@ncking

ncking/build-service-worker.js

Created March 6, 2024 11:27
Show Gist options
  • Download ZIP
  • Star 0 (0) You must be signed in to star a gist
  • Fork 0 (0) You must be signed in to fork a gist
  • 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.
Download ZIP
Vite Workbox injectManifest
Raw
build-service-worker.js
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.