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