Skip to content

Commit

Permalink
perf: improve little bit hotreload
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 11, 2023
1 parent 640370a commit 751a218
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/cli/dev-live-reload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ import { watch } from "node:fs";
import path from "node:path";
import dangerHTML from "../core/danger-html";
import getConstants from "../constants";
import { SpawnOptions } from "bun";

type Spawn = SpawnOptions.OptionsObject<
SpawnOptions.Writable,
SpawnOptions.Readable,
SpawnOptions.Readable
> & { cmd: string[] };

const { LOG_PREFIX, SRC_DIR, IS_PRODUCTION } = getConstants();
const LIVE_RELOAD_WEBSOCKET_PATH = "__brisa_live_reload__";
const LIVE_RELOAD_COMMAND = "reload";
const buildPath = path.join(import.meta.dir, "..", "build.js");
const spawnOptions: Spawn = {
cmd: [process.execPath, buildPath],
env: process.env,
stderr: "pipe",
};

let semaphore = false;
let waitFilename = "";

if (!IS_PRODUCTION) {
console.log(LOG_PREFIX.INFO, "hot reloading enabled");
watch(SRC_DIR, { recursive: true }, async (event, filename) => {
const filePath = path.join(SRC_DIR, filename as string);
const createdOrRemoved = Bun.file(filePath).size === 0;

if (!createdOrRemoved && event !== "change") return;
if (event !== "change" && Bun.file(filePath).size !== 0) return;

console.log(LOG_PREFIX.WAIT, `recompiling ${filename}...`);
if (semaphore) waitFilename = filename as string;
Expand All @@ -28,11 +41,7 @@ function recompile(filename: string) {
globalThis.Loader.registry.clear();

const nsStart = Bun.nanoseconds();
const { exitCode, stderr } = Bun.spawnSync({
cmd: [process.execPath, path.join(import.meta.dir, "..", "build.js")],
env: process.env,
stderr: "pipe",
});
const { exitCode, stderr } = Bun.spawnSync(spawnOptions);
const nsEnd = Bun.nanoseconds();
const ms = ((nsEnd - nsStart) / 1000000).toFixed(2);

Expand Down

0 comments on commit 751a218

Please sign in to comment.