koguma 2.3.0 → 2.3.2

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 ADDED
@@ -0,0 +1,48 @@
1
+ // src/vite.ts
2
+ import { createHash } from "node:crypto";
3
+ import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
4
+ import { join } from "node:path";
5
+ function kogumaHMR() {
6
+ const HASH_FILENAME = "dbhash";
7
+ const HASH_DIR = ".koguma";
8
+ const HASH_SUBPATH = `${HASH_DIR}/${HASH_FILENAME}`;
9
+ const SQLITE_GLOB = "**/.koguma/.wrangler/state/v3/d1/**/*.sqlite";
10
+ return {
11
+ name: "koguma-hmr",
12
+ configureServer(server) {
13
+ const root = server.config.root;
14
+ const hashDir = join(root, HASH_DIR);
15
+ const hashPath = join(root, HASH_SUBPATH);
16
+ server.watcher.add(SQLITE_GLOB);
17
+ server.watcher.on("change", (file) => {
18
+ if (!file.includes(".koguma/.wrangler/state/v3/d1") || !file.endsWith(".sqlite"))
19
+ return;
20
+ try {
21
+ const hash = createHash("sha256").update(readFileSync(file)).digest("hex");
22
+ if (!existsSync(hashDir))
23
+ mkdirSync(hashDir, { recursive: true });
24
+ writeFileSync(hashPath, hash);
25
+ } catch {}
26
+ });
27
+ server.watcher.add(hashPath);
28
+ },
29
+ handleHotUpdate({ file, server }) {
30
+ if (file.endsWith(HASH_FILENAME) && file.includes(HASH_DIR)) {
31
+ server.ws.send({ type: "full-reload" });
32
+ return [];
33
+ }
34
+ },
35
+ config() {
36
+ return {
37
+ server: {
38
+ watch: {
39
+ ignored: ["**/content/**"]
40
+ }
41
+ }
42
+ };
43
+ }
44
+ };
45
+ }
46
+ export {
47
+ kogumaHMR
48
+ };
package/src/vite.ts CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  import { createHash } from 'node:crypto';
14
14
  import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
15
- import { resolve } from 'node:path';
15
+ import { join } from 'node:path';
16
16
  import type { Plugin } from 'vite';
17
17
 
18
18
  /**
@@ -28,22 +28,27 @@ import type { Plugin } from 'vite';
28
28
  * browser refetches fresh content on each reload.
29
29
  */
30
30
  export function kogumaHMR(): Plugin {
31
- const HASH_FILE = '.koguma/dbhash';
31
+ const HASH_FILENAME = 'dbhash';
32
+ const HASH_DIR = '.koguma';
33
+ const HASH_SUBPATH = `${HASH_DIR}/${HASH_FILENAME}`;
34
+
35
+ // Glob passed to chokidar — must NOT go through path.resolve or the ** breaks
36
+ const SQLITE_GLOB = '**/.koguma/.wrangler/state/v3/d1/**/*.sqlite';
32
37
 
33
38
  return {
34
39
  name: 'koguma-hmr',
35
40
 
36
41
  configureServer(server) {
37
42
  const root = server.config.root;
38
- const hashPath = resolve(root, HASH_FILE);
39
- const kogumaDir = resolve(root, '.koguma');
43
+ const hashDir = join(root, HASH_DIR);
44
+ const hashPath = join(root, HASH_SUBPATH);
40
45
 
41
- // Watch the wrangler local D1 directory for SQLite changes
42
- server.watcher.add(resolve(root, '.wrangler/state/v3/d1/**/*.sqlite'));
46
+ // Add the wrangler SQLite glob directly (no resolve — globs need literal **)
47
+ server.watcher.add(SQLITE_GLOB);
43
48
 
44
49
  server.watcher.on('change', (file: string) => {
45
50
  if (
46
- !file.includes('.wrangler/state/v3/d1') ||
51
+ !file.includes('.koguma/.wrangler/state/v3/d1') ||
47
52
  !file.endsWith('.sqlite')
48
53
  )
49
54
  return;
@@ -51,19 +56,26 @@ export function kogumaHMR(): Plugin {
51
56
  const hash = createHash('sha256')
52
57
  .update(readFileSync(file))
53
58
  .digest('hex');
54
- if (!existsSync(kogumaDir)) mkdirSync(kogumaDir, { recursive: true });
59
+ if (!existsSync(hashDir)) mkdirSync(hashDir, { recursive: true });
55
60
  writeFileSync(hashPath, hash);
56
61
  } catch {
57
62
  // SQLite may be locked mid-write — the next write will succeed
58
63
  }
59
64
  });
60
65
 
61
- // Watch the hash file as the actual reload trigger.
62
- // Vite's default behaviour for a watched file not in the module graph
63
- // is a full-reload — no handleHotUpdate override needed.
66
+ // Watch the hash file Vite does NOT auto-reload non-module files,
67
+ // so handleHotUpdate below is required to send the full-reload signal.
64
68
  server.watcher.add(hashPath);
65
69
  },
66
70
 
71
+ handleHotUpdate({ file, server }) {
72
+ // When the hash file changes, the DB has new content → reload the page
73
+ if (file.endsWith(HASH_FILENAME) && file.includes(HASH_DIR)) {
74
+ server.ws.send({ type: 'full-reload' });
75
+ return [];
76
+ }
77
+ },
78
+
67
79
  config() {
68
80
  return {
69
81
  server: {