-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert the service worker to work with Vite
- Loading branch information
Showing
10 changed files
with
2,215 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
app/javascript/mastodon/service_worker/web_push_locales.js
This file was deleted.
Oops, something went wrong.
6 changes: 4 additions & 2 deletions
6
app/javascript/mastodon/service_worker/web_push_notifications.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite-plugin-pwa/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* This plugin provides the `virtual:mastodon-sw-locales` import | ||
which exports translations for every locales, but only with the | ||
keys defined below. | ||
This is used by the notifications code in the service-worker, to | ||
provide localised texts without having to load all the translations | ||
*/ | ||
|
||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import type { Plugin, ResolvedConfig } from 'vite'; | ||
|
||
const KEEP_KEYS = [ | ||
'notification.favourite', | ||
'notification.follow', | ||
'notification.follow_request', | ||
'notification.mention', | ||
'notification.reblog', | ||
'notification.poll', | ||
'notification.status', | ||
'notification.update', | ||
'notification.admin.sign_up', | ||
'status.show_more', | ||
'status.reblog', | ||
'status.favourite', | ||
'notifications.group', | ||
]; | ||
|
||
export function MastodonServiceWorkerLocales(): Plugin { | ||
const virtualModuleId = 'virtual:mastodon-sw-locales'; | ||
const resolvedVirtualModuleId = '\0' + virtualModuleId; | ||
|
||
let config: ResolvedConfig; | ||
|
||
return { | ||
name: 'mastodon-sw-locales', | ||
configResolved(resolvedConfig) { | ||
config = resolvedConfig; | ||
}, | ||
resolveId(id) { | ||
if (id === virtualModuleId) { | ||
return resolvedVirtualModuleId; | ||
} | ||
|
||
return undefined; | ||
}, | ||
load(id) { | ||
if (id === resolvedVirtualModuleId) { | ||
const filteredLocales: Record<string, Record<string, string>> = {}; | ||
const localesPath = path.resolve(config.root, 'mastodon/locales'); | ||
|
||
const filenames = fs.readdirSync(localesPath); | ||
|
||
filenames | ||
.filter((filename) => filename.match(/[a-zA-Z-]+\.json$/)) | ||
.forEach((filename) => { | ||
const content = fs.readFileSync( | ||
path.resolve(localesPath, filename), | ||
'utf-8', | ||
); | ||
const full = JSON.parse(content) as Record<string, string>; | ||
const locale = filename.split('.')[0]; | ||
|
||
const filteredLocale: Record<string, string> = {}; | ||
|
||
Object.entries(full).forEach(([key, value]) => { | ||
if (KEEP_KEYS.includes(key)) filteredLocale[key] = value; | ||
}); | ||
|
||
filteredLocales[locale] = filteredLocale; | ||
}); | ||
|
||
return `const locales = ${JSON.stringify(filteredLocales)}; \n export default locales;`; | ||
} | ||
|
||
return undefined; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.