@archie/devtools 0.0.21 → 0.0.23

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/dist/index.mjs CHANGED
@@ -178,6 +178,36 @@ function babelPluginDndAuto({ types: t }, opts = {}) {
178
178
 
179
179
  // src/babel-plugin-editor-meta.ts
180
180
  import * as nodePath from "path";
181
+ var KIT_SLOT_ATTR = "data-kit-slot";
182
+ var MAX_SLOT_SEGMENTS = 4;
183
+ var NON_BINDING_ATTRS = /* @__PURE__ */ new Set(["__editorMeta", "key", "ref", KIT_SLOT_ATTR]);
184
+ var STRUCTURAL_ATTRS = /* @__PURE__ */ new Set([
185
+ "id",
186
+ "htmlFor",
187
+ "slot",
188
+ "name",
189
+ "type",
190
+ "role",
191
+ // The camelCase carrier of the SAME durable key as `data-agent-id`: kit
192
+ // components take `agentId` and render it as the data attribute. Found on the
193
+ // real corpus (×4) — blocking only the dashed form would have left the prop
194
+ // form routable, and a durable key with a string-literal usage value sails
195
+ // straight through the usage-value law.
196
+ "agentId"
197
+ ]);
198
+ function isNonContentAttribute(name) {
199
+ if (/^on[A-Z]/.test(name)) return true;
200
+ if (name.startsWith("data-")) return true;
201
+ return STRUCTURAL_ATTRS.has(name);
202
+ }
203
+ function normalizeSlot(raw) {
204
+ const value = raw.trim();
205
+ if (!value) return null;
206
+ const segments = value.split("/");
207
+ if (segments.length > MAX_SLOT_SEGMENTS) return null;
208
+ if (segments.some((segment) => segment.trim() === "")) return null;
209
+ return value;
210
+ }
181
211
  function isPackageImport(src) {
182
212
  if (src.startsWith(".") || src.startsWith("/")) return false;
183
213
  if (src.startsWith("@")) {
@@ -193,7 +223,8 @@ function babelPluginEditorMeta({ types: t }, opts = {}) {
193
223
  const {
194
224
  fileExclude = /node_modules/,
195
225
  instrumentImported = true,
196
- includeHostSourceRef = false
226
+ includeHostSourceRef = false,
227
+ origin = "screen"
197
228
  } = opts;
198
229
  function getJSXTagName(el) {
199
230
  const { name } = el;
@@ -306,29 +337,275 @@ function babelPluginEditorMeta({ types: t }, opts = {}) {
306
337
  }
307
338
  return false;
308
339
  }
340
+ function buildPayloadFilter(payload) {
341
+ return t.callExpression(
342
+ t.arrowFunctionExpression(
343
+ [
344
+ t.objectPattern([
345
+ t.objectProperty(
346
+ t.identifier("__editorMeta"),
347
+ t.identifier("_$em"),
348
+ false,
349
+ false
350
+ ),
351
+ t.objectProperty(
352
+ t.stringLiteral(KIT_SLOT_ATTR),
353
+ t.identifier("_$ks"),
354
+ false,
355
+ false
356
+ ),
357
+ t.restElement(t.identifier("_$r"))
358
+ ])
359
+ ],
360
+ t.identifier("_$r")
361
+ ),
362
+ [t.objectExpression([t.spreadElement(payload)])]
363
+ );
364
+ }
309
365
  function sanitizeSpreadAttributes(attrs) {
310
366
  for (const attr of attrs) {
311
367
  if (!t.isJSXSpreadAttribute(attr)) continue;
312
368
  if (isMetaStripSpreadExpression(attr.argument)) continue;
313
- const origArg = attr.argument;
314
- attr.argument = t.callExpression(
315
- t.arrowFunctionExpression(
316
- [
317
- t.objectPattern([
318
- t.objectProperty(
319
- t.identifier("__editorMeta"),
320
- t.identifier("_$em"),
321
- false,
322
- false
323
- ),
324
- t.restElement(t.identifier("_$r"))
325
- ])
326
- ],
327
- t.identifier("_$r")
328
- ),
329
- [t.objectExpression([t.spreadElement(origArg)])]
330
- );
369
+ attr.argument = buildPayloadFilter(attr.argument);
370
+ }
371
+ }
372
+ function consumeKitSlot(attrs) {
373
+ let slot = null;
374
+ let found = false;
375
+ for (const attr of attrs) {
376
+ if (!t.isJSXAttribute(attr)) continue;
377
+ if (!t.isJSXIdentifier(attr.name, { name: KIT_SLOT_ATTR })) continue;
378
+ found = true;
379
+ if (slot !== null) continue;
380
+ const { value } = attr;
381
+ if (t.isStringLiteral(value)) {
382
+ slot = normalizeSlot(value.value);
383
+ } else if (t.isJSXExpressionContainer(value) && t.isStringLiteral(value.expression)) {
384
+ slot = normalizeSlot(value.expression.value);
385
+ }
331
386
  }
387
+ if (!found) return { slot: null, found: false, attrs };
388
+ return {
389
+ slot,
390
+ found: true,
391
+ attrs: attrs.filter(
392
+ (attr) => !(t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name, { name: KIT_SLOT_ATTR }))
393
+ )
394
+ };
395
+ }
396
+ function getFunctionName(fn) {
397
+ const node = fn.node;
398
+ if (t.isFunctionDeclaration(node) && node.id) return node.id.name;
399
+ if (t.isFunctionExpression(node) && node.id) return node.id.name;
400
+ const parent = fn.parentPath;
401
+ if (parent?.isVariableDeclarator() && t.isIdentifier(parent.node.id)) {
402
+ return parent.node.id.name;
403
+ }
404
+ return null;
405
+ }
406
+ function buildPropMap(pattern) {
407
+ const map = /* @__PURE__ */ new Map();
408
+ for (const prop of pattern.properties) {
409
+ if (!t.isObjectProperty(prop) || prop.computed) continue;
410
+ const key = t.isIdentifier(prop.key) ? prop.key.name : t.isStringLiteral(prop.key) ? prop.key.value : null;
411
+ if (!key) continue;
412
+ const value = prop.value;
413
+ if (t.isIdentifier(value)) {
414
+ map.set(value.name, key);
415
+ } else if (t.isAssignmentPattern(value) && t.isIdentifier(value.left)) {
416
+ map.set(value.left.name, key);
417
+ }
418
+ }
419
+ return map;
420
+ }
421
+ function resolvePropContext(path2) {
422
+ let fn = path2.getFunctionParent();
423
+ while (fn) {
424
+ const [first] = fn.node.params;
425
+ if (first && t.isObjectPattern(first)) {
426
+ if (fn.scope.parent !== fn.scope.getProgramParent()) return null;
427
+ return {
428
+ componentScope: fn.scope,
429
+ propMap: buildPropMap(first),
430
+ componentName: getFunctionName(fn)
431
+ };
432
+ }
433
+ fn = fn.getFunctionParent();
434
+ }
435
+ return null;
436
+ }
437
+ function resolveProp(name, scope, ctx) {
438
+ const prop = ctx.propMap.get(name);
439
+ if (!prop) return null;
440
+ const binding = scope.getBinding(name);
441
+ if (!binding) return null;
442
+ if (binding.scope !== ctx.componentScope) return null;
443
+ if (!binding.constant) return null;
444
+ return prop;
445
+ }
446
+ function isScopeWithinComponent(scope, ctx) {
447
+ let current = scope;
448
+ while (current) {
449
+ if (current === ctx.componentScope) return true;
450
+ current = current.parent;
451
+ }
452
+ return false;
453
+ }
454
+ function resolveLocalConstInit(name, scope, ctx) {
455
+ const binding = scope.getBinding(name);
456
+ if (!binding) return null;
457
+ if (!binding.constant) return null;
458
+ const declarator = binding.path;
459
+ if (!declarator.isVariableDeclarator()) return null;
460
+ if (!isScopeWithinComponent(binding.scope, ctx)) return null;
461
+ const init = declarator.node.init;
462
+ if (!init || !t.isExpression(init)) return null;
463
+ return { init, scope: declarator.scope };
464
+ }
465
+ function isStaticStringLiteral(node) {
466
+ if (t.isStringLiteral(node)) return true;
467
+ return t.isTemplateLiteral(node) && node.expressions.length === 0;
468
+ }
469
+ function collectPropIdentifiers(node, scope, ctx, out = []) {
470
+ if (!node) return out;
471
+ const visit = (child) => collectPropIdentifiers(child, scope, ctx, out);
472
+ if (t.isIdentifier(node)) {
473
+ const prop = resolveProp(node.name, scope, ctx);
474
+ if (prop && !out.includes(prop)) out.push(prop);
475
+ return out;
476
+ }
477
+ if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
478
+ visit(node.object);
479
+ if (node.computed) visit(node.property);
480
+ return out;
481
+ }
482
+ if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) {
483
+ visit(node.callee);
484
+ node.arguments.forEach((arg) => visit(arg));
485
+ return out;
486
+ }
487
+ if (t.isTemplateLiteral(node)) {
488
+ node.expressions.forEach((expr) => visit(expr));
489
+ return out;
490
+ }
491
+ if (t.isConditionalExpression(node)) {
492
+ visit(node.test);
493
+ visit(node.consequent);
494
+ visit(node.alternate);
495
+ return out;
496
+ }
497
+ if (t.isLogicalExpression(node) || t.isBinaryExpression(node)) {
498
+ visit(node.left);
499
+ visit(node.right);
500
+ return out;
501
+ }
502
+ if (t.isUnaryExpression(node) || t.isAwaitExpression(node)) {
503
+ visit(node.argument);
504
+ return out;
505
+ }
506
+ if (t.isArrayExpression(node)) {
507
+ node.elements.forEach((el) => visit(el));
508
+ return out;
509
+ }
510
+ if (t.isObjectExpression(node)) {
511
+ for (const prop of node.properties) {
512
+ if (t.isObjectProperty(prop)) {
513
+ if (prop.computed) visit(prop.key);
514
+ visit(prop.value);
515
+ } else if (t.isSpreadElement(prop)) {
516
+ visit(prop.argument);
517
+ }
518
+ }
519
+ return out;
520
+ }
521
+ if (t.isSpreadElement(node)) {
522
+ visit(node.argument);
523
+ return out;
524
+ }
525
+ if (t.isTSAsExpression(node) || t.isTSNonNullExpression(node) || t.isTypeCastExpression(node)) {
526
+ visit(node.expression);
527
+ return out;
528
+ }
529
+ return out;
530
+ }
531
+ function classifyExpression(expr, scope, ctx, hop) {
532
+ if (t.isIdentifier(expr)) {
533
+ const prop = resolveProp(expr.name, scope, ctx);
534
+ if (prop) return { kind: "direct", prop };
535
+ if (hop > 0) return null;
536
+ const local = resolveLocalConstInit(expr.name, scope, ctx);
537
+ if (!local) return null;
538
+ const inner = classifyExpression(local.init, local.scope, ctx, hop + 1);
539
+ if (!inner) return null;
540
+ return { kind: inner.kind === "formatted" ? "formatted" : "derived", prop: inner.prop };
541
+ }
542
+ if (t.isLogicalExpression(expr) && (expr.operator === "||" || expr.operator === "??") && isStaticStringLiteral(expr.right)) {
543
+ const left = classifyExpression(expr.left, scope, ctx, hop);
544
+ if (!left) return null;
545
+ return { kind: left.kind === "formatted" ? "formatted" : "derived", prop: left.prop };
546
+ }
547
+ const props = collectPropIdentifiers(expr, scope, ctx);
548
+ return props.length > 0 ? { kind: "formatted", prop: props[0] } : null;
549
+ }
550
+ function meaningfulChildren(children) {
551
+ return children.filter((child) => {
552
+ if (t.isJSXText(child)) return child.value.trim() !== "";
553
+ if (t.isJSXExpressionContainer(child)) return !t.isJSXEmptyExpression(child.expression);
554
+ return true;
555
+ });
556
+ }
557
+ function collectPropBindings(path2, attrs) {
558
+ const ctx = resolvePropContext(path2);
559
+ if (!ctx || ctx.propMap.size === 0) return { bindings: [], owner: null };
560
+ if (ctx.componentName === null) return { bindings: [], owner: null };
561
+ const scope = path2.scope;
562
+ const bindings = [];
563
+ const elementPath = path2.parentPath;
564
+ if (elementPath && elementPath.isJSXElement()) {
565
+ const children = meaningfulChildren(elementPath.node.children);
566
+ const isSoleChild = children.length === 1;
567
+ for (const child of children) {
568
+ if (!t.isJSXExpressionContainer(child)) continue;
569
+ const expression = child.expression;
570
+ if (t.isJSXEmptyExpression(expression)) continue;
571
+ const verdict = classifyExpression(expression, scope, ctx, 0);
572
+ if (!verdict) continue;
573
+ bindings.push({
574
+ target: "children",
575
+ prop: verdict.prop,
576
+ kind: isSoleChild ? verdict.kind : "formatted"
577
+ });
578
+ }
579
+ }
580
+ for (const attr of attrs) {
581
+ if (!t.isJSXAttribute(attr)) continue;
582
+ const name = t.isJSXIdentifier(attr.name) ? attr.name.name : `${attr.name.namespace.name}:${attr.name.name.name}`;
583
+ if (NON_BINDING_ATTRS.has(name)) continue;
584
+ const value = attr.value;
585
+ if (!t.isJSXExpressionContainer(value)) continue;
586
+ const expression = value.expression;
587
+ if (t.isJSXEmptyExpression(expression)) continue;
588
+ const verdict = classifyExpression(expression, scope, ctx, 0);
589
+ if (!verdict) continue;
590
+ bindings.push({
591
+ target: `attr:${name}`,
592
+ prop: verdict.prop,
593
+ // M2b: machine-contract attributes are reported, never offered.
594
+ kind: isNonContentAttribute(name) ? "formatted" : verdict.kind
595
+ });
596
+ }
597
+ return { bindings, owner: bindings.length > 0 ? ctx.componentName : null };
598
+ }
599
+ function propBindingsToAst(bindings) {
600
+ return t.arrayExpression(
601
+ bindings.map(
602
+ (binding) => t.objectExpression([
603
+ t.objectProperty(t.identifier("target"), t.stringLiteral(binding.target)),
604
+ t.objectProperty(t.identifier("prop"), t.stringLiteral(binding.prop)),
605
+ t.objectProperty(t.identifier("kind"), t.stringLiteral(binding.kind))
606
+ ])
607
+ )
608
+ );
332
609
  }
333
610
  return {
334
611
  name: "babel-plugin-editor-meta",
@@ -356,25 +633,13 @@ function babelPluginEditorMeta({ types: t }, opts = {}) {
356
633
  if (!propsArg || t.isNullLiteral(propsArg)) return;
357
634
  if (!t.isExpression(propsArg)) return;
358
635
  if (isMetaStripSpreadExpression(propsArg)) return;
359
- args[1] = t.callExpression(
360
- t.arrowFunctionExpression(
361
- [
362
- t.objectPattern([
363
- t.objectProperty(
364
- t.identifier("__editorMeta"),
365
- t.identifier("_$em"),
366
- false,
367
- false
368
- ),
369
- t.restElement(t.identifier("_$r"))
370
- ])
371
- ],
372
- t.identifier("_$r")
373
- ),
374
- [t.objectExpression([t.spreadElement(propsArg)])]
375
- );
636
+ args[1] = buildPayloadFilter(propsArg);
376
637
  },
377
638
  JSXOpeningElement(path2, state) {
639
+ const consumed = consumeKitSlot(path2.node.attributes);
640
+ path2.node.attributes = consumed.attrs;
641
+ const slot = consumed.slot;
642
+ const hasSlotAttr = consumed.found;
378
643
  const filename = state.filename ?? "";
379
644
  if (fileExclude.test(filename)) return;
380
645
  const existing = path2.node.attributes.find(
@@ -420,9 +685,11 @@ function babelPluginEditorMeta({ types: t }, opts = {}) {
420
685
  }
421
686
  }
422
687
  const nodeId = computeNodeId(file, line, col, componentName);
423
- const movable = isMovable(path2);
688
+ const isKitOrigin = origin === "kit" || hasSlotAttr;
689
+ const movable = isKitOrigin ? false : isMovable(path2);
424
690
  const parentNodeId = getParentNodeId(path2, file);
425
691
  const staticPropsObj = extractStaticProps(path2.node.attributes);
692
+ const { bindings, owner } = collectPropBindings(path2, path2.node.attributes);
426
693
  const metaObj = t.objectExpression([
427
694
  t.objectProperty(t.identifier("nodeId"), t.stringLiteral(nodeId)),
428
695
  t.objectProperty(t.identifier("file"), t.stringLiteral(file)),
@@ -439,6 +706,22 @@ function babelPluginEditorMeta({ types: t }, opts = {}) {
439
706
  parentNodeId ? t.stringLiteral(parentNodeId) : t.nullLiteral()
440
707
  )
441
708
  ]);
709
+ if (slot !== null) {
710
+ metaObj.properties.push(
711
+ t.objectProperty(t.identifier("slot"), t.stringLiteral(slot))
712
+ );
713
+ }
714
+ if (bindings.length > 0) {
715
+ metaObj.properties.push(
716
+ t.objectProperty(t.identifier("propBindings"), propBindingsToAst(bindings))
717
+ );
718
+ metaObj.properties.push(
719
+ t.objectProperty(
720
+ t.identifier("propOwner"),
721
+ owner ? t.stringLiteral(owner) : t.nullLiteral()
722
+ )
723
+ );
724
+ }
442
725
  if (isHostElement && includeHostSourceRef) {
443
726
  path2.node.attributes.push(
444
727
  t.jsxAttribute(
@@ -447,10 +730,11 @@ function babelPluginEditorMeta({ types: t }, opts = {}) {
447
730
  )
448
731
  );
449
732
  }
733
+ const metaThunk = t.arrowFunctionExpression([], metaObj);
450
734
  path2.node.attributes.push(
451
735
  t.jsxAttribute(
452
736
  t.jsxIdentifier("__editorMeta"),
453
- t.jsxExpressionContainer(metaObj)
737
+ t.jsxExpressionContainer(metaThunk)
454
738
  )
455
739
  );
456
740
  }
@@ -489,16 +773,28 @@ function archieDevTools(options = {}) {
489
773
  },
490
774
  configureServer(server) {
491
775
  server.middlewares.use(LOCAL_INSPECTOR_SERVE_PATH, (_req, res) => {
492
- const standaloneFile = join(__dirname, "../src/client/inject-inspector/inspector.standalone.js");
493
- try {
494
- const content = readFileSync(standaloneFile, "utf-8");
495
- res.setHeader("Content-Type", "application/javascript; charset=utf-8");
496
- res.setHeader("Cache-Control", "no-store");
497
- res.end(content);
498
- } catch {
499
- res.statusCode = 404;
500
- res.end("Not found");
776
+ const candidates = [
777
+ join(__dirname, "client/inject-inspector/inspector.standalone.js"),
778
+ join(__dirname, "../src/client/inject-inspector/inspector.standalone.js")
779
+ ];
780
+ for (const candidate of candidates) {
781
+ try {
782
+ const content = readFileSync(candidate, "utf-8");
783
+ res.setHeader(
784
+ "Content-Type",
785
+ "application/javascript; charset=utf-8"
786
+ );
787
+ res.setHeader("Cache-Control", "no-store");
788
+ res.end(content);
789
+ return;
790
+ } catch {
791
+ }
501
792
  }
793
+ server.config.logger.warn(
794
+ `[archie-devtools] inspector.standalone.js not found at ${LOCAL_INSPECTOR_SERVE_PATH}. Tried: ${candidates.join(", ")}. Did you build with \`pnpm build:local\`?`
795
+ );
796
+ res.statusCode = 404;
797
+ res.end("Not found");
502
798
  });
503
799
  },
504
800
  async transform(code, id) {