-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.js
54 lines (50 loc) · 1.43 KB
/
esbuild.config.js
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
const chokidar = require("chokidar");
const esbuild = require("esbuild");
const http = require("http");
const path = require("path");
const clients = [];
const watch = process.argv.includes("--watch");
const watchedDirectories = [
"./app/javascript/**/*.js",
"./app/views/**/*.html.erb",
"./app/assets/stylesheets/*.css",
];
const bannerJs = watch
? ' (() => new EventSource("http://localhost:8082").onmessage = () => location.reload())();'
: "";
const config = {
entryPoints: ["application.js"],
bundle: true,
sourcemap: true,
incremental: watch,
outdir: path.join(process.cwd(), "app/assets/builds"),
absWorkingDir: path.join(process.cwd(), "app/javascript"),
banner: { js: bannerJs },
};
if (watch) {
http
.createServer((req, res) => {
return clients.push(
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*",
Connection: "keep-alive",
})
);
})
.listen(8082);
(async () => {
const result = await esbuild.build(config);
chokidar.watch(watchedDirectories).on("all", (event, path) => {
if (path.includes("javascript")) {
console.log(`rebuilding ${path}`);
result.rebuild();
}
clients.forEach((res) => res.write("data: update\n\n"));
clients.length = 0;
});
})();
} else {
esbuild.build(config).catch(() => process.exit(1));
}