Skip to content

Commit

Permalink
feat: add semaphore to hotreload
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 10, 2023
1 parent 0fea78f commit 4ea6449
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
60 changes: 37 additions & 23 deletions src/cli/dev-live-reload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,53 @@ import getConstants from "../constants";
const { LOG_PREFIX, SRC_DIR, IS_PRODUCTION } = getConstants();
const LIVE_RELOAD_WEBSOCKET_PATH = "__brisa_live_reload__";
const LIVE_RELOAD_COMMAND = "reload";
let semaphore = false;
let waitFilename = "";

if (!IS_PRODUCTION) {
console.log(LOG_PREFIX.INFO, "hot reloading enabled");
watch(SRC_DIR, { recursive: true }, async (event, filename) => {
if (event !== "change") return;

console.log(LOG_PREFIX.WAIT, `recompiling ${filename}...`);
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",
stdout: "inherit",
});
const nsEnd = Bun.nanoseconds();
const ms = ((nsEnd - nsStart) / 1000000).toFixed(2);

if (exitCode !== 0) {
console.log(
LOG_PREFIX.ERROR,
`failed to recompile ${filename}`,
stderr.toString(),
);
return;
}

console.log(LOG_PREFIX.READY, `recompiled successfully in ${ms}ms`);
globalThis?.ws?.send(LIVE_RELOAD_COMMAND);
if (semaphore) waitFilename = filename as string;
else recompile(filename as string);
});
}

function recompile(filename: string) {
semaphore = true;
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 nsEnd = Bun.nanoseconds();
const ms = ((nsEnd - nsStart) / 1000000).toFixed(2);

if (exitCode !== 0) {
console.log(
LOG_PREFIX.ERROR,
`failed to recompile ${filename}`,
stderr.toString(),
);
semaphore = false;
return;
}

console.log(LOG_PREFIX.READY, `recompiled successfully in ${ms}ms`);
globalThis?.ws?.send(LIVE_RELOAD_COMMAND);
if (waitFilename) {
let popFilename = waitFilename;
waitFilename = "";
recompile(popFilename);
}
semaphore = false;
}

export function LiveReloadScript({
port,
children,
Expand Down
10 changes: 8 additions & 2 deletions src/cli/serve/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import { ServeOptions } from "bun";
const { IS_PRODUCTION, BUILD_DIR, PAGES_DIR, LOG_PREFIX } = getConstants();

if (IS_PRODUCTION && !fs.existsSync(BUILD_DIR)) {
console.log(LOG_PREFIX.ERROR, 'Not exist "build" yet. Please run "brisa build" first');
console.log(
LOG_PREFIX.ERROR,
'Not exist "build" yet. Please run "brisa build" first',
);
process.exit(1);
}

if (!fs.existsSync(PAGES_DIR)) {
const path = IS_PRODUCTION ? "build/pages" : "src/pages";
const cli = IS_PRODUCTION ? "brisa start" : "brisa dev";

console.log(LOG_PREFIX.ERROR, `Not exist ${path}" directory. It\'s required to run "${cli}"`);
console.log(
LOG_PREFIX.ERROR,
`Not exist ${path}" directory. It\'s required to run "${cli}"`,
);
process.exit(1);
}

Expand Down

0 comments on commit 4ea6449

Please sign in to comment.