@ikas/component-cli 1.4.0-beta.1 → 1.4.0-beta.100

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 (146) hide show
  1. package/dist/commands/add-sections-to-page.d.ts +3 -0
  2. package/dist/commands/add-sections-to-page.d.ts.map +1 -0
  3. package/dist/commands/add-sections-to-page.js +39 -0
  4. package/dist/commands/add-sections-to-page.js.map +1 -0
  5. package/dist/commands/add-to-page.d.ts +3 -0
  6. package/dist/commands/add-to-page.d.ts.map +1 -0
  7. package/dist/commands/add-to-page.js +41 -0
  8. package/dist/commands/add-to-page.js.map +1 -0
  9. package/dist/commands/add.d.ts.map +1 -1
  10. package/dist/commands/add.js +10 -1
  11. package/dist/commands/add.js.map +1 -1
  12. package/dist/commands/build.d.ts.map +1 -1
  13. package/dist/commands/build.js +5 -165
  14. package/dist/commands/build.js.map +1 -1
  15. package/dist/commands/config.d.ts.map +1 -1
  16. package/dist/commands/config.js +484 -157
  17. package/dist/commands/config.js.map +1 -1
  18. package/dist/commands/create-design-tokens.d.ts +24 -0
  19. package/dist/commands/create-design-tokens.d.ts.map +1 -0
  20. package/dist/commands/create-design-tokens.js +317 -0
  21. package/dist/commands/create-design-tokens.js.map +1 -0
  22. package/dist/commands/create-global-variable.d.ts +3 -0
  23. package/dist/commands/create-global-variable.d.ts.map +1 -0
  24. package/dist/commands/create-global-variable.js +53 -0
  25. package/dist/commands/create-global-variable.js.map +1 -0
  26. package/dist/commands/create-page.d.ts +3 -0
  27. package/dist/commands/create-page.d.ts.map +1 -0
  28. package/dist/commands/create-page.js +31 -0
  29. package/dist/commands/create-page.js.map +1 -0
  30. package/dist/commands/delete-theme-globals.d.ts +4 -0
  31. package/dist/commands/delete-theme-globals.d.ts.map +1 -0
  32. package/dist/commands/delete-theme-globals.js +48 -0
  33. package/dist/commands/delete-theme-globals.js.map +1 -0
  34. package/dist/commands/dev.d.ts.map +1 -1
  35. package/dist/commands/dev.js +299 -32
  36. package/dist/commands/dev.js.map +1 -1
  37. package/dist/commands/get-component-props.d.ts +3 -0
  38. package/dist/commands/get-component-props.d.ts.map +1 -0
  39. package/dist/commands/get-component-props.js +32 -0
  40. package/dist/commands/get-component-props.js.map +1 -0
  41. package/dist/commands/get-page-by-type.d.ts +3 -0
  42. package/dist/commands/get-page-by-type.d.ts.map +1 -0
  43. package/dist/commands/get-page-by-type.js +25 -0
  44. package/dist/commands/get-page-by-type.js.map +1 -0
  45. package/dist/commands/get-section-values.d.ts +3 -0
  46. package/dist/commands/get-section-values.d.ts.map +1 -0
  47. package/dist/commands/get-section-values.js +39 -0
  48. package/dist/commands/get-section-values.js.map +1 -0
  49. package/dist/commands/import.d.ts +3 -0
  50. package/dist/commands/import.d.ts.map +1 -0
  51. package/dist/commands/import.js +25 -0
  52. package/dist/commands/import.js.map +1 -0
  53. package/dist/commands/list-entities.d.ts +3 -0
  54. package/dist/commands/list-entities.d.ts.map +1 -0
  55. package/dist/commands/list-entities.js +32 -0
  56. package/dist/commands/list-entities.js.map +1 -0
  57. package/dist/commands/list-imported.d.ts +3 -0
  58. package/dist/commands/list-imported.d.ts.map +1 -0
  59. package/dist/commands/list-imported.js +25 -0
  60. package/dist/commands/list-imported.js.map +1 -0
  61. package/dist/commands/list-page-sections.d.ts +3 -0
  62. package/dist/commands/list-page-sections.d.ts.map +1 -0
  63. package/dist/commands/list-page-sections.js +25 -0
  64. package/dist/commands/list-page-sections.js.map +1 -0
  65. package/dist/commands/list-pages.d.ts +3 -0
  66. package/dist/commands/list-pages.d.ts.map +1 -0
  67. package/dist/commands/list-pages.js +21 -0
  68. package/dist/commands/list-pages.js.map +1 -0
  69. package/dist/commands/list-theme-globals.d.ts +3 -0
  70. package/dist/commands/list-theme-globals.d.ts.map +1 -0
  71. package/dist/commands/list-theme-globals.js +22 -0
  72. package/dist/commands/list-theme-globals.js.map +1 -0
  73. package/dist/commands/publish-theme.d.ts +3 -0
  74. package/dist/commands/publish-theme.d.ts.map +1 -0
  75. package/dist/commands/publish-theme.js +29 -0
  76. package/dist/commands/publish-theme.js.map +1 -0
  77. package/dist/commands/publish.d.ts.map +1 -1
  78. package/dist/commands/publish.js +23 -2
  79. package/dist/commands/publish.js.map +1 -1
  80. package/dist/commands/search-products.d.ts +3 -0
  81. package/dist/commands/search-products.d.ts.map +1 -0
  82. package/dist/commands/search-products.js +40 -0
  83. package/dist/commands/search-products.js.map +1 -0
  84. package/dist/commands/update-global-variable.d.ts +3 -0
  85. package/dist/commands/update-global-variable.d.ts.map +1 -0
  86. package/dist/commands/update-global-variable.js +47 -0
  87. package/dist/commands/update-global-variable.js.map +1 -0
  88. package/dist/commands/update-page-sections.d.ts +3 -0
  89. package/dist/commands/update-page-sections.d.ts.map +1 -0
  90. package/dist/commands/update-page-sections.js +39 -0
  91. package/dist/commands/update-page-sections.js.map +1 -0
  92. package/dist/commands/update-section-prop.d.ts +3 -0
  93. package/dist/commands/update-section-prop.d.ts.map +1 -0
  94. package/dist/commands/update-section-prop.js +59 -0
  95. package/dist/commands/update-section-prop.js.map +1 -0
  96. package/dist/commands/upload-image.d.ts +3 -0
  97. package/dist/commands/upload-image.d.ts.map +1 -0
  98. package/dist/commands/upload-image.js +38 -0
  99. package/dist/commands/upload-image.js.map +1 -0
  100. package/dist/commands/upload-images.d.ts +3 -0
  101. package/dist/commands/upload-images.d.ts.map +1 -0
  102. package/dist/commands/upload-images.js +48 -0
  103. package/dist/commands/upload-images.js.map +1 -0
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +55 -0
  106. package/dist/index.js.map +1 -1
  107. package/dist/types.d.ts +11 -1
  108. package/dist/types.d.ts.map +1 -1
  109. package/dist/utils/compile.d.ts +4 -1
  110. package/dist/utils/compile.d.ts.map +1 -1
  111. package/dist/utils/compile.js +630 -102
  112. package/dist/utils/compile.js.map +1 -1
  113. package/dist/utils/component-helpers.d.ts +64 -3
  114. package/dist/utils/component-helpers.d.ts.map +1 -1
  115. package/dist/utils/component-helpers.js +151 -4
  116. package/dist/utils/component-helpers.js.map +1 -1
  117. package/dist/utils/editor-action-client.d.ts +28 -0
  118. package/dist/utils/editor-action-client.d.ts.map +1 -0
  119. package/dist/utils/editor-action-client.js +116 -0
  120. package/dist/utils/editor-action-client.js.map +1 -0
  121. package/dist/utils/forbid-deep-imports.d.ts +3 -0
  122. package/dist/utils/forbid-deep-imports.d.ts.map +1 -0
  123. package/dist/utils/forbid-deep-imports.js +54 -0
  124. package/dist/utils/forbid-deep-imports.js.map +1 -0
  125. package/dist/utils/forbid-external-packages.d.ts +16 -0
  126. package/dist/utils/forbid-external-packages.d.ts.map +1 -0
  127. package/dist/utils/forbid-external-packages.js +89 -0
  128. package/dist/utils/forbid-external-packages.js.map +1 -0
  129. package/dist/utils/load-image.d.ts +16 -0
  130. package/dist/utils/load-image.d.ts.map +1 -0
  131. package/dist/utils/load-image.js +50 -0
  132. package/dist/utils/load-image.js.map +1 -0
  133. package/dist/utils/websocket-server.d.ts +52 -2
  134. package/dist/utils/websocket-server.d.ts.map +1 -1
  135. package/dist/utils/websocket-server.js +59 -1
  136. package/dist/utils/websocket-server.js.map +1 -1
  137. package/package.json +1 -1
  138. package/templates/add/section.css +11 -0
  139. package/dist/commands/create.d.ts +0 -9
  140. package/dist/commands/create.d.ts.map +0 -1
  141. package/dist/commands/create.js +0 -9
  142. package/dist/commands/create.js.map +0 -1
  143. package/dist/commands/proxy.d.ts +0 -39
  144. package/dist/commands/proxy.d.ts.map +0 -1
  145. package/dist/commands/proxy.js +0 -212
  146. package/dist/commands/proxy.js.map +0 -1
@@ -1,9 +1,10 @@
1
1
  import chalk from "chalk";
2
2
  import chokidar from "chokidar";
3
3
  import { Command } from "commander";
4
+ import * as crypto from "crypto";
4
5
  import * as fs from "fs";
5
6
  import * as path from "path";
6
- import { toPascalCase, generateTypesFile, generateGlobalTypesFile, collectUsedEnumIds, collectUsedCustomTypeIds, generateComponentFile, generateStylesFile, updateBarrelExport, generateProjectId, generateComponentId, generateUniqueId, buildPropGroupHierarchy, findPropGroup, collectPropGroupIds, movePropGroupInTree, } from "../utils/component-helpers.js";
7
+ import { toPascalCase, generateTypesFile, generateGlobalTypesFile, collectUsedEnumIds, collectUsedCustomTypeIds, shouldWriteGlobalTypes, generateComponentFile, generateStylesFile, updateBarrelExport, generateProjectId, isValidProjectId, findInvalidComponentIds, generateComponentId, generateUniqueId, buildPropGroupHierarchy, findPropGroup, collectPropGroupIds, movePropGroupInTree, validateFilteredComponentIds, } from "../utils/component-helpers.js";
7
8
  import { compileAllComponents as compileAllComponentsMulti } from "../utils/compile.js";
8
9
  import { transformEsmToWindowGlobals, transformChunkForCanvas, buildChunkMapping, } from "../utils/esm-transform.js";
9
10
  import { DevServerWebSocket, } from "../utils/websocket-server.js";
@@ -12,10 +13,20 @@ const DEV_SERVER_PORT = 5201;
12
13
  const compiledComponents = new Map();
13
14
  const compiledSharedModules = new Map();
14
15
  let compiledGlobalStyles;
16
+ // Hashes broadcast on the last cycle, used to compute deltas. Reset only on process
17
+ // startup. Editors compare these hashes (sent on the wire as `contentHash`) against their
18
+ // stored YJS value to decide whether to apply the update — so once we've seen a given hash
19
+ // here, we can safely skip broadcasting it again.
20
+ const lastBroadcastComponentHashes = new Map();
21
+ const lastBroadcastSharedHashes = new Map();
22
+ let lastBroadcastGlobalStyleHash;
15
23
  // Cached types list received from the editor via sync-types
16
24
  let cachedEditorTypes = null;
17
25
  // Cached custom type definitions received from the editor via sync-types (full IType objects)
18
26
  let cachedCustomTypes = null;
27
+ // Cached theme global variables received from the editor via sync-types.
28
+ // Drives the ThemeSettingName union in src/global-types.ts.
29
+ let cachedThemeSettings = null;
19
30
  const LIBRARY_TYPE_PREFIX = "@ikas/";
20
31
  /**
21
32
  * Match custom types for a component's props from the cached custom types.
@@ -128,12 +139,23 @@ async function compileAllComponentsForDev(config) {
128
139
  if (chunkIds.length > 0) {
129
140
  console.log(chalk.cyan(` Extracted ${chunkIds.length} shared chunk(s)`));
130
141
  }
131
- // Store global CSS (unscoped)
142
+ // Store global CSS (unscoped). Preserve `updatedAt` across rebuilds when the compiled
143
+ // global CSS is byte-identical so the editor only sees a fresh timestamp when the
144
+ // designer actually changed `global.css` — the publish UI watches this timestamp to
145
+ // decide whether globals need (re)publishing independently of any code component.
132
146
  if (result.globalCss && config.projectId) {
133
- compiledGlobalStyles = {
147
+ const previousHash = computeGlobalStyleHash(compiledGlobalStyles);
148
+ const previousUpdatedAt = compiledGlobalStyles?.updatedAt;
149
+ const next = {
134
150
  projectId: config.projectId,
135
151
  compiledCss: result.globalCss,
136
152
  };
153
+ const nextHash = computeGlobalStyleHash(next);
154
+ next.updatedAt =
155
+ nextHash === previousHash && previousUpdatedAt !== undefined
156
+ ? previousUpdatedAt
157
+ : Date.now();
158
+ compiledGlobalStyles = next;
137
159
  console.log(chalk.cyan(` Compiled global styles`));
138
160
  }
139
161
  else {
@@ -208,6 +230,7 @@ async function compileAllComponentsForDev(config) {
208
230
  compiledComponents.set(component.id, data);
209
231
  console.log(chalk.green(` Compiled: ${component.name}`));
210
232
  }
233
+ populateContentHashes();
211
234
  return true;
212
235
  }
213
236
  /**
@@ -267,6 +290,7 @@ function rebuildCompiledComponentFromConfig(config, componentId) {
267
290
  : undefined,
268
291
  };
269
292
  compiledComponents.set(componentId, updated);
293
+ populateContentHashes();
270
294
  return updated;
271
295
  }
272
296
  /**
@@ -275,6 +299,152 @@ function rebuildCompiledComponentFromConfig(config, componentId) {
275
299
  function getAllSharedModules() {
276
300
  return Array.from(compiledSharedModules.values());
277
301
  }
302
+ function sha256Hex(input) {
303
+ return crypto.createHash("sha256").update(input).digest("hex");
304
+ }
305
+ /**
306
+ * Compute `contentHash` for every entry in `compiledSharedModules` and `compiledComponents`,
307
+ * mutating each entry in place. A component's hash incorporates the hashes of its referenced
308
+ * shared modules (sorted), so any shared-util change cascades into consumers without
309
+ * requiring a separate "shared module changed" message — the consumer's own hash already
310
+ * differs. Global styles are tracked separately via `updatedAt` on the global style payload;
311
+ * they intentionally do NOT roll into component hashes (designers publish globals as a
312
+ * standalone unit).
313
+ */
314
+ function populateContentHashes() {
315
+ for (const mod of compiledSharedModules.values()) {
316
+ mod.contentHash = sha256Hex([mod.id, mod.compiledServerJs, mod.compiledClientJs, mod.canvasClientJs, mod.compiledCss].join("\0"));
317
+ }
318
+ for (const comp of compiledComponents.values()) {
319
+ const referencedHashes = (comp.usedSharedModuleIds || [])
320
+ .slice()
321
+ .sort()
322
+ .map((id) => `${id}:${compiledSharedModules.get(id)?.contentHash || ""}`)
323
+ .join("|");
324
+ comp.contentHash = sha256Hex([
325
+ comp.id,
326
+ comp.name,
327
+ comp.projectId || "",
328
+ comp.type || "component",
329
+ comp.isHeader ? "1" : "0",
330
+ comp.isFooter ? "1" : "0",
331
+ comp.compiledServerJs,
332
+ comp.compiledClientJs,
333
+ comp.compiledCss,
334
+ comp.canvasClientJs,
335
+ JSON.stringify(comp.props),
336
+ JSON.stringify(comp.propGroups || null),
337
+ JSON.stringify(comp.configPropGroups || null),
338
+ JSON.stringify(comp.customTypes || null),
339
+ (comp.usedSharedModuleIds || []).slice().sort().join(","),
340
+ referencedHashes,
341
+ ].join("\0"));
342
+ }
343
+ }
344
+ function computeGlobalStyleHash(g) {
345
+ if (!g)
346
+ return undefined;
347
+ return sha256Hex([g.projectId, g.compiledCss].join("\0"));
348
+ }
349
+ /**
350
+ * Build a `component-update` payload's `sharedModules` array for one component: the chunks
351
+ * the editor needs to resolve this component's imports. We always include the full
352
+ * transitive set (cheap) so the editor never sees a missing chunk during a partial update.
353
+ */
354
+ function sharedModulesForComponent(comp) {
355
+ if (!comp.usedSharedModuleIds || comp.usedSharedModuleIds.length === 0)
356
+ return undefined;
357
+ const out = [];
358
+ for (const id of comp.usedSharedModuleIds) {
359
+ const mod = compiledSharedModules.get(id);
360
+ if (mod)
361
+ out.push(mod);
362
+ }
363
+ return out.length > 0 ? out : undefined;
364
+ }
365
+ /**
366
+ * Diff current compile state against `lastBroadcast*` maps and decide what to send:
367
+ *
368
+ * - Nothing changed → no broadcast at all (this is the whole point — quiet rebuilds).
369
+ * - Exactly one component changed, no deletes, no global change → single
370
+ * `component-update` (cheap, no full-state replay on the editor).
371
+ * - Anything more (multi-component, deletes, global style change) → one bundled
372
+ * `initial-state`. We deliberately collapse multi-component changes into ONE message so
373
+ * the editor doesn't tick through N sequential `markUpdating` / refresh cycles. The
374
+ * editor's `autoSyncToTheme` uses `contentHash` to skip YJS writes for components that
375
+ * didn't actually change, so the full-state payload only mutates what really moved.
376
+ *
377
+ * Newly-connecting editors always receive a full `initial-state` via `onRequestState` in
378
+ * websocket-server.ts and are independent of this path.
379
+ */
380
+ function broadcastCompileDelta(wsServer) {
381
+ const currentGlobalHash = computeGlobalStyleHash(compiledGlobalStyles);
382
+ const globalStylesChanged = currentGlobalHash !== lastBroadcastGlobalStyleHash;
383
+ const changedComponentIds = [];
384
+ for (const [id, comp] of compiledComponents) {
385
+ const prev = lastBroadcastComponentHashes.get(id);
386
+ if (prev !== comp.contentHash)
387
+ changedComponentIds.push(id);
388
+ }
389
+ const deletedComponentIds = [];
390
+ for (const id of lastBroadcastComponentHashes.keys()) {
391
+ if (!compiledComponents.has(id))
392
+ deletedComponentIds.push(id);
393
+ }
394
+ let changedSharedCount = 0;
395
+ for (const [id, mod] of compiledSharedModules) {
396
+ if (lastBroadcastSharedHashes.get(id) !== mod.contentHash)
397
+ changedSharedCount++;
398
+ }
399
+ const result = {
400
+ changedComponents: changedComponentIds.length,
401
+ deletedComponents: deletedComponentIds.length,
402
+ changedSharedModules: changedSharedCount,
403
+ globalStylesChanged,
404
+ };
405
+ const nothingChanged = changedComponentIds.length === 0 &&
406
+ deletedComponentIds.length === 0 &&
407
+ !globalStylesChanged;
408
+ if (nothingChanged)
409
+ return result;
410
+ // Single-component edit fast path: avoid making the editor clear+refill its devComponents
411
+ // map and run autoSyncToTheme for every component. One targeted update is enough.
412
+ const canUseSingleUpdate = changedComponentIds.length === 1 &&
413
+ deletedComponentIds.length === 0 &&
414
+ !globalStylesChanged;
415
+ if (canUseSingleUpdate) {
416
+ const comp = compiledComponents.get(changedComponentIds[0]);
417
+ wsServer.broadcastComponentUpdate(comp, sharedModulesForComponent(comp));
418
+ lastBroadcastComponentHashes.set(comp.id, comp.contentHash);
419
+ }
420
+ else {
421
+ for (const id of deletedComponentIds) {
422
+ wsServer.broadcastComponentDeleted(id);
423
+ lastBroadcastComponentHashes.delete(id);
424
+ }
425
+ wsServer.broadcast({
426
+ type: "initial-state",
427
+ payload: {
428
+ components: getAllComponents(),
429
+ sharedModules: getAllSharedModules(),
430
+ globalStyles: compiledGlobalStyles,
431
+ timestamp: Date.now(),
432
+ },
433
+ });
434
+ lastBroadcastComponentHashes.clear();
435
+ for (const [id, comp] of compiledComponents) {
436
+ if (comp.contentHash)
437
+ lastBroadcastComponentHashes.set(id, comp.contentHash);
438
+ }
439
+ }
440
+ lastBroadcastSharedHashes.clear();
441
+ for (const [id, mod] of compiledSharedModules) {
442
+ if (mod.contentHash)
443
+ lastBroadcastSharedHashes.set(id, mod.contentHash);
444
+ }
445
+ lastBroadcastGlobalStyleHash = currentGlobalHash;
446
+ return result;
447
+ }
278
448
  /**
279
449
  * Read and return the current config from disk
280
450
  */
@@ -294,12 +464,20 @@ function writeConfig(configPath, config) {
294
464
  function writeGlobalTypesFile(config) {
295
465
  const globalTypesPath = path.resolve(process.cwd(), "src/global-types.ts");
296
466
  const usedEnumIds = collectUsedEnumIds(config.components);
297
- const content = generateGlobalTypesFile(cachedEditorTypes, cachedCustomTypes, usedEnumIds);
298
- // Avoid unnecessary writes (prevents file watcher loops)
299
- if (fs.existsSync(globalTypesPath)) {
300
- const existing = fs.readFileSync(globalTypesPath, "utf-8");
301
- if (existing === content)
302
- return;
467
+ const content = generateGlobalTypesFile(cachedEditorTypes, cachedCustomTypes, usedEnumIds, cachedThemeSettings);
468
+ // Single read instead of existsSync + readFileSync; absent file reads as null.
469
+ let existing = null;
470
+ try {
471
+ existing = fs.readFileSync(globalTypesPath, "utf-8");
472
+ }
473
+ catch {
474
+ existing = null;
475
+ }
476
+ // Skip unnecessary writes (file-watcher loops) and, crucially, don't let the
477
+ // startup write clobber editor-synced theme globals before the editor has
478
+ // connected and re-synced them.
479
+ if (!shouldWriteGlobalTypes(existing, content, cachedThemeSettings != null)) {
480
+ return;
303
481
  }
304
482
  fs.writeFileSync(globalTypesPath, content);
305
483
  }
@@ -323,7 +501,10 @@ function createEditorHandlers(configPath, getConfig, setConfig, wsServer) {
323
501
  const broadcastConfigChange = (componentId) => {
324
502
  const updated = rebuildCompiledComponentFromConfig(getConfig(), componentId);
325
503
  if (updated) {
326
- wsServer.broadcastComponentUpdate(updated);
504
+ wsServer.broadcastComponentUpdate(updated, sharedModulesForComponent(updated));
505
+ if (updated.contentHash) {
506
+ lastBroadcastComponentHashes.set(updated.id, updated.contentHash);
507
+ }
327
508
  }
328
509
  };
329
510
  return {
@@ -342,6 +523,12 @@ function createEditorHandlers(configPath, getConfig, setConfig, wsServer) {
342
523
  if (config.components.some((c) => c.name === pascalName)) {
343
524
  throw new Error(`Component "${pascalName}" already exists`);
344
525
  }
526
+ // No `!` assertion: the config may have been hot-reloaded since startup, and
527
+ // minting with a missing/malformed projectId writes a corrupt component id
528
+ // into ikas.config.json that later publishes verbatim.
529
+ if (!config.projectId || !isValidProjectId(config.projectId)) {
530
+ throw new Error("Invalid or missing projectId in ikas.config.json — restore the generated value and restart the dev server");
531
+ }
345
532
  const componentId = generateComponentId(config.projectId);
346
533
  const componentDir = path.resolve(process.cwd(), `src/components/${pascalName}`);
347
534
  // Create directory and files
@@ -395,6 +582,14 @@ function createEditorHandlers(configPath, getConfig, setConfig, wsServer) {
395
582
  }
396
583
  }
397
584
  }
585
+ if (prop.filteredComponentIds && prop.filteredComponentIds.length > 0) {
586
+ const { unknown } = validateFilteredComponentIds(prop.filteredComponentIds, config.components);
587
+ if (unknown.length > 0) {
588
+ throw new Error(`Unknown filteredComponentIds: ${unknown.join(", ")}. ` +
589
+ `Component ids are opaque and cannot be derived from names. ` +
590
+ `Known components: ${config.components.map((c) => `${c.name} (${c.id})`).join(", ")}.`);
591
+ }
592
+ }
398
593
  component.props.push(prop);
399
594
  writeConfig(configPath, config);
400
595
  setConfig(config);
@@ -415,6 +610,14 @@ function createEditorHandlers(configPath, getConfig, setConfig, wsServer) {
415
610
  if (propIndex === -1) {
416
611
  throw new Error(`Prop "${propName}" not found on ${component.name}`);
417
612
  }
613
+ if (prop.filteredComponentIds && prop.filteredComponentIds.length > 0) {
614
+ const { unknown } = validateFilteredComponentIds(prop.filteredComponentIds, config.components);
615
+ if (unknown.length > 0) {
616
+ throw new Error(`Unknown filteredComponentIds: ${unknown.join(", ")}. ` +
617
+ `Component ids are opaque and cannot be derived from names. ` +
618
+ `Known components: ${config.components.map((c) => `${c.name} (${c.id})`).join(", ")}.`);
619
+ }
620
+ }
418
621
  component.props[propIndex] = prop;
419
622
  writeConfig(configPath, config);
420
623
  setConfig(config);
@@ -685,8 +888,10 @@ function createEditorHandlers(configPath, getConfig, setConfig, wsServer) {
685
888
  regenerateAllComponentTypes(config);
686
889
  console.log(chalk.green(` Deleted enum: ${removed.name} (${enumId})`));
687
890
  },
688
- onSyncTypes: (types, customTypes) => {
891
+ onSyncTypes: (types, customTypes, themeSettings) => {
689
892
  cachedEditorTypes = types;
893
+ if (themeSettings)
894
+ cachedThemeSettings = themeSettings;
690
895
  // Merge incoming custom types with existing ones instead of replacing.
691
896
  // Custom types belong to the code component project and should not be
692
897
  // lost when a different editor project connects.
@@ -765,6 +970,27 @@ async function startDevServer(opts = {}) {
765
970
  fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
766
971
  console.log(chalk.yellow(` Auto-generated projectId: ${config.projectId}`));
767
972
  }
973
+ else if (!isValidProjectId(config.projectId)) {
974
+ // A malformed projectId (hand-edited config) breaks the editor's republish
975
+ // stability: published/local id prefixes stop matching and every publish
976
+ // INSERTs duplicate design elements. Fail fast instead of corrupting data.
977
+ console.error(chalk.red(`Error: invalid projectId "${config.projectId}" in ikas.config.json`));
978
+ console.error(chalk.gray("projectId must be at least 6 alphanumeric characters (no '-' or '_'). " +
979
+ "Restore the original generated value — do not hand-edit it."));
980
+ process.exit(1);
981
+ }
982
+ // Component ids carry the same prefix/suffix contract as projectId; the dev
983
+ // server syncs them into the editor (and from there into themes), so a
984
+ // hand-edited id must be rejected before the server starts.
985
+ const invalidComponents = findInvalidComponentIds(config.components);
986
+ if (invalidComponents.length > 0) {
987
+ console.error(chalk.red("Error: invalid component id(s) in ikas.config.json:"));
988
+ invalidComponents.forEach((c) => {
989
+ console.error(chalk.red(` - "${c.id}"${c.name ? ` (${c.name})` : ""}`));
990
+ });
991
+ console.error(chalk.gray("Component ids are generated — restore the original values; do not hand-edit them."));
992
+ process.exit(1);
993
+ }
768
994
  console.log(chalk.cyan("\n ikas Component Dev Server\n"));
769
995
  console.log(chalk.gray(` Project: ${config.name}`));
770
996
  console.log(chalk.gray(` Components: ${config.components.length}\n`));
@@ -792,6 +1018,18 @@ async function startDevServer(opts = {}) {
792
1018
  // Initial compilation of all components (multi-entry with code splitting)
793
1019
  console.log(chalk.cyan(" Compiling components..."));
794
1020
  await compileAllComponentsForDev(config);
1021
+ // Seed the broadcast-hash maps so the first delta-broadcast after startup doesn't replay
1022
+ // the initial state — newly-connecting editors get a full `initial-state` directly via
1023
+ // `onRequestState`, independent of the delta path.
1024
+ for (const [id, comp] of compiledComponents) {
1025
+ if (comp.contentHash)
1026
+ lastBroadcastComponentHashes.set(id, comp.contentHash);
1027
+ }
1028
+ for (const [id, mod] of compiledSharedModules) {
1029
+ if (mod.contentHash)
1030
+ lastBroadcastSharedHashes.set(id, mod.contentHash);
1031
+ }
1032
+ lastBroadcastGlobalStyleHash = computeGlobalStyleHash(compiledGlobalStyles);
795
1033
  console.log("");
796
1034
  // Start WebSocket server
797
1035
  const wsServer = new DevServerWebSocket(DEV_SERVER_PORT);
@@ -832,16 +1070,15 @@ async function startDevServer(opts = {}) {
832
1070
  console.log(chalk.yellow(` Recompiling all (triggered by: ${triggeredBy})...`));
833
1071
  const success = await compileAllComponentsForDev(config);
834
1072
  if (success) {
835
- // Send full state to all connected editors
836
- wsServer.broadcast({
837
- type: "initial-state",
838
- payload: {
839
- components: getAllComponents(),
840
- sharedModules: getAllSharedModules(),
841
- globalStyles: compiledGlobalStyles,
842
- timestamp: Date.now(),
843
- },
844
- });
1073
+ const delta = broadcastCompileDelta(wsServer);
1074
+ if (delta.changedComponents === 0 &&
1075
+ delta.deletedComponents === 0 &&
1076
+ !delta.globalStylesChanged) {
1077
+ console.log(chalk.gray(" No editor-visible changes (skipped broadcast)"));
1078
+ }
1079
+ else {
1080
+ console.log(chalk.cyan(` Broadcast delta: ${delta.changedComponents} changed, ${delta.deletedComponents} deleted, ${delta.changedSharedModules} shared chunks changed${delta.globalStylesChanged ? ", globals updated" : ""}`));
1081
+ }
845
1082
  }
846
1083
  else {
847
1084
  // Broadcast error for the triggering component if identifiable
@@ -858,20 +1095,33 @@ async function startDevServer(opts = {}) {
858
1095
  if (normalizedPath === configPath) {
859
1096
  console.log(chalk.yellow("\n Config changed, reloading..."));
860
1097
  try {
861
- config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
1098
+ const reloaded = JSON.parse(fs.readFileSync(configPath, "utf-8"));
1099
+ // The startup guards validated projectId/component ids once; a hot-reload
1100
+ // must not smuggle a hand-edited value past them — a malformed id reaching
1101
+ // `generateComponentId` or chunk namespacing corrupts republish matching.
1102
+ // Keep the session's validated projectId if the key was removed; reject the
1103
+ // reload outright (previous config stays active) on invalid values.
1104
+ if (!reloaded.projectId) {
1105
+ reloaded.projectId = config.projectId;
1106
+ }
1107
+ else if (!isValidProjectId(reloaded.projectId)) {
1108
+ console.error(chalk.red(` Ignoring config reload: invalid projectId "${reloaded.projectId}". `) +
1109
+ chalk.gray("Restore the original generated value — previous config stays active."));
1110
+ return;
1111
+ }
1112
+ const reloadedInvalidComponents = findInvalidComponentIds(reloaded.components ?? []);
1113
+ if (reloadedInvalidComponents.length > 0) {
1114
+ console.error(chalk.red(` Ignoring config reload: invalid component id(s): ${reloadedInvalidComponents
1115
+ .map((c) => `"${c.id}"`)
1116
+ .join(", ")}. `) + chalk.gray("Restore the original generated values — previous config stays active."));
1117
+ return;
1118
+ }
1119
+ config = reloaded;
862
1120
  cachedCustomTypes = config.customTypes || [];
863
1121
  writeGlobalTypesFile(config);
864
1122
  await compileAllComponentsForDev(config);
865
- // Send full state to all connected editors
866
- wsServer.broadcast({
867
- type: "initial-state",
868
- payload: {
869
- components: getAllComponents(),
870
- sharedModules: getAllSharedModules(),
871
- globalStyles: compiledGlobalStyles,
872
- timestamp: Date.now(),
873
- },
874
- });
1123
+ const delta = broadcastCompileDelta(wsServer);
1124
+ console.log(chalk.cyan(` Config reload delta: ${delta.changedComponents} changed, ${delta.deletedComponents} deleted, ${delta.changedSharedModules} shared chunks changed${delta.globalStylesChanged ? ", globals updated" : ""}`));
875
1125
  }
876
1126
  catch (e) {
877
1127
  console.error(chalk.red(" Failed to reload config"));
@@ -898,6 +1148,23 @@ async function startDevServer(opts = {}) {
898
1148
  const entryPath = path.resolve(process.cwd(), component.entry);
899
1149
  if (!fs.existsSync(entryPath)) {
900
1150
  compiledComponents.delete(component.id);
1151
+ // Sync ikas.config.json: remove the component entry and strip any orphaned
1152
+ // filteredComponentIds references in remaining components' props. Write config
1153
+ // before broadcasting so an editor requesting state sees the post-delete view.
1154
+ const idx = config.components.findIndex((c) => c.id === component.id);
1155
+ if (idx !== -1) {
1156
+ config.components.splice(idx, 1);
1157
+ for (const remaining of config.components) {
1158
+ for (const prop of remaining.props || []) {
1159
+ if (!prop.filteredComponentIds)
1160
+ continue;
1161
+ prop.filteredComponentIds = prop.filteredComponentIds.filter((id) => id !== component.id);
1162
+ if (prop.filteredComponentIds.length === 0)
1163
+ delete prop.filteredComponentIds;
1164
+ }
1165
+ }
1166
+ writeConfig(configPath, config);
1167
+ }
901
1168
  wsServer.broadcastComponentDeleted(component.id);
902
1169
  console.log(chalk.red(` Component removed: ${component.name}`));
903
1170
  }