-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathdev_server.ts
More file actions
79 lines (70 loc) · 2.26 KB
/
Copy pathdev_server.ts
File metadata and controls
79 lines (70 loc) · 2.26 KB
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
import type { Plugin } from "vite";
import { nodeToRequest, responseToNode } from "../request.ts";
import * as path from "@std/path";
import { pathWithRoot } from "../utils.ts";
export function devServer(): Plugin[] {
let publicDir = "";
return [
{
name: "fresh:dev_server",
configResolved(cfg) {
publicDir = pathWithRoot(cfg.publicDir, cfg.root);
},
configureServer(server) {
const IGNORE_URLS = /^\/(@(vite|fs|id)|\.vite)\//;
server.middlewares.use(async (nodeReq, nodeRes, next) => {
const serverCfg = server.config.server;
const protocol = serverCfg.https ? "https" : "http";
const host = serverCfg.host ? serverCfg.host : "localhost";
const port = serverCfg.port;
const url = new URL(
`${protocol}://${host}:${port}${nodeReq.url ?? "/"}`,
);
// Check if it's a vite url
if (
IGNORE_URLS.test(url.pathname) ||
server.environments.client.moduleGraph.urlToModuleMap.has(
url.pathname,
)
) {
return next();
}
// Check if it's a static file first
// FIXME: Should this still go through fresh?
if (url.pathname !== "/") {
try {
await Deno.stat(path.join(publicDir, url.pathname));
return next();
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
return next(err);
}
}
}
const mod = await server.ssrLoadModule("fresh:server_entry");
try {
const req = nodeToRequest(nodeReq, url);
const res = await mod.default.fetch(req);
await responseToNode(res, nodeRes);
return nodeRes;
} catch (err) {
return next(err);
}
});
},
},
{
name: "fresh:server_hmr",
applyToEnvironment(env) {
return env.name === "ssr";
},
hotUpdate(options) {
const clientMod = options.server.environments.client.moduleGraph
.getModulesByFile(options.file);
if (clientMod === undefined) {
options.server.hot.send("fresh:reload");
}
},
},
];
}