tencil 0.7.0 → 0.9.0

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.
@@ -0,0 +1,151 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { dirname, join, relative, sep } from "node:path";
3
+ //#region src/integrations/source-attributes.ts
4
+ /** Files whose JSX gets source attributes. */
5
+ const JSX_FILE = /\.[jt]sx$/;
6
+ /** Host elements that never get picked, or must stay untouched. */
7
+ const SKIP = /* @__PURE__ */ new Set([
8
+ "html",
9
+ "head",
10
+ "meta",
11
+ "link",
12
+ "script",
13
+ "style",
14
+ "title",
15
+ "base",
16
+ "noscript",
17
+ "template",
18
+ "slot"
19
+ ]);
20
+ /**
21
+ * Tags every host element (`<button>`, not `<Button>`) with where it's written,
22
+ * `data-tencil-src="src/card.tsx:42:7"`, and the component it's in,
23
+ * `data-tencil-component="Card"`. Production builds drop React's dev source
24
+ * info, so this is how a pin on a preview still knows its file and line.
25
+ *
26
+ * Paths are relative to `root`, the repository root, because that's what the
27
+ * cloud agent checks out. Returns undefined when there's nothing to tag or the
28
+ * file doesn't parse (the bundler will report that itself).
29
+ */
30
+ async function addSourceAttributes(code, id, root) {
31
+ const file = id.replace(/[?#].*$/, "");
32
+ if (!JSX_FILE.test(file) || file.includes(`${sep}node_modules${sep}`) || file.includes("\0")) return;
33
+ const path = relative(root, file).split(sep).join("/");
34
+ if (path.startsWith("..")) return void 0;
35
+ const { parseSync } = await import("oxc-parser");
36
+ const parsed = parseSync(file, code, {
37
+ lang: file.endsWith(".tsx") ? "tsx" : "jsx",
38
+ sourceType: "module"
39
+ });
40
+ if (parsed.errors.length > 0) return void 0;
41
+ const lines = lineStarts(code);
42
+ const inserts = [];
43
+ visit(parsed.program, void 0, (node) => {
44
+ const opening = node;
45
+ const tag = opening.name.type === "JSXIdentifier" ? opening.name.name : void 0;
46
+ if (!tag || !/^[a-z]/.test(tag) || SKIP.has(tag)) return;
47
+ if (opening.attributes.some(isTencilAttribute)) return;
48
+ const { line, column } = position(lines, opening.start);
49
+ return `${path}:${line}:${column}`;
50
+ }, inserts);
51
+ if (inserts.length === 0) return void 0;
52
+ const { default: MagicString } = await import("magic-string");
53
+ const output = new MagicString(code);
54
+ for (const insert of inserts) output.appendLeft(insert.at, insert.text);
55
+ return {
56
+ code: output.toString(),
57
+ map: output.generateMap({
58
+ source: file,
59
+ includeContent: true,
60
+ hires: "boundary"
61
+ })
62
+ };
63
+ }
64
+ /** Walks the AST, tracking the component each JSX element is written in. */
65
+ function visit(node, component, sourceOf, inserts) {
66
+ let current = component;
67
+ const named = componentName(node);
68
+ if (named) current = named;
69
+ if (node.type === "JSXOpeningElement") {
70
+ const source = sourceOf(node);
71
+ if (source) {
72
+ const name = node.name.end;
73
+ const attributes = [`data-tencil-src={${JSON.stringify(source)}}`];
74
+ if (current) attributes.push(`data-tencil-component={${JSON.stringify(current)}}`);
75
+ inserts.push({
76
+ at: name,
77
+ text: ` ${attributes.join(" ")}`
78
+ });
79
+ }
80
+ }
81
+ for (const key in node) {
82
+ if (key === "type" || key === "start" || key === "end") continue;
83
+ const value = node[key];
84
+ if (Array.isArray(value)) {
85
+ for (const child of value) if (isNode(child)) visit(child, current, sourceOf, inserts);
86
+ } else if (isNode(value)) visit(value, current, sourceOf, inserts);
87
+ }
88
+ }
89
+ /** `function Card()`, `const Card = () =>`, `const Card = memo(…)`, `class Card`. */
90
+ function componentName(node) {
91
+ let name;
92
+ if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") name = node.id?.name;
93
+ else if (node.type === "VariableDeclarator") {
94
+ const init = node.init;
95
+ if (init && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression" || init.type === "CallExpression")) name = node.id.name;
96
+ }
97
+ return name && /^[A-Z]/.test(name) ? name : void 0;
98
+ }
99
+ function isTencilAttribute(attribute) {
100
+ const name = attribute.name;
101
+ return attribute.type === "JSXAttribute" && name?.name === "data-tencil-src";
102
+ }
103
+ function isNode(value) {
104
+ return typeof value === "object" && value !== null && typeof value.type === "string";
105
+ }
106
+ function lineStarts(code) {
107
+ const starts = [0];
108
+ for (let i = 0; i < code.length; i++) if (code[i] === "\n") starts.push(i + 1);
109
+ return starts;
110
+ }
111
+ /** 1-based line and column of an offset. */
112
+ function position(starts, offset) {
113
+ let low = 0;
114
+ let high = starts.length - 1;
115
+ while (low < high) {
116
+ const middle = low + high + 1 >> 1;
117
+ if (starts[middle] <= offset) low = middle;
118
+ else high = middle - 1;
119
+ }
120
+ return {
121
+ line: low + 1,
122
+ column: offset - starts[low] + 1
123
+ };
124
+ }
125
+ const WORKSPACE_FILES = [
126
+ "pnpm-workspace.yaml",
127
+ "turbo.json",
128
+ "nx.json",
129
+ "lerna.json"
130
+ ];
131
+ /**
132
+ * The repository a build runs in: the nearest folder with `.git`, else the
133
+ * outermost workspace root (some hosts build without `.git`), else `start`.
134
+ */
135
+ function repositoryRoot(start) {
136
+ let workspace;
137
+ for (let dir = start;; dir = dirname(dir)) {
138
+ if (existsSync(join(dir, ".git"))) return dir;
139
+ if (WORKSPACE_FILES.some((name) => existsSync(join(dir, name))) || hasWorkspaces(dir)) workspace = dir;
140
+ if (dirname(dir) === dir) return workspace ?? start;
141
+ }
142
+ }
143
+ function hasWorkspaces(dir) {
144
+ try {
145
+ return "workspaces" in JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
146
+ } catch {
147
+ return false;
148
+ }
149
+ }
150
+ //#endregion
151
+ export { repositoryRoot as n, addSourceAttributes as t };
package/dist/vite.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as TencilCloudOptions } from "./cloud-jOobzTY9.mjs";
1
+ import { t as TencilCloudOptions } from "./cloud-dXtv3DCr.mjs";
2
2
  //#region src/integrations/vite.d.ts
3
3
  /** Structural subset of Vite's Plugin type, so `vite` stays an optional peer. */
4
4
  interface VitePlugin {
package/dist/vite.mjs CHANGED
@@ -1,155 +1,6 @@
1
1
  import { n as cloudLoader, r as resolveCloudBuild } from "./cloud-CiwIrP1h.mjs";
2
- import { n as overlayLoader } from "./loader-BevujQ44.mjs";
3
- import { existsSync, readFileSync } from "node:fs";
4
- import { dirname, join, relative, sep } from "node:path";
5
- //#region src/integrations/source-attributes.ts
6
- /** Files whose JSX gets source attributes. */
7
- const JSX_FILE = /\.[jt]sx$/;
8
- /** Host elements that never get picked, or must stay untouched. */
9
- const SKIP = /* @__PURE__ */ new Set([
10
- "html",
11
- "head",
12
- "meta",
13
- "link",
14
- "script",
15
- "style",
16
- "title",
17
- "base",
18
- "noscript",
19
- "template",
20
- "slot"
21
- ]);
22
- /**
23
- * Tags every host element (`<button>`, not `<Button>`) with where it's written,
24
- * `data-tencil-src="src/card.tsx:42:7"`, and the component it's in,
25
- * `data-tencil-component="Card"`. Production builds drop React's dev source
26
- * info, so this is how a pin on a preview still knows its file and line.
27
- *
28
- * Paths are relative to `root`, the repository root, because that's what the
29
- * cloud agent checks out. Returns undefined when there's nothing to tag or the
30
- * file doesn't parse (the bundler will report that itself).
31
- */
32
- async function addSourceAttributes(code, id, root) {
33
- const file = id.replace(/[?#].*$/, "");
34
- if (!JSX_FILE.test(file) || file.includes(`${sep}node_modules${sep}`) || file.includes("\0")) return;
35
- const path = relative(root, file).split(sep).join("/");
36
- if (path.startsWith("..")) return void 0;
37
- const { parseSync } = await import("oxc-parser");
38
- const parsed = parseSync(file, code, {
39
- lang: file.endsWith(".tsx") ? "tsx" : "jsx",
40
- sourceType: "module"
41
- });
42
- if (parsed.errors.length > 0) return void 0;
43
- const lines = lineStarts(code);
44
- const inserts = [];
45
- visit(parsed.program, void 0, (node) => {
46
- const opening = node;
47
- const tag = opening.name.type === "JSXIdentifier" ? opening.name.name : void 0;
48
- if (!tag || !/^[a-z]/.test(tag) || SKIP.has(tag)) return;
49
- if (opening.attributes.some(isTencilAttribute)) return;
50
- const { line, column } = position(lines, opening.start);
51
- return `${path}:${line}:${column}`;
52
- }, inserts);
53
- if (inserts.length === 0) return void 0;
54
- const { default: MagicString } = await import("magic-string");
55
- const output = new MagicString(code);
56
- for (const insert of inserts) output.appendLeft(insert.at, insert.text);
57
- return {
58
- code: output.toString(),
59
- map: output.generateMap({
60
- source: file,
61
- includeContent: true,
62
- hires: "boundary"
63
- })
64
- };
65
- }
66
- /** Walks the AST, tracking the component each JSX element is written in. */
67
- function visit(node, component, sourceOf, inserts) {
68
- let current = component;
69
- const named = componentName(node);
70
- if (named) current = named;
71
- if (node.type === "JSXOpeningElement") {
72
- const source = sourceOf(node);
73
- if (source) {
74
- const name = node.name.end;
75
- const attributes = [`data-tencil-src={${JSON.stringify(source)}}`];
76
- if (current) attributes.push(`data-tencil-component={${JSON.stringify(current)}}`);
77
- inserts.push({
78
- at: name,
79
- text: ` ${attributes.join(" ")}`
80
- });
81
- }
82
- }
83
- for (const key in node) {
84
- if (key === "type" || key === "start" || key === "end") continue;
85
- const value = node[key];
86
- if (Array.isArray(value)) {
87
- for (const child of value) if (isNode(child)) visit(child, current, sourceOf, inserts);
88
- } else if (isNode(value)) visit(value, current, sourceOf, inserts);
89
- }
90
- }
91
- /** `function Card()`, `const Card = () =>`, `const Card = memo(…)`, `class Card`. */
92
- function componentName(node) {
93
- let name;
94
- if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") name = node.id?.name;
95
- else if (node.type === "VariableDeclarator") {
96
- const init = node.init;
97
- if (init && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression" || init.type === "CallExpression")) name = node.id.name;
98
- }
99
- return name && /^[A-Z]/.test(name) ? name : void 0;
100
- }
101
- function isTencilAttribute(attribute) {
102
- const name = attribute.name;
103
- return attribute.type === "JSXAttribute" && name?.name === "data-tencil-src";
104
- }
105
- function isNode(value) {
106
- return typeof value === "object" && value !== null && typeof value.type === "string";
107
- }
108
- function lineStarts(code) {
109
- const starts = [0];
110
- for (let i = 0; i < code.length; i++) if (code[i] === "\n") starts.push(i + 1);
111
- return starts;
112
- }
113
- /** 1-based line and column of an offset. */
114
- function position(starts, offset) {
115
- let low = 0;
116
- let high = starts.length - 1;
117
- while (low < high) {
118
- const middle = low + high + 1 >> 1;
119
- if (starts[middle] <= offset) low = middle;
120
- else high = middle - 1;
121
- }
122
- return {
123
- line: low + 1,
124
- column: offset - starts[low] + 1
125
- };
126
- }
127
- const WORKSPACE_FILES = [
128
- "pnpm-workspace.yaml",
129
- "turbo.json",
130
- "nx.json",
131
- "lerna.json"
132
- ];
133
- /**
134
- * The repository a build runs in: the nearest folder with `.git`, else the
135
- * outermost workspace root (some hosts build without `.git`), else `start`.
136
- */
137
- function repositoryRoot(start) {
138
- let workspace;
139
- for (let dir = start;; dir = dirname(dir)) {
140
- if (existsSync(join(dir, ".git"))) return dir;
141
- if (WORKSPACE_FILES.some((name) => existsSync(join(dir, name))) || hasWorkspaces(dir)) workspace = dir;
142
- if (dirname(dir) === dir) return workspace ?? start;
143
- }
144
- }
145
- function hasWorkspaces(dir) {
146
- try {
147
- return "workspaces" in JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
148
- } catch {
149
- return false;
150
- }
151
- }
152
- //#endregion
2
+ import { n as overlayLoader } from "./loader-th3V-C7L.mjs";
3
+ import { n as repositoryRoot, t as addSourceAttributes } from "./source-attributes-Cs2NvuxG.mjs";
153
4
  //#region src/integrations/vite.ts
154
5
  /** Vite's dev client, which every dev page loads, whether or not it has an index.html. */
155
6
  const VITE_CLIENT = /vite\/dist\/client\/(?:client|bundledDevClient)\.mjs$/;
@@ -179,6 +30,7 @@ function tencil(options = {}) {
179
30
  },
180
31
  configResolved(config) {
181
32
  root = repositoryRoot(config.root);
33
+ if (command === "serve") loader = overlayLoader(port, root);
182
34
  },
183
35
  transform(code, id) {
184
36
  if (command === "build") return addSourceAttributes(code, id, root);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tencil",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Point at any element of your running app and tell your coding agent what to fix.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Pixel Perfect Agency",
@@ -69,7 +69,7 @@
69
69
  "oxc-parser": "^0.153.0",
70
70
  "picocolors": "^1.1.1",
71
71
  "ws": "^8.22.0",
72
- "zod": "^4.6.5"
72
+ "zod": "^4.6.6"
73
73
  },
74
74
  "peerDependencies": {
75
75
  "@anthropic-ai/claude-agent-sdk": "^0.3.293",
@@ -93,13 +93,14 @@
93
93
  "@types/ws": "^8.18.2",
94
94
  "react": "^19.3.0",
95
95
  "tsdown": "^0.23.0",
96
- "@tencildev/bridge": "0.3.0",
97
96
  "@tencildev/overlay": "0.0.0",
97
+ "@tencildev/protocol": "0.5.0",
98
+ "@tencildev/bridge": "0.5.0",
99
+ "@tencildev/provider-ensemble": "0.0.0",
98
100
  "@tencildev/provider-claude-code": "0.0.0",
99
- "@tencildev/protocol": "0.3.0",
100
- "@tencildev/provider-t3code": "0.0.0",
101
+ "@tencildev/testing": "0.3.0",
101
102
  "@tencildev/tsconfig": "0.0.0",
102
- "@tencildev/testing": "0.1.2"
103
+ "@tencildev/provider-t3code": "0.0.0"
103
104
  },
104
105
  "scripts": {
105
106
  "build": "tsdown && node scripts/copy-overlay.mjs",