atagen

malice

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