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