@vojtaholik/static-kit-cli 2.1.0 → 2.1.1

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 +2 -2
  2. package/src/commands/dev.ts +39 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vojtaholik/static-kit-cli",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "static-kit": "./src/cli.ts"
@@ -17,7 +17,7 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@vojtaholik/static-kit-core": "^2.1.0",
20
+ "@vojtaholik/static-kit-core": "^2.1.1",
21
21
  "lightningcss": "^1.28.2",
22
22
  "browserslist": "^4.24.4"
23
23
  },
@@ -51,19 +51,35 @@ try {
51
51
  // svg/ directory doesn't exist, skip silently
52
52
  }
53
53
 
54
- // Dynamically import site's blocks and pages
55
- const blocksModule = await import(join(blocksDir, "index.ts"));
56
- const pagesModule = await import(join(pagesDir, "index.ts"));
54
+ // Hot-reloadable module loader
55
+ // bun --watch can't trace dynamic import() with computed paths,
56
+ // so we use require() + cache clearing to reload pages/blocks in-process.
57
+ let pages: PageConfig[];
58
+ let getPageByPath: (path: string) => PageConfig | undefined;
59
+
60
+ function clearModuleCache(...dirs: string[]) {
61
+ for (const key of Object.keys(require.cache)) {
62
+ if (dirs.some((dir) => key.startsWith(dir))) {
63
+ delete require.cache[key];
64
+ }
65
+ }
66
+ }
67
+
68
+ function reloadModules() {
69
+ clearModuleCache(blocksDir, pagesDir);
57
70
 
58
- // Register all blocks at startup
59
- if (typeof blocksModule.registerAllBlocks === "function") {
60
- blocksModule.registerAllBlocks();
71
+ blockRegistry.clear();
72
+ const blocksModule = require(join(blocksDir, "index.ts"));
73
+ if (typeof blocksModule.registerAllBlocks === "function") {
74
+ blocksModule.registerAllBlocks();
75
+ }
76
+
77
+ const pagesModule = require(join(pagesDir, "index.ts"));
78
+ pages = pagesModule.pages;
79
+ getPageByPath = pagesModule.getPageByPath;
61
80
  }
62
81
 
63
- const pages: PageConfig[] = pagesModule.pages;
64
- const getPageByPath = pagesModule.getPageByPath as (
65
- path: string
66
- ) => PageConfig | undefined;
82
+ reloadModules();
67
83
 
68
84
  const PORT = config.devPort;
69
85
  const publicPath = config.publicPath; // e.g. "/public"
@@ -144,20 +160,29 @@ async function handleFileChange(filename: string, dir: string) {
144
160
  return;
145
161
  }
146
162
 
147
- // HTML template changed - recompile templates before reload
163
+ // HTML template changed - recompile templates, reload modules, then broadcast
148
164
  if (filename.endsWith(".block.html")) {
149
165
  console.log(`📝 Template changed: ${filename}`);
150
166
  await compileBlockTemplates({
151
167
  blocksDir,
152
168
  genDir: join(blocksDir, "gen"),
153
169
  });
154
- // bun --watch will restart the server after recompile
155
- // which triggers browser reload via SSE reconnect
170
+ try {
171
+ reloadModules();
172
+ } catch (err) {
173
+ console.error("⚠ Module reload failed:", err);
174
+ }
175
+ broadcastReload("full");
156
176
  return;
157
177
  }
158
178
 
159
- // For other source files, just broadcast (bun --watch handles the restart)
179
+ // Source file changed - reload modules in-process, then broadcast
160
180
  if (filename.endsWith(".ts") || filename.endsWith(".js")) {
181
+ try {
182
+ reloadModules();
183
+ } catch (err) {
184
+ console.error("⚠ Module reload failed:", err);
185
+ }
161
186
  broadcastReload("full");
162
187
  }
163
188
  }