-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild-des.config.mjs
62 lines (55 loc) · 1.88 KB
/
esbuild-des.config.mjs
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
// Recarga viva (live reloading) durante desarrollo
// Recompila automáticamente ante cambios en javascript de app/javascript
// y refresca automáticamente en navegador
// Basado en https://www.colby.so/posts/live-reloading-with-esbuild-and-rails
import * as path from "path"
import * as http from "http"
const watch = process.argv.includes('--watch')
const clients = []
const watchOptions = {
onRebuild: (error, result) => {
if (error) {
console.error('Falló construcción:', error)
} else {
console.log('Construcción exitosa')
clients.forEach((res) => res.write('data: update\n\n'))
clients.length = 0
}
}
}
import * as esbuild from 'esbuild'
if (watch && watchOptions) {
let ctx = await esbuild.context({
entryPoints: ['application.js'],
bundle: true,
preserveSymlinks: true,
outdir: path.join(process.cwd(), "app/assets/builds"),
absWorkingDir: path.join(process.cwd(), "app/javascript"),
banner: {
js: ` (() => new EventSource("http://${process.env.MAQRECVIVA}:${process.env.PUERTORECVIVA}").onmessage = () => location.reload())();`,
}
})
await ctx.watch()
} else {
let result = esbuild.build({
entryPoints: ["application.js"],
bundle: true,
preserveSymlinks: true,
outdir: path.join(process.cwd(), "app/assets/builds"),
absWorkingDir: path.join(process.cwd(), "app/javascript"),
banner: {
js: ` (() => new EventSource("http://${process.env.MAQRECVIVA}:${process.env.PUERTORECVIVA}").onmessage = () => location.reload())();`,
}
}).catch(() => process.exit(1));
console.log(result);
}
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(process.env.PUERTORECVIVA, process.env.IPDES);