-
Notifications
You must be signed in to change notification settings - Fork 167
/
start.ts
63 lines (54 loc) · 2.29 KB
/
start.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
import { basename, join } from "https://deno.land/std@0.136.0/path/mod.ts";
import { serve as stdServe, serveTls } from "https://deno.land/std@0.136.0/http/server.ts";
import { findFile } from "../lib/fs.ts";
import { builtinModuleExts } from "../lib/helpers.ts";
import log, { blue } from "../lib/log.ts";
import { initModuleLoaders, loadImportMap } from "../server/config.ts";
import { build } from "../server/build.ts";
import { serve } from "../server/mod.ts";
import { proxyModules } from "../server/proxy_modules.ts";
import type { AlephConfig } from "../server/types.ts";
if (import.meta.main) {
// add envs
Deno.env.set("ALEPH_CLI", "true");
Deno.env.set("ALEPH_ENV", "production");
// set log level to 'debug' when in aleph framework dev mode
if (Deno.env.get("ALEPH_DEV")) {
log.setLevel("debug");
}
// serve app modules
const ac = new AbortController();
const importMap = await loadImportMap();
const moduleLoaders = await initModuleLoaders(importMap);
proxyModules(6060, { importMap, moduleLoaders, signal: ac.signal });
let serverEntry = await findFile(builtinModuleExts.map((ext) => `server.${ext}`));
if (serverEntry) {
await import(
`http://localhost:${Deno.env.get("ALEPH_MODULES_PROXY_PORT")}/${basename(serverEntry)}?t=${
Date.now().toString(16)
}`
);
}
// make the default handler
if (!Reflect.has(globalThis, "__ALEPH_SERVER")) {
serverEntry = undefined;
serve();
}
log.info("Building...");
const { clientModules } = await build(serverEntry);
log.info(`${clientModules.size} client modules built`);
// close the app modules server
ac.abort();
const config: AlephConfig | undefined = Reflect.get(globalThis, "__ALEPH_CONFIG");
const outputDir = config?.build?.outputDir ?? "dist";
const distServerEntry = "file://" + join(Deno.cwd(), outputDir, "server.js");
await import(distServerEntry);
log.info(`Bootstrap server from ${blue(join(outputDir, "server.js"))}...`);
const { hostname, port = 8080, certFile, keyFile, signal, handler } = Reflect.get(globalThis, "__ALEPH_SERVER") || {};
log.info(`Server ready on http://localhost:${port}`);
if (certFile && keyFile) {
await serveTls(handler, { port, hostname, certFile, keyFile, signal });
} else {
await stdServe(handler, { port, hostname, signal });
}
}