Open o-alexandrov opened 1 year ago
@o-alexandrov I'm missing with this issue: enabling devOptions, just registers a Vite plugin for dev, any plugin can apply any transformation to the custom sw, on closed issue I show you a custom SW using import.meta.env
(define is not just a plugin https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/define.ts ?).
@o-alexandrov on dev it works:
@userquin
on closed issue I show you a custom SW using import.meta.env
In that issue I asked: how would you use import.meta.env
to modify code you don't manage? (ex. that comes from dependencies; and obviously it's impossible w/ import.meta.env.*
, this question is just to draw attention to such case)
sw.ts
of the reproducible exampleAdded a repo that showcases a couple of possible scenarios when this issue becomes a bug for a user:
resolve
option and a separate example for define
option
npm run start
to run dev mode, and npm run start:prod
to run in prod mode after buildIt's worth noting:
injectManifest
scenario
vite-plugin-pwa
registers itself (i.e. generateSW
strategy), it does apply vite's plugins@o-alexandrov can you send a PR with your proposal?
I guess we need to add:
injectManifest
strategyThank you for supporting this change. I'll create a PR in the next few days then based on the points you shared
I’m sorry, I can’t dedicate to this task anymore as requirements changed at my work. Closing this issue to not pollute the list of open issues here.
Is this work being picked up anytime soon? Are there workarounds?
@userquin This issue has nothing to do with #506, as #506 doesn't have any changes to the dev
mode, does it?
generateInjectManifest
isn't used in dev
mode, so no vite
plugins are applied.
When I tried to use vite-plugin-pwa@0.15.0
, process.env.CUSTOM_VALUE
is still undefined when running locally, meaning dev mode still doesn't use any plugins
& define
from vite config.
build
mode, while making no changes to the dev
So, now the problem is in dev mode, I Will check your repo and try to fix it.
The new custom sw build has been included to fix vitepress builds, since pwa plugin was creating weird sw code. Same problem with sveltekit integration (now sveltekit build the sw).
Can you duplicate plugins in app and pwa options?
Related links
prod
, but notdev
, resulting in completely different output (unrelated to minification) in dev/prod for some users ofvite-plugin-pwa
, making it dangerous to rely ondev
modeWhy not using
vite
's plugins in dev mode is dangerousBy not applying any vite plugins, the service worker cannot reuse all these plugins' configurations that a user might have in own
vite.config.ts
, for example:define
w/ variables replacement (usingimport.meta.env.*
is not a sufficient alternative, as you can't replace values from the code you don't manage by usingimport.meta.env.*
)esbuild
plugin'starget
setting, orjsx
resolve
w/ pointers for different libraries, ex.lodash
&lodash-es
Reproducible example
Click to see code snippets from the demo repo above
```ts // src/index.ts - entry file for index.html if (`serviceWorker` in navigator) { void navigator.serviceWorker.register( import.meta.env.MODE === `production` ? `/sw.js` : `/dev-sw.js?dev-sw`, { type: import.meta.env.MODE === `production` ? `classic` : `module` } ); } ``` ```ts // src/sw.ts - service worker ///Alternative plugins or how to overcome this issue
It's possible to use vite-plugin-native-sw that has working
dev & prod
modesvite-plugin-pwa
has