@vojtaholik/static-kit-cli 2.0.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 +38 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vojtaholik/static-kit-cli",
3
- "version": "2.0.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.0.0",
20
+ "@vojtaholik/static-kit-core": "^2.1.1",
21
21
  "lightningcss": "^1.28.2",
22
22
  "browserslist": "^4.24.4"
23
23
  },
@@ -51,32 +51,36 @@ 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"));
57
-
58
- // Register all blocks at startup
59
- if (typeof blocksModule.registerAllBlocks === "function") {
60
- blocksModule.registerAllBlocks();
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
+ }
61
66
  }
62
67
 
63
- const pages: PageConfig[] = pagesModule.pages;
64
- const getPageByPath = pagesModule.getPageByPath as (
65
- path: string
66
- ) => PageConfig | undefined;
68
+ function reloadModules() {
69
+ clearModuleCache(blocksDir, pagesDir);
67
70
 
68
- // Try to load cms-blocks if it exists
69
- let cmsBlocks: Record<string, unknown> = {};
70
- try {
71
- const cmsPath = config.cmsBlocksFile
72
- ? join(cwd, config.cmsBlocksFile)
73
- : join(cwd, "cms-blocks.ts");
74
- const cmsBlocksModule = await import(`file://${cmsPath}`);
75
- cmsBlocks = cmsBlocksModule.cmsBlocks ?? cmsBlocksModule.default ?? {};
76
- } catch {
77
- // No cms-blocks file, that's fine
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;
78
80
  }
79
81
 
82
+ reloadModules();
83
+
80
84
  const PORT = config.devPort;
81
85
  const publicPath = config.publicPath; // e.g. "/public"
82
86
 
@@ -156,20 +160,29 @@ async function handleFileChange(filename: string, dir: string) {
156
160
  return;
157
161
  }
158
162
 
159
- // HTML template changed - recompile templates before reload
163
+ // HTML template changed - recompile templates, reload modules, then broadcast
160
164
  if (filename.endsWith(".block.html")) {
161
165
  console.log(`📝 Template changed: ${filename}`);
162
166
  await compileBlockTemplates({
163
167
  blocksDir,
164
168
  genDir: join(blocksDir, "gen"),
165
169
  });
166
- // bun --watch will restart the server after recompile
167
- // 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");
168
176
  return;
169
177
  }
170
178
 
171
- // For other source files, just broadcast (bun --watch handles the restart)
179
+ // Source file changed - reload modules in-process, then broadcast
172
180
  if (filename.endsWith(".ts") || filename.endsWith(".js")) {
181
+ try {
182
+ reloadModules();
183
+ } catch (err) {
184
+ console.error("⚠ Module reload failed:", err);
185
+ }
173
186
  broadcastReload("full");
174
187
  }
175
188
  }
@@ -268,7 +281,6 @@ Bun.serve({
268
281
  title: p.title,
269
282
  })),
270
283
  blocks: blockRegistry.types(),
271
- schemas: cmsBlocks,
272
284
  });
273
285
  }
274
286
 
@@ -312,7 +324,6 @@ Bun.serve({
312
324
  templateFile: `${blocksDir}/${block.type}.block.html`,
313
325
  }
314
326
  : null,
315
- schema: block ? cmsBlocks[block.type] : null,
316
327
  });
317
328
  } catch (err) {
318
329
  return Response.json(