544a170ead2c | atagen | 2026-10-20 11:47 UTC | 1 | import { readdirSync, readFileSync } from 'node:fs'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 2 | import { join } from 'node:path'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 3 | import { fileURLToPath } from 'node:url'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | import { defineConfig } from 'vite'; |
f12c7de2a3ab | atagen | 2026-10-23 08:10 UTC | 5 | import { svelte } from '@sveltejs/vite-plugin-svelte'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 7 | const EMOJI_DATA_CHUNK = /^assets\/(compact|emojibase)-[^/]*\.js$/; |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 8 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | /** |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | * The page isn't SW-controlled on first launch, so the worker chunk and the |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | * wasm can bypass fetch interception entirely and never get cached. Instead |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | * of relying on runtime fetches, prepend the full list of emitted files |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | * (chunks, assets, wasm) to sw.js as a global so install() can precache all |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | * of it directly, no separate request needed. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | function precacheManifestPlugin(): import('vite').Plugin { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | return { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | name: 'malice-sw-precache-manifest', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | generateBundle(_options, bundle) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | const urls = Object.keys(bundle) |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 21 | .filter((fileName) => fileName !== 'sw.js' && !fileName.startsWith('twemoji/') && !EMOJI_DATA_CHUNK.test(fileName)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | .map((fileName) => `/${fileName}`); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | const swChunk = bundle['sw.js']; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | if (swChunk && swChunk.type === 'chunk') { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | swChunk.code = `self.__MALICE_PRECACHE__=${JSON.stringify(urls)};\n${swChunk.code}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 31 | function twemojiPlugin(): import('vite').Plugin { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 32 | const dir = fileURLToPath(new URL('./node_modules/@twemoji/svg', import.meta.url)); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 33 | const svgs = () => readdirSync(dir).filter((f) => f.endsWith('.svg')); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 34 | return { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 35 | name: 'malice-twemoji', |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 36 | configureServer(server) { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 37 | server.middlewares.use('/twemoji', (req, res, next) => { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 38 | const name = decodeURIComponent((req.url ?? '').split('?')[0]).replace(/^\//, ''); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 39 | if (!/^[0-9a-f-]+\.svg$/.test(name)) return next(); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 40 | try { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 41 | res.setHeader('Content-Type', 'image/svg+xml'); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 42 | res.end(readFileSync(join(dir, name))); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 43 | } catch { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 44 | res.statusCode = 404; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 45 | res.end(); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 46 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 47 | }); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 48 | }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 49 | generateBundle() { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 50 | for (const file of svgs()) { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 51 | this.emitFile({ type: 'asset', fileName: `twemoji/${file}`, source: readFileSync(join(dir, file)) }); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 52 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 53 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 54 | }; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 55 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 56 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | export default defineConfig(({ mode }) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | const device = mode === 'device'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | return { |
f12c7de2a3ab | atagen | 2026-10-23 08:10 UTC | 60 | plugins: [svelte(), twemojiPlugin(), ...(device ? [precacheManifestPlugin()] : [])], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | assetsInclude: ['**/*.wasm'], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | resolve: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | alias: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | 'malice:device-transport': device ? '/src/lib/device-transport.ts' : '/src/lib/device-transport.stub.ts' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | server: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | host: '127.0.0.1', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | port: 5173, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | proxy: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | '/api': 'http://localhost:3000' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | preview: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | host: '127.0.0.1', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | port: 4173, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | proxy: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | '/api': 'http://localhost:3000' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | build: device |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | ? { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | outDir: 'dist-device', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | emptyOutDir: true, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | rollupOptions: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | input: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | main: 'index.html', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | sw: 'src/sw.ts' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | output: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | entryFileNames: (chunk) => (chunk.name === 'sw' ? 'sw.js' : 'assets/[name]-[hash].js') |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | : { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | outDir: 'dist', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | emptyOutDir: true |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | }); |