The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | import * as fsp from "node:fs/promises"; |
| 2 | import * as path from "node:path"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { defineConfig, preview } from "vite"; |
| 5 | |
| 6 | // The playground's prerender plugin: after the build, a preview server |
| 7 | // renders `/prerendered` into the client output. |
| 8 | export default defineConfig({ |
| 9 | plugins: [ |
| 10 | { |
| 11 | name: "prerender-plugin", |
| 12 | enforce: "post", |
| 13 | buildApp: { |
| 14 | order: "post", |
| 15 | async handler(builder) { |
| 16 | const previewServer = await preview({ |
| 17 | root: path.dirname(fileURLToPath(import.meta.url)), |
| 18 | logLevel: "silent", |
| 19 | preview: { |
| 20 | port: 0, |
| 21 | }, |
| 22 | }); |
| 23 | |
| 24 | const baseUrl = previewServer.resolvedUrls?.local[0]; |
| 25 | const clientOutputDirectory = builder.environments.client?.config.build.outDir; |
| 26 | |
| 27 | if (baseUrl && clientOutputDirectory) { |
| 28 | const response = await fetch(new URL("/prerendered", baseUrl)); |
| 29 | const html = await response.text(); |
| 30 | |
| 31 | const outputDirectory = path.resolve(builder.config.root, clientOutputDirectory); |
| 32 | await fsp.mkdir(outputDirectory, { recursive: true }); |
| 33 | await fsp.writeFile(path.join(outputDirectory, "prerendered.html"), html); |
| 34 | |
| 35 | console.log("Pre-rendered /prerendered"); |
| 36 | } |
| 37 | |
| 38 | await previewServer.close(); |
| 39 | }, |
| 40 | }, |
| 41 | }, |
| 42 | ], |
| 43 | }); |