atagen

malice

public
Star0
Watch0
dffd63b58af439639de9f8e155d0365d83605a8b
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/vite.config.tsTypescript70 lines2.1 KB
1
import { defineConfig } from 'vite';
2
import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte';
3
4
/**
5
 * The page isn't SW-controlled on first launch, so the worker chunk and the
6
 * wasm can bypass fetch interception entirely and never get cached. Instead
7
 * of relying on runtime fetches, prepend the full list of emitted files
8
 * (chunks, assets, wasm) to sw.js as a global so install() can precache all
9
 * of it directly, no separate request needed.
10
 */
11
function precacheManifestPlugin(): import('vite').Plugin {
12
  return {
13
    name: 'malice-sw-precache-manifest',
14
    generateBundle(_options, bundle) {
15
      const urls = Object.keys(bundle)
16
        .filter((fileName) => fileName !== 'sw.js' && fileName !== '.vite/manifest.json')
17
        .map((fileName) => `/${fileName}`);
18
      const swChunk = bundle['sw.js'];
19
      if (swChunk && swChunk.type === 'chunk') {
20
        swChunk.code = `self.__MALICE_PRECACHE__=${JSON.stringify(urls)};\n${swChunk.code}`;
21
      }
22
    }
23
  };
24
}
25
26
export default defineConfig(({ mode }) => {
27
  const device = mode === 'device';
28
  return {
29
    plugins: [svelte({ preprocess: vitePreprocess() }), ...(device ? [precacheManifestPlugin()] : [])],
30
    assetsInclude: ['**/*.wasm'],
31
    resolve: {
32
      alias: {
33
        'malice:device-transport': device ? '/src/lib/device-transport.ts' : '/src/lib/device-transport.stub.ts'
34
      }
35
    },
36
    server: {
37
      host: '127.0.0.1',
38
      port: 5173,
39
      proxy: {
40
        '/api': 'http://localhost:3000'
41
      }
42
    },
43
    preview: {
44
      host: '127.0.0.1',
45
      port: 4173,
46
      proxy: {
47
        '/api': 'http://localhost:3000'
48
      }
49
    },
50
    build: device
51
      ? {
52
          outDir: 'dist-device',
53
          emptyOutDir: true,
54
          manifest: true,
55
          rollupOptions: {
56
            input: {
57
              main: 'index.html',
58
              sw: 'src/sw.ts'
59
            },
60
            output: {
61
              entryFileNames: (chunk) => (chunk.name === 'sw' ? 'sw.js' : 'assets/[name]-[hash].js')
62
            }
63
          }
64
        }
65
      : {
66
          outDir: 'dist',
67
          emptyOutDir: true
68
        }
69
  };
70
});