#!/usr/bin/env node /** * Sert dist-portal + chaque dist- sous /slides// (tests E2E, prod locale). * Comme le plugin Vite en dev : racine du deck → 302 vers /slides//1 ; routes Slidev (/1, /2…) via SPA (sirv single). */ import http from 'node:http' import path from 'node:path' import { fileURLToPath } from 'node:url' import sirv from 'sirv' import { listPresentationSlugs } from './list-slugs.mjs' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const port = Number(process.env.PREVIEW_PORT || 4174) const portalStatic = sirv(path.join(root, 'dist-portal'), { etag: true }) const slideSirv = new Map() function getSirv(slug) { if (!slideSirv.has(slug)) { slideSirv.set( slug, sirv(path.join(root, `dist-${slug}`), { etag: true, single: 'index.html', }), ) } return slideSirv.get(slug) } const server = http.createServer((req, res) => { const u = new URL(req.url || '/', 'http://127.0.0.1') const pathname = u.pathname const m = pathname.match(/^\/slides\/([^/]+)(.*)$/) if (m) { const slug = m[1] const rest = m[2] || '/' const slugs = new Set(listPresentationSlugs(root)) if (!slugs.has(slug)) { res.statusCode = 404 res.end('not found') return } const atDeckRoot = rest === '/' || rest === '' if (atDeckRoot && (req.method === 'GET' || req.method === 'HEAD')) { res.statusCode = 302 res.setHeader('Location', `/slides/${slug}/1${u.search || ''}`) res.end() return } const orig = req.url req.url = rest + (u.search || '') getSirv(slug)(req, res, () => { req.url = orig res.statusCode = 404 res.end() }) return } portalStatic(req, res, () => { res.statusCode = 404 res.end('not found') }) }) server.listen(port, '127.0.0.1', () => { console.log(`[static-preview] http://127.0.0.1:${port}/`) })