-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathesbuild.dev.config.ts
More file actions
109 lines (91 loc) · 2.69 KB
/
esbuild.dev.config.ts
File metadata and controls
109 lines (91 loc) · 2.69 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
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
// Import Node.js Dependencies
import fs from "node:fs/promises";
import http from "node:http";
import path from "node:path";
// Import Third-party Dependencies
import * as i18n from "@nodesecure/i18n";
import chokidar from "chokidar";
import esbuild from "esbuild";
import open from "open";
import {
WebSocketServerInstanciator,
logger,
buildServer
} from "@nodesecure/server";
// Import Internal Dependencies
import english from "./i18n/english.js";
import french from "./i18n/french.js";
import {
PUBLIC_DIR,
OUTPUT_DIR,
getSharedBuildOptions,
copyStaticAssets
} from "./esbuild.common.ts";
// CONSTANTS
const kComponentsDir = path.join(PUBLIC_DIR, "components");
const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json");
const kDevPort = Number(process.env.DEV_PORT ?? 8080);
await Promise.all([
i18n.getLocalLang(),
i18n.extendFromSystemPath(path.join(import.meta.dirname, "i18n"))
]);
await copyStaticAssets();
const buildContext = await esbuild.context(
getSharedBuildOptions()
);
await buildContext.watch();
const { hosts: esbuildHosts, port: esbuildPort } = await buildContext.serve({
servedir: OUTPUT_DIR
});
const dataFilePath = await fs.access(
kDefaultPayloadPath
).then(() => kDefaultPayloadPath, () => undefined);
const { httpServer, cache, viewBuilder } = await buildServer(dataFilePath, {
projectRootDir: import.meta.dirname,
componentsDir: kComponentsDir,
runFromPayload: dataFilePath !== undefined,
i18n: {
english: { ui: english.ui },
french: { ui: french.ui }
},
middleware: (req, res, next) => {
if (req.url === "/esbuild") {
const proxyReq = http.request(
{
hostname: esbuildHosts[0],
port: esbuildPort,
path: req.url,
method: req.method,
headers: req.headers
},
(proxyRes) => {
res.writeHead(proxyRes.statusCode!, proxyRes.headers);
proxyRes.pipe(res);
}
);
proxyReq.on("error", (err) => {
console.error(`[proxy/esbuild] ${err.message}`);
res.writeHead(502);
res.end("Bad Gateway");
});
req.pipe(proxyReq);
return;
}
next();
}
});
const htmlWatcher = chokidar.watch(kComponentsDir, {
persistent: false,
awaitWriteFinish: true,
ignored: (path, stats) => (stats?.isFile() ?? false) && !path.endsWith(".html")
});
htmlWatcher.on("change", async(filePath) => {
await buildContext.rebuild().catch(console.error);
viewBuilder.freeCache(filePath);
});
httpServer.listen(kDevPort, () => {
console.log(`Dev server: http://localhost:${kDevPort}`);
open(`http://localhost:${kDevPort}`);
});
new WebSocketServerInstanciator({ cache, logger });
console.log("Watching...");