-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
105 lines (94 loc) · 2.75 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
createApp,
createRouter,
eventHandler,
fromNodeMiddleware,
send,
appendHeader,
sendError,
toNodeListener,
} from "h3";
import { listen } from "listhen";
import { resolve } from "node:path";
import { readFileSync } from "node:fs";
import type { ServerReturn } from "./types/ssr";
import type { ViteDevServer } from "vite";
import * as jsdom from "jsdom";
const isTest = process.env.VITEST;
export const createServer = async (
root = process.cwd(),
hmrPort = 1337
): Promise<ServerReturn> => {
const app = createApp({ debug: true });
const vite: ViteDevServer = await (
await import("vite")
).createServer({
root,
logLevel: isTest ? "error" : "info",
server: {
middlewareMode: true,
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
hmr: {
port: hmrPort,
},
},
appType: "custom",
});
app.use(fromNodeMiddleware(vite.middlewares));
// use vite's connect instance as middleware
app.use(
"*",
eventHandler(async (event) => {
try {
const url = event.path.replace(/\/test/, "");
// always read fresh template in dev
let template = readFileSync(resolve("index.html"), "utf-8");
template = await vite.transformIndexHtml(url, template);
const render = (await vite.ssrLoadModule("/src/entry-server.ts"))
.render;
const { appHtml, ctx, preloadLinks, title, meta } = await render(
url,
{}
);
let html = template
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-html-->`, appHtml)
.replace(`<!--title-->`, title)
.replace(`<!--meta-->`, meta);
if (ctx.teleports) {
const DOMParsed = new jsdom.JSDOM(html);
for (const teleportsKey in ctx.teleports) {
const parent =
DOMParsed.window.document.querySelector(teleportsKey);
if (parent) {
parent.insertAdjacentHTML(
"afterbegin",
ctx.teleports[teleportsKey]
);
}
}
html = DOMParsed.serialize();
}
appendHeader(event, "Content-Type", "text/html");
return send(event, html);
} catch (e) {
vite && vite.ssrFixStacktrace(e as Error);
console.error((e as Error).stack);
return sendError(event, e);
}
})
);
return { app, vite };
};
if (!isTest) {
createServer().then(({ app }) =>
listen(toNodeListener(app), { port: 3000 }).then(() => {
console.log("Serveur lancé sur le port http://localhost:3000");
})
);
}