atagen

malice

public
Star0
Watch0
2fd195b22029193d9537c977c1220f0141cd2117
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/vite.config.ts

544a170ead2catagen2026-10-20 11:47 UTC1
import { readdirSync, readFileSync } from 'node:fs';
544a170ead2catagen2026-10-20 11:47 UTC2
import { join } from 'node:path';
544a170ead2catagen2026-10-20 11:47 UTC3
import { fileURLToPath } from 'node:url';
973f8fbdaf80atagen2026-09-28 09:10 UTC4
import { defineConfig } from 'vite';
f12c7de2a3abatagen2026-10-23 08:10 UTC5
import { svelte } from '@sveltejs/vite-plugin-svelte';
973f8fbdaf80atagen2026-09-28 09:10 UTC6
919753ab134catagen2026-10-26 03:48 UTC7
const EMOJI_DATA_CHUNK = /^assets\/(compact|emojibase)-[^/]*\.js$/;
919753ab134catagen2026-10-26 03:48 UTC8
973f8fbdaf80atagen2026-09-28 09:10 UTC9
/**
973f8fbdaf80atagen2026-09-28 09:10 UTC10
 * The page isn't SW-controlled on first launch, so the worker chunk and the
973f8fbdaf80atagen2026-09-28 09:10 UTC11
 * wasm can bypass fetch interception entirely and never get cached. Instead
973f8fbdaf80atagen2026-09-28 09:10 UTC12
 * of relying on runtime fetches, prepend the full list of emitted files
973f8fbdaf80atagen2026-09-28 09:10 UTC13
 * (chunks, assets, wasm) to sw.js as a global so install() can precache all
973f8fbdaf80atagen2026-09-28 09:10 UTC14
 * of it directly, no separate request needed.
973f8fbdaf80atagen2026-09-28 09:10 UTC15
 */
973f8fbdaf80atagen2026-09-28 09:10 UTC16
function precacheManifestPlugin(): import('vite').Plugin {
973f8fbdaf80atagen2026-09-28 09:10 UTC17
  return {
973f8fbdaf80atagen2026-09-28 09:10 UTC18
    name: 'malice-sw-precache-manifest',
973f8fbdaf80atagen2026-09-28 09:10 UTC19
    generateBundle(_options, bundle) {
973f8fbdaf80atagen2026-09-28 09:10 UTC20
      const urls = Object.keys(bundle)
919753ab134catagen2026-10-26 03:48 UTC21
        .filter((fileName) => fileName !== 'sw.js' && !fileName.startsWith('twemoji/') && !EMOJI_DATA_CHUNK.test(fileName))
973f8fbdaf80atagen2026-09-28 09:10 UTC22
        .map((fileName) => `/${fileName}`);
973f8fbdaf80atagen2026-09-28 09:10 UTC23
      const swChunk = bundle['sw.js'];
973f8fbdaf80atagen2026-09-28 09:10 UTC24
      if (swChunk && swChunk.type === 'chunk') {
973f8fbdaf80atagen2026-09-28 09:10 UTC25
        swChunk.code = `self.__MALICE_PRECACHE__=${JSON.stringify(urls)};\n${swChunk.code}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC26
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC27
    }
973f8fbdaf80atagen2026-09-28 09:10 UTC28
  };
973f8fbdaf80atagen2026-09-28 09:10 UTC29
}
973f8fbdaf80atagen2026-09-28 09:10 UTC30
544a170ead2catagen2026-10-20 11:47 UTC31
function twemojiPlugin(): import('vite').Plugin {
544a170ead2catagen2026-10-20 11:47 UTC32
  const dir = fileURLToPath(new URL('./node_modules/@twemoji/svg', import.meta.url));
544a170ead2catagen2026-10-20 11:47 UTC33
  const svgs = () => readdirSync(dir).filter((f) => f.endsWith('.svg'));
544a170ead2catagen2026-10-20 11:47 UTC34
  return {
544a170ead2catagen2026-10-20 11:47 UTC35
    name: 'malice-twemoji',
544a170ead2catagen2026-10-20 11:47 UTC36
    configureServer(server) {
544a170ead2catagen2026-10-20 11:47 UTC37
      server.middlewares.use('/twemoji', (req, res, next) => {
544a170ead2catagen2026-10-20 11:47 UTC38
        const name = decodeURIComponent((req.url ?? '').split('?')[0]).replace(/^\//, '');
544a170ead2catagen2026-10-20 11:47 UTC39
        if (!/^[0-9a-f-]+\.svg$/.test(name)) return next();
544a170ead2catagen2026-10-20 11:47 UTC40
        try {
544a170ead2catagen2026-10-20 11:47 UTC41
          res.setHeader('Content-Type', 'image/svg+xml');
544a170ead2catagen2026-10-20 11:47 UTC42
          res.end(readFileSync(join(dir, name)));
544a170ead2catagen2026-10-20 11:47 UTC43
        } catch {
544a170ead2catagen2026-10-20 11:47 UTC44
          res.statusCode = 404;
544a170ead2catagen2026-10-20 11:47 UTC45
          res.end();
544a170ead2catagen2026-10-20 11:47 UTC46
        }
544a170ead2catagen2026-10-20 11:47 UTC47
      });
544a170ead2catagen2026-10-20 11:47 UTC48
    },
544a170ead2catagen2026-10-20 11:47 UTC49
    generateBundle() {
544a170ead2catagen2026-10-20 11:47 UTC50
      for (const file of svgs()) {
544a170ead2catagen2026-10-20 11:47 UTC51
        this.emitFile({ type: 'asset', fileName: `twemoji/${file}`, source: readFileSync(join(dir, file)) });
544a170ead2catagen2026-10-20 11:47 UTC52
      }
544a170ead2catagen2026-10-20 11:47 UTC53
    }
544a170ead2catagen2026-10-20 11:47 UTC54
  };
544a170ead2catagen2026-10-20 11:47 UTC55
}
544a170ead2catagen2026-10-20 11:47 UTC56
973f8fbdaf80atagen2026-09-28 09:10 UTC57
export default defineConfig(({ mode }) => {
973f8fbdaf80atagen2026-09-28 09:10 UTC58
  const device = mode === 'device';
973f8fbdaf80atagen2026-09-28 09:10 UTC59
  return {
d8772d4eb408atagen2026-10-26 04:43 UTC60
    plugins: [svelte(), twemojiPlugin(), precacheManifestPlugin()],
d8772d4eb408atagen2026-10-26 04:43 UTC61
    define: { __MALICE_DEVICE__: JSON.stringify(device) },
973f8fbdaf80atagen2026-09-28 09:10 UTC62
    assetsInclude: ['**/*.wasm'],
2c899da1562batagen2026-10-26 06:04 UTC63
    worker: { format: 'es' },
973f8fbdaf80atagen2026-09-28 09:10 UTC64
    resolve: {
973f8fbdaf80atagen2026-09-28 09:10 UTC65
      alias: {
973f8fbdaf80atagen2026-09-28 09:10 UTC66
        'malice:device-transport': device ? '/src/lib/device-transport.ts' : '/src/lib/device-transport.stub.ts'
973f8fbdaf80atagen2026-09-28 09:10 UTC67
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC68
    },
973f8fbdaf80atagen2026-09-28 09:10 UTC69
    server: {
973f8fbdaf80atagen2026-09-28 09:10 UTC70
      host: '127.0.0.1',
973f8fbdaf80atagen2026-09-28 09:10 UTC71
      port: 5173,
973f8fbdaf80atagen2026-09-28 09:10 UTC72
      proxy: {
973f8fbdaf80atagen2026-09-28 09:10 UTC73
        '/api': 'http://localhost:3000'
973f8fbdaf80atagen2026-09-28 09:10 UTC74
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC75
    },
973f8fbdaf80atagen2026-09-28 09:10 UTC76
    preview: {
973f8fbdaf80atagen2026-09-28 09:10 UTC77
      host: '127.0.0.1',
973f8fbdaf80atagen2026-09-28 09:10 UTC78
      port: 4173,
973f8fbdaf80atagen2026-09-28 09:10 UTC79
      proxy: {
973f8fbdaf80atagen2026-09-28 09:10 UTC80
        '/api': 'http://localhost:3000'
973f8fbdaf80atagen2026-09-28 09:10 UTC81
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC82
    },
d8772d4eb408atagen2026-10-26 04:43 UTC83
    build: {
d8772d4eb408atagen2026-10-26 04:43 UTC84
      outDir: device ? 'dist-device' : 'dist',
d8772d4eb408atagen2026-10-26 04:43 UTC85
      emptyOutDir: true,
d8772d4eb408atagen2026-10-26 04:43 UTC86
      rollupOptions: {
d8772d4eb408atagen2026-10-26 04:43 UTC87
        input: {
d8772d4eb408atagen2026-10-26 04:43 UTC88
          main: 'index.html',
d8772d4eb408atagen2026-10-26 04:43 UTC89
          sw: 'src/sw.ts'
d8772d4eb408atagen2026-10-26 04:43 UTC90
        },
d8772d4eb408atagen2026-10-26 04:43 UTC91
        output: {
d8772d4eb408atagen2026-10-26 04:43 UTC92
          entryFileNames: (chunk) => (chunk.name === 'sw' ? 'sw.js' : 'assets/[name]-[hash].js')
973f8fbdaf80atagen2026-09-28 09:10 UTC93
        }
d8772d4eb408atagen2026-10-26 04:43 UTC94
      }
d8772d4eb408atagen2026-10-26 04:43 UTC95
    }
973f8fbdaf80atagen2026-09-28 09:10 UTC96
  };
973f8fbdaf80atagen2026-09-28 09:10 UTC97
});