@jsnchn/buntastic 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +71 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsnchn/buntastic",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "A simple static site generator built with Bun",
5
5
  "type": "module",
6
6
  "bin": {
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { mkdir, writeFile, readFile, copyFile, exists } from "fs/promises";
2
- import { watch as fsWatch } from "fs/promises";
2
+ import { watch as fsWatch } from "node:fs";
3
+ import { spawn } from "node:child_process";
3
4
  import { join, relative, dirname, extname } from "path";
4
5
  import { Glob } from "bun";
5
6
 
@@ -8,6 +9,28 @@ const LAYOUTS_DIR = join(process.cwd(), "src/layouts");
8
9
  const PUBLIC_DIR = join(process.cwd(), "public");
9
10
  const DIST_DIR = join(process.cwd(), "dist");
10
11
 
12
+ const MIME_TYPES: Record<string, string> = {
13
+ ".html": "text/html",
14
+ ".css": "text/css",
15
+ ".js": "application/javascript",
16
+ ".json": "application/json",
17
+ ".png": "image/png",
18
+ ".jpg": "image/jpeg",
19
+ ".jpeg": "image/jpeg",
20
+ ".gif": "image/gif",
21
+ ".svg": "image/svg+xml",
22
+ ".ico": "image/x-icon",
23
+ ".woff": "font/woff",
24
+ ".woff2": "font/woff2",
25
+ ".ttf": "font/ttf",
26
+ ".eot": "application/vnd.ms-fontobject",
27
+ };
28
+
29
+ function getMimeType(filePath: string): string {
30
+ const ext = extname(filePath).toLowerCase();
31
+ return MIME_TYPES[ext] || "application/octet-stream";
32
+ }
33
+
11
34
  interface Frontmatter {
12
35
  title?: string;
13
36
  date?: string;
@@ -263,6 +286,16 @@ async function dev(): Promise<void> {
263
286
  const url = new URL(req.url);
264
287
  let path = url.pathname;
265
288
 
289
+ const distPath = join(DIST_DIR, path);
290
+
291
+ if (await exists(distPath)) {
292
+ const file = Bun.file(distPath);
293
+ const mimeType = getMimeType(distPath);
294
+ return new Response(file, {
295
+ headers: { "Content-Type": mimeType },
296
+ });
297
+ }
298
+
266
299
  if (path === "/") {
267
300
  path = "/index";
268
301
  }
@@ -294,48 +327,36 @@ async function dev(): Promise<void> {
294
327
 
295
328
  await build(false);
296
329
 
297
- const watcher1 = fsWatch(CONTENT_DIR, { recursive: true });
298
- const watcher2 = fsWatch(LAYOUTS_DIR, { recursive: true });
299
- const watcher3 = fsWatch(PUBLIC_DIR, { recursive: true });
330
+ let buildRunning = false;
331
+ let debounceTimer: ReturnType<typeof setTimeout> | undefined;
300
332
 
301
- const watchers = [watcher1, watcher2, watcher3];
333
+ function runBuild() {
334
+ if (buildRunning) return;
335
+ buildRunning = true;
336
+ console.log("Rebuilding...");
337
+ const proc = spawn("bun", ["run", "build"], {
338
+ stdio: "inherit",
339
+ env: { ...process.env, NODE_ENV: "development" },
340
+ });
341
+ proc.on("exit", () => {
342
+ buildRunning = false;
343
+ });
344
+ }
302
345
 
303
- let isBuilding = false;
304
- let debounceTimer: ReturnType<typeof setTimeout> | undefined;
305
- const rebuild = () => {
306
- clearTimeout(debounceTimer);
307
- debounceTimer = setTimeout(async () => {
308
- if (isBuilding) return;
309
- isBuilding = true;
310
- console.log("Rebuilding...");
311
- try {
312
- await build(false);
313
- } finally {
314
- isBuilding = false;
315
- }
316
- }, 100);
317
- };
346
+ function scheduleBuild() {
347
+ if (debounceTimer) clearTimeout(debounceTimer);
348
+ debounceTimer = setTimeout(runBuild, 100);
349
+ }
318
350
 
319
- (async () => {
320
- for (const watcher of watchers) {
321
- (async () => {
322
- for await (const event of watcher) {
323
- const filename = event.filename || "";
324
- const isPublicFile = filename.includes("public");
325
-
326
- if (isPublicFile) {
327
- const ext = filename.split(".").pop()?.toLowerCase();
328
- if (ext !== "css" && ext !== "js" && ext !== "ts") {
329
- continue;
330
- }
331
- }
332
-
333
- console.log(`[${event.eventType}] ${event.filename} - rebuilding...`);
334
- rebuild();
335
- }
336
- })();
337
- }
338
- })();
351
+ const watchDirs = [CONTENT_DIR, LAYOUTS_DIR, PUBLIC_DIR];
352
+ for (const dir of watchDirs) {
353
+ fsWatch(dir, { recursive: true }, (eventType, filename) => {
354
+ if (!filename) return;
355
+ if (filename.endsWith("~") || filename.startsWith(".")) return;
356
+ console.log(`[${eventType}] ${filename} - rebuilding...`);
357
+ scheduleBuild();
358
+ });
359
+ }
339
360
  }
340
361
 
341
362
  async function preview(): Promise<void> {
@@ -348,6 +369,16 @@ async function preview(): Promise<void> {
348
369
  const url = new URL(req.url);
349
370
  let path = url.pathname;
350
371
 
372
+ const distPath = join(DIST_DIR, path);
373
+
374
+ if (await exists(distPath)) {
375
+ const file = Bun.file(distPath);
376
+ const mimeType = getMimeType(distPath);
377
+ return new Response(file, {
378
+ headers: { "Content-Type": mimeType },
379
+ });
380
+ }
381
+
351
382
  if (path === "/") {
352
383
  path = "/index";
353
384
  }