|
| 1 | +import tsx from "dedent"; |
| 2 | + |
| 3 | +export function server() { |
| 4 | + return tsx` |
| 5 | + import { createRequestHandler } from "@react-router/express"; |
| 6 | + import express from "express"; |
| 7 | +
|
| 8 | + const port = process.env.PORT ?? 3000 |
| 9 | + const hmrPort = process.env.HMR_PORT ?? 3001 |
| 10 | +
|
| 11 | + const app = express(); |
| 12 | +
|
| 13 | + const getLoadContext = () => ({}); |
| 14 | +
|
| 15 | + if (process.env.NODE_ENV === "production") { |
| 16 | + app.use( |
| 17 | + "/assets", |
| 18 | + express.static("build/client/assets", { immutable: true, maxAge: "1y" }) |
| 19 | + ); |
| 20 | + app.use(express.static("build/client", { maxAge: "1h" })); |
| 21 | + app.all("*", createRequestHandler({ |
| 22 | + build: await import("./build/index.js"), |
| 23 | + getLoadContext, |
| 24 | + })); |
| 25 | + } else { |
| 26 | + const viteDevServer = await import("vite").then( |
| 27 | + (vite) => vite.createServer({ |
| 28 | + server: { |
| 29 | + middlewareMode: true, |
| 30 | + hmr: { port: hmrPort }, |
| 31 | + }, |
| 32 | + }) |
| 33 | + ); |
| 34 | + app.use(viteDevServer.middlewares); |
| 35 | + app.all("*", createRequestHandler({ |
| 36 | + build:() => viteDevServer.ssrLoadModule("virtual:react-router/server-build"), |
| 37 | + getLoadContext, |
| 38 | + })); |
| 39 | + } |
| 40 | +
|
| 41 | + app.listen(port, () => console.log('http://localhost:' + port)); |
| 42 | + `; |
| 43 | +} |
| 44 | + |
| 45 | +export function rsc() { |
| 46 | + return tsx` |
| 47 | + import { createRequestListener } from "@mjackson/node-fetch-server"; |
| 48 | + import express from "express"; |
| 49 | +
|
| 50 | + const port = process.env.PORT ?? 3000 |
| 51 | + const hmrPort = process.env.HMR_PORT ?? 3001 |
| 52 | +
|
| 53 | + const app = express(); |
| 54 | +
|
| 55 | + if (process.env.NODE_ENV === "production") { |
| 56 | + app.use( |
| 57 | + "/assets", |
| 58 | + express.static("build/client/assets", { immutable: true, maxAge: "1y" }) |
| 59 | + ); |
| 60 | + app.all("*", createRequestListener((await import("./build/server/index.js")).default)); |
| 61 | + } else { |
| 62 | + const viteDevServer = await import("vite").then( |
| 63 | + (vite) => vite.createServer({ |
| 64 | + server: { |
| 65 | + middlewareMode: true, |
| 66 | + hmr: { port: hmrPort }, |
| 67 | + }, |
| 68 | + }) |
| 69 | + ); |
| 70 | + app.use(viteDevServer.middlewares); |
| 71 | + } |
| 72 | +
|
| 73 | + app.listen(port, () => console.log('http://localhost:' + port)); |
| 74 | + `; |
| 75 | +} |
0 commit comments