import fs from 'node:fs/promises'; import Koa from "koa"; import c2k from 'koa-connect'; import Send from 'koa-send'; // Constants const isProduction = process.env.NODE_ENV === 'production'; const port = process.env.PORT || 5173; const base = process.env.BASE || '/'; // Cached production assets const templateHtml = isProduction ? await fs.readFile('./dist/client/index.html', 'utf-8') : ''; const app = new Koa(); // Add Vite or respective production middlewares /** @type {import('vite').ViteDevServer | undefined} */ let vite; if (!isProduction) { const { createServer } = await import('vite'); vite = await createServer({ server: { middlewareMode: true }, appType: 'custom', base, }); app.use(c2k(vite.middlewares)); } else { app.use(Send({ root: 'dist/client', index: false })); } app.use(async (ctx, next) => { try { const url = ctx.path.replace(base, ''); /** @type {string} */ let template; /** @type {import('./client/entry-server.ts').render} */ let render; if (!isProduction) { // Always read fresh template in development template = await fs.readFile('./index.html', 'utf-8'); template = await vite.transformIndexHtml(url, template); render = (await vite.ssrLoadModule('/client/entry-server.ts')).render; } else { template = templateHtml; // @ts-ignore render = (await import('./dist/server/entry-server.js')).render; } const rendered = await render(url); const html = template .replace(``, rendered.head ?? '') .replace(``, rendered.html ?? ''); ctx.status = 200; ctx.set({ 'Content-Type': 'text/html' }); ctx.body = html; } catch (e) { vite?.ssrFixStacktrace(e); console.log(e.stack); ctx.status = 500; ctx.body = e.stack; } await next(); }); // Start http server app.listen(port, () => { console.log(`Server started at http://localhost:${port}`); });