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