Skip to content

Commit 4ea6449

Browse files
committed
feat: add semaphore to hotreload
1 parent 0fea78f commit 4ea6449

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

src/cli/dev-live-reload.tsx

+37-23
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,53 @@ import getConstants from "../constants";
66
const { LOG_PREFIX, SRC_DIR, IS_PRODUCTION } = getConstants();
77
const LIVE_RELOAD_WEBSOCKET_PATH = "__brisa_live_reload__";
88
const LIVE_RELOAD_COMMAND = "reload";
9+
let semaphore = false;
10+
let waitFilename = "";
911

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

1517
console.log(LOG_PREFIX.WAIT, `recompiling ${filename}...`);
16-
globalThis.Loader.registry.clear();
17-
18-
const nsStart = Bun.nanoseconds();
19-
const { exitCode, stderr } = Bun.spawnSync({
20-
cmd: [process.execPath, path.join(import.meta.dir, "..", "build.js")],
21-
env: process.env,
22-
stderr: "pipe",
23-
stdout: "inherit",
24-
});
25-
const nsEnd = Bun.nanoseconds();
26-
const ms = ((nsEnd - nsStart) / 1000000).toFixed(2);
27-
28-
if (exitCode !== 0) {
29-
console.log(
30-
LOG_PREFIX.ERROR,
31-
`failed to recompile ${filename}`,
32-
stderr.toString(),
33-
);
34-
return;
35-
}
36-
37-
console.log(LOG_PREFIX.READY, `recompiled successfully in ${ms}ms`);
38-
globalThis?.ws?.send(LIVE_RELOAD_COMMAND);
18+
if (semaphore) waitFilename = filename as string;
19+
else recompile(filename as string);
3920
});
4021
}
4122

23+
function recompile(filename: string) {
24+
semaphore = true;
25+
globalThis.Loader.registry.clear();
26+
27+
const nsStart = Bun.nanoseconds();
28+
const { exitCode, stderr } = Bun.spawnSync({
29+
cmd: [process.execPath, path.join(import.meta.dir, "..", "build.js")],
30+
env: process.env,
31+
stderr: "pipe",
32+
});
33+
const nsEnd = Bun.nanoseconds();
34+
const ms = ((nsEnd - nsStart) / 1000000).toFixed(2);
35+
36+
if (exitCode !== 0) {
37+
console.log(
38+
LOG_PREFIX.ERROR,
39+
`failed to recompile ${filename}`,
40+
stderr.toString(),
41+
);
42+
semaphore = false;
43+
return;
44+
}
45+
46+
console.log(LOG_PREFIX.READY, `recompiled successfully in ${ms}ms`);
47+
globalThis?.ws?.send(LIVE_RELOAD_COMMAND);
48+
if (waitFilename) {
49+
let popFilename = waitFilename;
50+
waitFilename = "";
51+
recompile(popFilename);
52+
}
53+
semaphore = false;
54+
}
55+
4256
export function LiveReloadScript({
4357
port,
4458
children,

src/cli/serve/index.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ import { ServeOptions } from "bun";
66
const { IS_PRODUCTION, BUILD_DIR, PAGES_DIR, LOG_PREFIX } = getConstants();
77

88
if (IS_PRODUCTION && !fs.existsSync(BUILD_DIR)) {
9-
console.log(LOG_PREFIX.ERROR, 'Not exist "build" yet. Please run "brisa build" first');
9+
console.log(
10+
LOG_PREFIX.ERROR,
11+
'Not exist "build" yet. Please run "brisa build" first',
12+
);
1013
process.exit(1);
1114
}
1215

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

17-
console.log(LOG_PREFIX.ERROR, `Not exist ${path}" directory. It\'s required to run "${cli}"`);
20+
console.log(
21+
LOG_PREFIX.ERROR,
22+
`Not exist ${path}" directory. It\'s required to run "${cli}"`,
23+
);
1824
process.exit(1);
1925
}
2026

0 commit comments

Comments
 (0)