Skip to content

Instantly share code, notes, and snippets.

@timm-oh
Created April 12, 2022 15:41
Show Gist options
  • Select an option

  • Save timm-oh/d6a22ada6c7eda1e4b18de578d9eebd3 to your computer and use it in GitHub Desktop.

Select an option

Save timm-oh/d6a22ada6c7eda1e4b18de578d9eebd3 to your computer and use it in GitHub Desktop.

Revisions

  1. timm-oh created this gist Apr 12, 2022.
    63 changes: 63 additions & 0 deletions build.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/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)
    })