import { defineConfig } from 'vite'; import { readFileSync, writeFileSync } from 'fs'; /** Vite plugin: generates dist/sw.js with precache URLs from the build manifest. */ function generateSW() { return { name: 'generate-sw', apply: 'build', closeBundle() { const staticAssets = [ '/', '/manifest.json', '/icons/icon-192.svg', '/icons/icon-512.svg', ]; try { const manifest = JSON.parse(readFileSync('dist/.vite/manifest.json', 'utf-8')); for (const entry of Object.values(manifest)) { staticAssets.push('/' + entry.file); if (entry.css) entry.css.forEach(f => staticAssets.push('/' + f)); } } catch { /* manifest may not exist in dev */ } const template = readFileSync('sw.js', 'utf-8'); const out = template.replace('__PRECACHE_URLS__', JSON.stringify(staticAssets, null, 4)); writeFileSync('dist/sw.js', out); console.log('[generate-sw] wrote dist/sw.js with', staticAssets.length, 'precache URLs'); }, }; } export default defineConfig({ root: '.', build: { outDir: 'dist', assetsDir: 'assets', target: 'esnext', manifest: true, rollupOptions: { output: { manualChunks: { three: ['three'], }, }, }, }, plugins: [generateSW()], server: { host: true, }, });