Created
April 12, 2022 15:41
-
-
Save timm-oh/d6a22ada6c7eda1e4b18de578d9eebd3 to your computer and use it in GitHub Desktop.
esbuild config file with live reloading for rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import esbuild from "esbuild" | |
import chokidar from "chokidar" | |
import http from "http" | |
const watchDirectories = [ | |
"./app/assets/builds/*", | |
"./app/views/**/*.erb", | |
] | |
const watch = process.argv.includes("--watch") | |
const clients = [] | |
const config = { | |
entryPoints: ["app/assets/javascript/application.js"], | |
bundle: true, | |
outdir: "app/assets/builds", | |
watch: watch, | |
banner: { | |
js: ' (() => new EventSource("http://localhost:8082").onmessage = () => location.reload())();', | |
}, | |
} | |
if (watch) { | |
(async () => { | |
await esbuild.build(config); | |
chokidar.watch(watchDirectories).on('all', (event, path) => { | |
console.log(`rebuilding ${path}`) | |
clients.forEach((res) => res.write('data: update\n\n')) | |
clients.length = 0 | |
}) | |
})(); | |
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); | |
} else { | |
esbuild | |
.build(config) | |
.catch(error => { | |
console.log(error) | |
process.exit(1) | |
}) | |
} | |
// Anything Vendor related | |
esbuild.build({ | |
entryPoints: ["vendor/blazer/javascript/application.js"], | |
bundle: true, | |
outfile: "app/assets/builds/blazer/application.js", | |
watch: process.argv.includes("--watch") | |
}).catch(error => { | |
console.log(error) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment