-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
server.ts
133 lines (115 loc) · 3.14 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ULTRA_COMPILER_PATH } from "./constants.ts";
import { assert, Hono, resolve, toFileUrl } from "./deps.ts";
import { ensureMinDenoVersion } from "./dev/ensureMinDenoVersion.ts";
import { serveStatic } from "./middleware/serveStatic.ts";
import { CreateServerOptions, Mode } from "./types.ts";
import { UltraServer } from "./ultra.ts";
import { log } from "./logger.ts";
import { resolveImportMapPath } from "./utils/import-map.ts";
/**
* Check if we are running on Deno Deploy and set the mode to production
* if the mode hasn't been specified via the environment.
*/
const modeFromEnv = Deno.env.get("ULTRA_MODE") ||
(Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined ? "production" : undefined);
const defaultOptions = {
mode: (modeFromEnv || "development") as Mode,
};
export async function createServer(
options: CreateServerOptions,
) {
const resolvedOptions = {
...defaultOptions,
...options,
};
assertServerOptions(resolvedOptions);
const { mode = "development", browserEntrypoint } =
resolvedOptions as Required<CreateServerOptions>;
const root = Deno.cwd();
const importMapPath = resolveImportMapPath(mode, root, options.importMapPath);
const assetManifestPath =
toFileUrl(resolve(root, "asset-manifest.json")).href;
const server = new UltraServer(
root,
mode,
importMapPath,
assetManifestPath,
browserEntrypoint,
);
await server.init();
if (mode === "development") {
log.info("Loading compiler");
const { compiler } = await import("./middleware/compiler.ts");
server.use(
`${ULTRA_COMPILER_PATH}/*`,
compiler({
mode,
root,
...options.compilerOptions,
}),
);
/**
* Serve assets from "./public" at "/"
*/
server.use(
"*",
serveStatic({
root: resolve(root, "./public"),
cache: false,
}),
);
} else {
/**
* Serve assets from "./public" at "/"
*/
server.use(
"*",
serveStatic({
root: resolve(root, "./public"),
cache: true,
}),
);
/**
* Serve anything else static at "/"
*/
server.use(
"*",
serveStatic({
root: resolve(root, "./"),
cache: true,
}),
);
}
return server;
}
export function createRouter() {
const router = new Hono();
return router;
}
export function assertServerOptions(options: CreateServerOptions) {
try {
/**
* Ensure we are running a supported Deno version
*/
options.mode === "development" && ensureMinDenoVersion();
/**
* Assert that we are provided a valid "mode"
*/
assert(
["development", "production"].includes(options.mode!),
`Invalid value supplied for "mode", expected either "production" or "development" received "${options.mode}"`,
);
/**
* Assert that we are provided an importMapPath
*/
assert(options.importMapPath, "No importMapPath was supplied");
/**
* Assert that we are provided a browserEntrypoint
*/
assert(
`A browser entrypoint was not provided "${options.browserEntrypoint}"`,
);
} catch (error) {
throw new Error(error.message);
}
}