Applies Replit PR #28 feature on top of current main: - Root sw.js template with __PRECACHE_URLS__ placeholder - generate-sw Vite plugin: reads build manifest, injects actual asset URLs - manifest: true in Vite build config for asset manifest generation - SW registration gated to import.meta.env.PROD (no dev interference) - Preserves existing manifest.json and public/sw.js as dev fallback
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
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,
|
|
},
|
|
});
|