-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
90 lines (76 loc) · 2.31 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import fs from "fs";
import path from "path";
import express from "express";
import { fileURLToPath } from "url";
import { instanceToPlain } from "class-transformer";
const isTest = process.env.VITEST;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const resolve = (p: string) => path.resolve(__dirname, p);
const isProd = process.env.NODE_ENV === "production";
export async function createServer(root = process.cwd(), hmrPort = 3001) {
const app = express();
let vite: any; // TODO cast
if (isProd) {
app.use((await import("compression")).default());
app.use(
(await import("serve-static")).default(resolve("./dist/client"), {
index: false,
})
);
} else {
vite = await (
await import("vite")
).createServer({
root,
logLevel: isTest ? "error" : "info",
server: {
middlewareMode: true,
watch: {
usePolling: true,
interval: 100,
},
hmr: {
port: hmrPort,
},
},
appType: "custom",
});
app.use(vite.middlewares);
}
const { getStore, prefetch, render } = isProd
? await import("./dist/server/entry-server.js")
: await vite.ssrLoadModule("./src/entry-server.tsx");
const indexProd = isProd
? fs.readFileSync(resolve("./dist/client/index.html"), "utf-8")
: "";
return { app, vite, indexProd, getStore, prefetch, render };
}
createServer().then(({ app, vite, indexProd, getStore, prefetch, render }) => {
app.use("*", async (req, res) => {
try {
const url: string = req.originalUrl;
const template: string = isProd
? indexProd
: await vite.transformIndexHtml(
url,
fs.readFileSync(resolve("index.html"), "utf-8")
);
const store = getStore();
await prefetch(url, store);
const { appHtml } = render(url, store);
const html = template.replace(`<!--content-->`, appHtml).replace(
`<!--__STATE__-->`,
`window.__STATE=${JSON.stringify(
instanceToPlain(store, {
enableCircularCheck: true,
})
)}`
);
res.status(200).set({ "Content-Type": "text/html" }).end(html);
} catch (e: any) {
!isProd && vite.ssrFixStacktrace(e);
res.status(500).end(e.stack);
}
});
app.listen(3000);
});