atagen

malice

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

Blame: web/vite.config.ts

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