It's worth also adding "isolatedModules": false - otherwise you'll get warnings in VScode asking you to add export to your scripts, and then Chrome/Edge etc will complain with "Unexpected token 'export'".
// See comment at https://dev.to/wtho/custom-service-worker-logic-in-typescript-on-vite-4f27#comment-1n0i7
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": ["ESNext", "WebWorker"],
"noEmit": false,
"strict": false,
"outDir": "public",
"target": "ESNext",
// Makes script require 'export' and Chrome will fail to load extensions with
// "Unexpected token 'export'"
"isolatedModules": false
},
"include": ["service-worker/service-worker.ts"]
}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think it's important to note that this only works when running
vite build
and does not work with the dev server when runningvite
.I settled for a different approach which doesn't require
vite-plugin-pwa
and usestsc --watch
to run in parallel withvite
.I put the
sw.ts
file in thesw
dir to keep config separated betweentsconfig.sw.json
andtsconfig.json
files.This works a treat and will watch and emit the
sw/sw.ts
topublic\sw.js
for Vite dev server to use as a static asset.Just run
yarn dev
package.json
tsconfig.json
tsconfig.sw.json
You saved me a lot of time ❤️
This obviously won't trigger any sort of reload for the SW when the SW code changes, right?
It's worth also adding
"isolatedModules": false
- otherwise you'll get warnings in VScode asking you to addexport
to your scripts, and then Chrome/Edge etc will complain with"Unexpected token 'export'"
.