2025-02-28 10:52:04 -05:00
|
|
|
import { readFile } from 'node:fs/promises'
|
2026-01-06 16:23:17 +02:00
|
|
|
import { dirname, resolve } from 'node:path'
|
|
|
|
|
import { fileURLToPath } from 'node:url'
|
2026-06-04 21:59:09 +03:00
|
|
|
import { exactRegex } from '@rolldown/pluginutils'
|
2026-01-06 16:23:17 +02:00
|
|
|
import { build } from 'vite'
|
2026-01-08 17:26:52 +02:00
|
|
|
|
2026-01-06 16:22:52 +02:00
|
|
|
import {
|
|
|
|
|
generateServiceWorkerMessages,
|
|
|
|
|
i18nFiles,
|
|
|
|
|
} from './service_worker_messages.js'
|
2025-02-28 10:52:04 -05:00
|
|
|
|
2025-02-28 13:29:27 -05:00
|
|
|
const getSWMessagesAsText = async () => {
|
|
|
|
|
const messages = await generateServiceWorkerMessages()
|
|
|
|
|
return `export default ${JSON.stringify(messages, undefined, 2)}`
|
|
|
|
|
}
|
2025-02-28 10:52:04 -05:00
|
|
|
const projectRoot = dirname(dirname(fileURLToPath(import.meta.url)))
|
|
|
|
|
|
2026-06-02 23:16:03 +03:00
|
|
|
// Idea taken from
|
|
|
|
|
// https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/plugins/build.ts
|
|
|
|
|
// rollup does not support compiling to iife if we want to code-split;
|
|
|
|
|
// however, we must compile the service worker to iife because of browser support.
|
|
|
|
|
// Run another vite build just for the service worker targeting iife at
|
|
|
|
|
// the end of the build.
|
|
|
|
|
export const buildSwPlugin = ({ swSrc, swDest }) => {
|
2026-06-04 17:47:07 +03:00
|
|
|
const swFullSrc = resolve(projectRoot, swSrc)
|
|
|
|
|
const swEnvName = 'virtual:pleroma-fe/service_worker_env'
|
|
|
|
|
const swEnvNameResolved = '\0' + swEnvName
|
|
|
|
|
|
2026-06-04 21:59:09 +03:00
|
|
|
let config
|
2026-06-04 17:47:07 +03:00
|
|
|
|
2026-06-02 23:16:03 +03:00
|
|
|
return {
|
|
|
|
|
name: 'build-sw-plugin',
|
|
|
|
|
enforce: 'post',
|
|
|
|
|
configResolved(resolvedConfig) {
|
2026-06-04 21:59:09 +03:00
|
|
|
resolvedConfig
|
2026-06-02 23:16:03 +03:00
|
|
|
config = {
|
|
|
|
|
define: resolvedConfig.define,
|
|
|
|
|
resolve: resolvedConfig.resolve,
|
|
|
|
|
plugins: [swMessagesPlugin()],
|
|
|
|
|
publicDir: false,
|
|
|
|
|
build: {
|
|
|
|
|
...resolvedConfig.build,
|
2026-06-03 00:14:11 +03:00
|
|
|
emptyOutDir: false,
|
2026-06-02 23:16:03 +03:00
|
|
|
rolldownOptions: {
|
2026-06-03 06:05:28 +03:00
|
|
|
input: {
|
2026-06-04 21:59:09 +03:00
|
|
|
main: swSrc,
|
2026-06-03 06:05:28 +03:00
|
|
|
},
|
|
|
|
|
context: 'self',
|
2026-06-02 23:16:03 +03:00
|
|
|
output: {
|
|
|
|
|
entryFileNames: swDest,
|
2026-06-03 06:05:28 +03:00
|
|
|
codeSplitting: false,
|
|
|
|
|
format: 'iife',
|
2026-06-04 21:59:09 +03:00
|
|
|
},
|
2026-06-02 23:16:03 +03:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
configFile: false,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
generateBundle: {
|
|
|
|
|
order: 'post',
|
|
|
|
|
sequential: true,
|
|
|
|
|
async handler(_, bundle) {
|
|
|
|
|
const assets = Object.keys(bundle)
|
|
|
|
|
.filter((name) => !/\.map$/.test(name))
|
|
|
|
|
.map((name) => '/' + name)
|
2026-06-04 17:47:07 +03:00
|
|
|
|
2026-06-02 23:16:03 +03:00
|
|
|
config.plugins.push({
|
|
|
|
|
name: 'build-sw-env-plugin',
|
2026-06-04 17:47:07 +03:00
|
|
|
mode: 'production',
|
|
|
|
|
resolveId: {
|
|
|
|
|
filter: { id: exactRegex(swEnvName) },
|
|
|
|
|
handler: () => swEnvNameResolved,
|
2026-06-02 23:16:03 +03:00
|
|
|
},
|
2026-06-04 17:47:07 +03:00
|
|
|
load: {
|
|
|
|
|
filter: { id: exactRegex(swEnvNameResolved) },
|
|
|
|
|
handler() {
|
|
|
|
|
return `self.serviceWorkerOption = { assets: ${JSON.stringify(assets)} };`
|
2026-06-04 21:59:09 +03:00
|
|
|
},
|
2026-06-02 23:16:03 +03:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-06-04 17:47:07 +03:00
|
|
|
resolveId: {
|
|
|
|
|
filter: { id: new RegExp(swDest) },
|
|
|
|
|
handler() {
|
|
|
|
|
return swFullSrc
|
2026-06-04 21:59:09 +03:00
|
|
|
},
|
2026-06-04 17:47:07 +03:00
|
|
|
},
|
|
|
|
|
load: {
|
|
|
|
|
filter: { id: new RegExp(swFullSrc) },
|
|
|
|
|
async handler() {
|
|
|
|
|
config.plugins.push({
|
|
|
|
|
name: 'dummy-sw-env',
|
|
|
|
|
mode: 'development',
|
|
|
|
|
resolveId: {
|
|
|
|
|
filter: { id: exactRegex(swEnvName) },
|
|
|
|
|
handler: () => swEnvNameResolved,
|
|
|
|
|
},
|
|
|
|
|
load: {
|
|
|
|
|
filter: { id: exactRegex(swEnvNameResolved) },
|
|
|
|
|
handler: () => 'self.serviceWorkerOption = { assets: [] }',
|
2026-06-04 21:59:09 +03:00
|
|
|
},
|
2026-06-04 17:47:07 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const swBundle = await build(config)
|
|
|
|
|
return swBundle.output[0]
|
2026-06-04 21:59:09 +03:00
|
|
|
},
|
2026-06-04 17:47:07 +03:00
|
|
|
},
|
2026-06-02 23:16:03 +03:00
|
|
|
closeBundle: {
|
|
|
|
|
order: 'post',
|
|
|
|
|
sequential: true,
|
|
|
|
|
async handler() {
|
|
|
|
|
console.info('Building service worker for production')
|
|
|
|
|
await build(config)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-02 22:14:13 +03:00
|
|
|
|
2026-06-02 23:16:03 +03:00
|
|
|
const swMessagesName = 'virtual:pleroma-fe/service_worker_messages'
|
|
|
|
|
const swMessagesNameResolved = '\0' + swMessagesName
|
|
|
|
|
|
|
|
|
|
export const swMessagesPlugin = () => {
|
2025-02-28 10:52:04 -05:00
|
|
|
return {
|
|
|
|
|
name: 'sw-messages-plugin',
|
2026-06-02 23:16:03 +03:00
|
|
|
resolveId(id) {
|
|
|
|
|
if (id === swMessagesName) {
|
|
|
|
|
Object.values(i18nFiles).forEach((f) => {
|
|
|
|
|
this.addWatchFile(f)
|
|
|
|
|
})
|
2025-02-28 10:52:04 -05:00
|
|
|
return swMessagesNameResolved
|
2026-06-02 23:16:03 +03:00
|
|
|
} else {
|
|
|
|
|
return null
|
2025-02-28 10:52:04 -05:00
|
|
|
}
|
|
|
|
|
},
|
2026-06-02 23:16:03 +03:00
|
|
|
async load(id) {
|
|
|
|
|
if (id === swMessagesNameResolved) {
|
2025-02-28 13:29:27 -05:00
|
|
|
return await getSWMessagesAsText()
|
2025-02-28 10:52:04 -05:00
|
|
|
}
|
2026-06-02 23:16:03 +03:00
|
|
|
return null
|
2026-01-06 16:22:52 +02:00
|
|
|
},
|
2025-02-28 10:52:04 -05:00
|
|
|
}
|
|
|
|
|
}
|