trying to use vite-pwa for SW

This commit is contained in:
Henry Jameson 2026-06-02 22:14:13 +03:00
commit 8824049f28
6 changed files with 1499 additions and 52 deletions

View file

@ -3,6 +3,7 @@ import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import * as esbuild from 'esbuild'
import { build } from 'vite'
import { exactRegex } from '@rolldown/pluginutils'
import {
generateServiceWorkerMessages,
@ -185,27 +186,23 @@ export const buildSwPlugin = ({ swSrc, swDest }) => {
}
}
const swMessagesName = 'virtual:pleroma-fe/service_worker_messages'
const swMessagesNameResolved = '\0' + swMessagesName
export const swMessagesPlugin = () => {
const swMessagesName = 'virtual:pleroma-fe/service_worker_messages'
const swMessagesNameResolved = '\0' + swMessagesName
return {
name: 'sw-messages-plugin',
resolveId(id) {
if (id === swMessagesName) {
Object.values(i18nFiles).forEach((f) => {
this.addWatchFile(f)
})
resolveId: {
filter: { id: exactRegex(swMessagesName) },
handler() {
return swMessagesNameResolved
} else {
return null
}
},
async load(id) {
if (id === swMessagesNameResolved) {
load: {
filter: { id: exactRegex(swMessagesNameResolved) },
async handler () {
return await getSWMessagesAsText()
}
return null
},
}
}

View file

@ -97,6 +97,7 @@
"lodash": "4.17.21",
"msw": "2.10.5",
"nightwatch": "3.12.2",
"oxc": "^1.0.1",
"playwright": "1.57.0",
"postcss": "8.5.6",
"postcss-html": "^1.5.0",
@ -116,6 +117,7 @@
"stylelint-config-standard": "38.0.0",
"vite": "^8.0.0",
"vite-plugin-eslint2": "^5.1.0",
"vite-plugin-pwa": "^1.3.0",
"vite-plugin-stylelint": "^6.1.0",
"vitest": "^3.0.7",
"vue-eslint-parser": "10.2.0"

View file

@ -19,7 +19,7 @@ function getOrCreateServiceWorker() {
if (!isSWSupported()) return
const swType = process.env.HAS_MODULE_SERVICE_WORKER ? 'module' : 'classic'
return navigator.serviceWorker
.register('/sw-pleroma.js', { type: swType })
.register(import.meta.env.MODE === 'production' ? '/sw-pleroma.js' : '/dev-sw.js?dev-sw', { type: swType })
.catch((err) =>
console.error('Unable to get or create a service worker.', err),
)

View file

@ -1,7 +1,5 @@
/* eslint-env serviceworker */
import 'virtual:pleroma-fe/service_worker_env'
import { createI18n } from 'vue-i18n'
import { storage } from 'src/lib/storage.js'

View file

@ -11,12 +11,11 @@ import { configDefaults } from 'vitest/config'
import { getCommitHash } from './build/commit_hash.js'
import copyPlugin from './build/copy_plugin.js'
import emojisPlugin from './build/emojis_plugin.js'
import mswPlugin from './build/msw_plugin.js'
import {
buildSwPlugin,
devSwPlugin,
swMessagesPlugin,
} from './build/sw_plugin.js'
import { VitePWA } from 'vite-plugin-pwa'
const localConfigPath = '<projectRoot>/config/local.json'
const normalizeTarget = (target) => {
@ -110,7 +109,7 @@ export default defineConfig(async ({ mode, command }) => {
},
}
const swSrc = 'src/sw.js'
const swSrc = 'src/sw-pleroma.js'
const swDest = 'sw-pleroma.js'
const alias = {
src: '/src',
@ -142,9 +141,6 @@ export default defineConfig(async ({ mode, command }) => {
// outDir: 'custom-dir', // optional, defaults to Vite's build.outDir
},
}),
devSwPlugin({ swSrc, swDest, transformSW, alias }),
buildSwPlugin({ swSrc, swDest }),
swMessagesPlugin(),
emojisPlugin(),
copyPlugin({
inUrl: '/static/ruffle',
@ -163,7 +159,24 @@ export default defineConfig(async ({ mode, command }) => {
'node_modules/.cache/stylelintcache',
),
}),
...(mode === 'test' ? [mswPlugin()] : []),
swMessagesPlugin(),
VitePWA({
strategies: 'injectManifest',
srcDir: 'src',
filename: 'sw-pleroma.js',
manifest: false,
injectRegister: null,
devOptions: {
enabled: true,
type: 'classic',
},
injectManifest: {
injectionPoint: undefined,
buildPlugins: {
vite: [swMessagesPlugin()],
},
}
}),
],
optimizeDeps: {
// For unknown reasons, during vitest, vite will re-optimize the following

1493
yarn.lock

File diff suppressed because it is too large Load diff