koguma 2.3.4 → 2.3.5

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.
package/src/vite.js CHANGED
@@ -1,29 +1,19 @@
1
1
  // src/vite.ts
2
- import { createHash } from "node:crypto";
3
- import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
2
+ import { existsSync, mkdirSync } from "node:fs";
4
3
  import { join } from "node:path";
5
4
  function kogumaHMR(options = {}) {
6
- const HASH_DIR = options.kogumaDir ?? ".koguma";
5
+ const KOGUMA_DIR = options.kogumaDir ?? ".koguma";
7
6
  const HASH_FILE = "dbhash";
8
- const HASH_SUBPATH = `${HASH_DIR}/${HASH_FILE}`;
9
7
  return {
10
8
  name: "koguma-hmr",
11
9
  configureServer(server) {
12
10
  const root = server.config.root;
13
- const hashDir = join(root, HASH_DIR);
14
- const hashPath = join(root, HASH_SUBPATH);
15
- const wranglerDir = join(root, HASH_DIR, ".wrangler");
16
- if (!existsSync(hashDir))
17
- mkdirSync(hashDir, { recursive: true });
18
- server.watcher.add(hashDir);
11
+ const kogumaDir = join(root, KOGUMA_DIR);
12
+ const hashPath = join(kogumaDir, HASH_FILE);
13
+ if (!existsSync(kogumaDir))
14
+ mkdirSync(kogumaDir, { recursive: true });
15
+ server.watcher.add(hashPath);
19
16
  server.watcher.on("change", (file) => {
20
- if (file.startsWith(wranglerDir) && file.endsWith(".sqlite")) {
21
- try {
22
- const hash = createHash("sha256").update(readFileSync(file)).digest("hex");
23
- writeFileSync(hashPath, hash);
24
- } catch {}
25
- return;
26
- }
27
17
  if (file === hashPath) {
28
18
  server.ws.send({ type: "full-reload" });
29
19
  }
package/src/vite.ts CHANGED
@@ -10,8 +10,7 @@
10
10
  * });
11
11
  */
12
12
 
13
- import { createHash } from 'node:crypto';
14
- import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
13
+ import { existsSync, mkdirSync } from 'node:fs';
15
14
  import { join } from 'node:path';
16
15
  import type { Plugin } from 'vite';
17
16
 
@@ -25,52 +24,36 @@ export interface KogumaHMROptions {
25
24
  }
26
25
 
27
26
  /**
28
- * Watches the wrangler local D1 SQLite file and triggers a full browser reload
29
- * when content changes. Uses a SHA-256 hash file in .koguma/dbhash to debounce
30
- * the multiple WAL writes that SQLite generates per D1 operation.
27
+ * Triggers a full browser reload when koguma syncs content to D1.
28
+ *
29
+ * How it works:
30
+ * 1. koguma's dev-sync writes a timestamp to .koguma/dbhash after each
31
+ * successful content sync (file→D1 or dashboard→file).
32
+ * 2. This plugin watches .koguma/dbhash via chokidar and sends a
33
+ * full-reload to the Vite WebSocket client when it changes.
31
34
  *
32
35
  * Also suppresses Vite HMR for content/ since those changes sync through
33
36
  * koguma's own pipeline → D1 → API, not through the Vite module graph.
34
37
  */
35
38
  export function kogumaHMR(options: KogumaHMROptions = {}): Plugin {
36
- const HASH_DIR = options.kogumaDir ?? '.koguma';
39
+ const KOGUMA_DIR = options.kogumaDir ?? '.koguma';
37
40
  const HASH_FILE = 'dbhash';
38
- const HASH_SUBPATH = `${HASH_DIR}/${HASH_FILE}`;
39
41
 
40
42
  return {
41
43
  name: 'koguma-hmr',
42
44
 
43
45
  configureServer(server) {
44
46
  const root = server.config.root;
45
- const hashDir = join(root, HASH_DIR);
46
- const hashPath = join(root, HASH_SUBPATH);
47
- const wranglerDir = join(root, HASH_DIR, '.wrangler');
47
+ const kogumaDir = join(root, KOGUMA_DIR);
48
+ const hashPath = join(kogumaDir, HASH_FILE);
48
49
 
49
- // Ensure .koguma exists so chokidar can watch it
50
- if (!existsSync(hashDir)) mkdirSync(hashDir, { recursive: true });
50
+ // Ensure .koguma dir exists so chokidar can watch the file
51
+ if (!existsSync(kogumaDir)) mkdirSync(kogumaDir, { recursive: true });
51
52
 
52
- // Watch .koguma/ directorycovers both the SQLite files inside
53
- // .wrangler/ and the dbhash file we write ourselves.
54
- server.watcher.add(hashDir);
53
+ // Watch just the hash file written by koguma dev-sync on real syncs
54
+ server.watcher.add(hashPath);
55
55
 
56
56
  server.watcher.on('change', (file: string) => {
57
- // SQLite changed → compute hash and write dbhash file.
58
- // The next change event for dbhash will trigger the reload.
59
- if (file.startsWith(wranglerDir) && file.endsWith('.sqlite')) {
60
- try {
61
- const hash = createHash('sha256')
62
- .update(readFileSync(file))
63
- .digest('hex');
64
- writeFileSync(hashPath, hash);
65
- } catch {
66
- // SQLite may be locked mid-write — next write will succeed
67
- }
68
- return;
69
- }
70
-
71
- // dbhash changed → new content is in D1 → reload the page.
72
- // We send directly from here rather than via handleHotUpdate because
73
- // handleHotUpdate only fires for files in Vite's module graph.
74
57
  if (file === hashPath) {
75
58
  server.ws.send({ type: 'full-reload' });
76
59
  }