atagen

malice

public
Star0
Watch0
4929dcd3493716eb569d34503d3dee34b610742c
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 {
f12c7de2a3abatagen2026-10-23 08:10 UTC60
    plugins: [svelte(), twemojiPlugin(), ...(device ? [precacheManifestPlugin()] : [])],
973f8fbdaf80atagen2026-09-28 09:10 UTC61
    assetsInclude: ['**/*.wasm'],
973f8fbdaf80atagen2026-09-28 09:10 UTC62
    resolve: {
973f8fbdaf80atagen2026-09-28 09:10 UTC63
      alias: {
973f8fbdaf80atagen2026-09-28 09:10 UTC64
        'malice:device-transport': device ? '/src/lib/device-transport.ts' : '/src/lib/device-transport.stub.ts'
973f8fbdaf80atagen2026-09-28 09:10 UTC65
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC66
    },
973f8fbdaf80atagen2026-09-28 09:10 UTC67
    server: {
973f8fbdaf80atagen2026-09-28 09:10 UTC68
      host: '127.0.0.1',
973f8fbdaf80atagen2026-09-28 09:10 UTC69
      port: 5173,
973f8fbdaf80atagen2026-09-28 09:10 UTC70
      proxy: {
973f8fbdaf80atagen2026-09-28 09:10 UTC71
        '/api': 'http://localhost:3000'
973f8fbdaf80atagen2026-09-28 09:10 UTC72
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC73
    },
973f8fbdaf80atagen2026-09-28 09:10 UTC74
    preview: {
973f8fbdaf80atagen2026-09-28 09:10 UTC75
      host: '127.0.0.1',
973f8fbdaf80atagen2026-09-28 09:10 UTC76
      port: 4173,
973f8fbdaf80atagen2026-09-28 09:10 UTC77
      proxy: {
973f8fbdaf80atagen2026-09-28 09:10 UTC78
        '/api': 'http://localhost:3000'
973f8fbdaf80atagen2026-09-28 09:10 UTC79
      }
973f8fbdaf80atagen2026-09-28 09:10 UTC80
    },
973f8fbdaf80atagen2026-09-28 09:10 UTC81
    build: device
973f8fbdaf80atagen2026-09-28 09:10 UTC82
      ? {
973f8fbdaf80atagen2026-09-28 09:10 UTC83
          outDir: 'dist-device',
973f8fbdaf80atagen2026-09-28 09:10 UTC84
          emptyOutDir: true,
973f8fbdaf80atagen2026-09-28 09:10 UTC85
          rollupOptions: {
973f8fbdaf80atagen2026-09-28 09:10 UTC86
            input: {
973f8fbdaf80atagen2026-09-28 09:10 UTC87
              main: 'index.html',
973f8fbdaf80atagen2026-09-28 09:10 UTC88
              sw: 'src/sw.ts'
973f8fbdaf80atagen2026-09-28 09:10 UTC89
            },
973f8fbdaf80atagen2026-09-28 09:10 UTC90
            output: {
973f8fbdaf80atagen2026-09-28 09:10 UTC91
              entryFileNames: (chunk) => (chunk.name === 'sw' ? 'sw.js' : 'assets/[name]-[hash].js')
973f8fbdaf80atagen2026-09-28 09:10 UTC92
            }
973f8fbdaf80atagen2026-09-28 09:10 UTC93
          }
973f8fbdaf80atagen2026-09-28 09:10 UTC94
        }
973f8fbdaf80atagen2026-09-28 09:10 UTC95
      : {
973f8fbdaf80atagen2026-09-28 09:10 UTC96
          outDir: 'dist',
973f8fbdaf80atagen2026-09-28 09:10 UTC97
          emptyOutDir: true
973f8fbdaf80atagen2026-09-28 09:10 UTC98
        }
973f8fbdaf80atagen2026-09-28 09:10 UTC99
  };
973f8fbdaf80atagen2026-09-28 09:10 UTC100
});