@useonsite/vite 0.1.0 → 0.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.
package/README.md CHANGED
@@ -71,6 +71,17 @@ The quick chat sits just under the element, or above it when there is no room be
71
71
 
72
72
  A frame on the element shows each step while the agent works. For more room, open the quick chat in the full chat with <kbd>⌘</kbd>/<kbd>Ctrl</kbd> + <kbd>⇧ Shift</kbd> + <kbd>Enter</kbd>, or tap <kbd>⌥</kbd> twice. Your draft and selection come along. <kbd>Esc</kbd> or a click outside closes the quick chat; the draft stays. With the panel open, <kbd>⌥</kbd>-click adds elements to the panel.
73
73
 
74
+ ### What you can select
75
+
76
+ A click selects the nearest element that carries a source stamp. In dev, Onsite adds a stamp (`data-onsite-src`, the file, line and column) to these JSX elements in your source folders:
77
+
78
+ - Each HTML or SVG element, such as `<div>` or `<button>`.
79
+ - Each component from a package, such as `<WaButton>` from Web Awesome, `<Button>` from MUI or `<Dialog.Root>` from Radix. The stamp names the line in your file that uses the component.
80
+
81
+ A component from a package is selectable when it passes `data-*` props on to the element it renders. Most UI libraries do, and so do the React wrappers that `@lit/react` makes, such as Web Awesome's. When a component does not pass them on, a click selects the nearest stamped element around it.
82
+
83
+ Your own components, such as `<Card>`, get no stamp: the elements inside them carry the stamp of the file that writes them. An import that Vite resolves into your source folders, such as `@/components/Card`, counts as your own. Onsite never stamps anything from `react` or `react-dom`.
84
+
74
85
  ### Ask again while the agent works
75
86
 
76
87
  Ask while a run works, and the new ask waits in a queue: the Send button turns into **Queue** (a list with a plus). When three asks wait, Queue is off, and its tooltip says why. Up to three asks wait, first in, first out. Each one starts when the run before it ends. It builds on the take of the elements you selected (see [Compare, keep or skip](#compare-keep-or-skip)), if that take's run had finished; else it starts from the files on disk. That take stays open until the ask starts: keep or skip it after.
@@ -131,6 +142,7 @@ Turn each skill on or off in the Onsite menu. The agent sees the name and descri
131
142
  |---|---|
132
143
  | <kbd>⌥</kbd> <kbd>⌥</kbd> (tap twice) | Open or close Onsite |
133
144
  | <kbd>⌥</kbd> + click | Select an element and open the quick chat beside it |
145
+ | <kbd>↑</kbd> / <kbd>↓</kbd> while picking (hold <kbd>⌥</kbd>, or Select mode) | Move the highlight to the element around it, or back down. The next click selects the highlighted element. In a text field, hold <kbd>⌥</kbd> |
134
146
  | <kbd>⌘</kbd>/<kbd>Ctrl</kbd> + <kbd>⇧</kbd> + <kbd>Enter</kbd> | Open the quick chat in the full chat |
135
147
  | Hold <kbd>⌥</kbd> | Show what the takes changed |
136
148
  | <kbd>⇧</kbd> + arrow key | Move Onsite to the next corner or middle (Onsite button or panel header focused) |
@@ -154,6 +166,7 @@ Returns the Vite plugin. Every option is optional.
154
166
  | `src` | `string[]` | `["src"]` | The source folders, relative to the Vite root. The agent reads and writes only here. |
155
167
  | `model` | `string` | Claude's default | The Claude model the agent uses, such as `"claude-sonnet-5-5"`. |
156
168
  | `enabled` | `boolean` | `true` | Set to `false` to turn Onsite off without removing the plugin. |
169
+ | `stampPackages` | `boolean` | `true` | Stamp components from packages, so a click selects them on their own (see [What you can select](#what-you-can-select)). Set to `false` when a library rejects unknown props. |
157
170
 
158
171
  ```ts
159
172
  onsite({
package/dist/index.mjs CHANGED
@@ -3169,29 +3169,77 @@ function parseJsx(code) {
3169
3169
  }
3170
3170
  }
3171
3171
  /**
3172
- * Adds the two attributes to each host JSX element of code. `file` is the
3173
- * path that the stamp names, relative to the Vite root, such as
3174
- * "src/pages/Invoices.tsx". It returns null when code has no host element to
3172
+ * True for a bare import specifier, such as "@mui/material" or "@/ui": not
3173
+ * relative, not absolute, and with no scheme such as "virtual:" or "node:".
3174
+ */
3175
+ function isBare(spec) {
3176
+ return spec !== "" && !/^(\.|\/|\\|\0|[a-zA-Z][\w+.-]*:)/.test(spec);
3177
+ }
3178
+ /** React itself: Fragment, StrictMode, Suspense and Profiler reject or ignore unknown props. */
3179
+ const fromReact = (spec) => /^react(-dom)?(\/|$)/.test(spec);
3180
+ /** The import specifier of each value binding that code imports from a bare specifier, by local name. */
3181
+ function bareImports(ast) {
3182
+ const out = /* @__PURE__ */ new Map();
3183
+ const body = ast.program?.body ?? [];
3184
+ for (const d of body) {
3185
+ if (d.type !== "ImportDeclaration" || d.importKind === "type") continue;
3186
+ const spec = d.source.value;
3187
+ if (typeof spec !== "string" || !isBare(spec) || fromReact(spec)) continue;
3188
+ for (const s of d.specifiers ?? []) {
3189
+ if (s.importKind === "type") continue;
3190
+ const local = s.local.name;
3191
+ if (typeof local === "string") out.set(local, spec);
3192
+ }
3193
+ }
3194
+ return out;
3195
+ }
3196
+ /**
3197
+ * The name at the root of a component's JSX name: "WaButton" for
3198
+ * `<WaButton>`, "Dialog" for `<Dialog.Root>`. It returns "" for a host
3199
+ * element and for a namespaced name such as `<svg:rect>`.
3200
+ */
3201
+ function componentRoot(opening) {
3202
+ let name = opening.name;
3203
+ if (name.type === "JSXIdentifier") return typeof name.name === "string" && !/^[a-z]/.test(name.name) ? name.name : "";
3204
+ while (name.type === "JSXMemberExpression") name = name.object;
3205
+ return name.type === "JSXIdentifier" && typeof name.name === "string" ? name.name : "";
3206
+ }
3207
+ /**
3208
+ * Adds the two attributes to each host JSX element of code, and to each JSX
3209
+ * element whose component comes from a package (see StampOptions). `file`
3210
+ * is the path that the stamp names, relative to the Vite root, such as
3211
+ * "src/pages/Invoices.tsx". It returns null when code has no element to
3175
3212
  * stamp. A file that does not parse is left alone: Vite reports the syntax
3176
3213
  * error itself.
3214
+ *
3215
+ * A component from a package gets the stamp of its usage site, which is the
3216
+ * line that the person can change. When the component passes `data-*`
3217
+ * props on to its root element, the stamp reaches the page.
3177
3218
  */
3178
- function stamp(code, file) {
3219
+ async function stamp(code, file, opts = {}) {
3179
3220
  if (!code.includes("<")) return null;
3180
3221
  const ast = parseJsx(code);
3181
3222
  if (!ast) return null;
3182
3223
  const base = (file.split("/").pop() ?? file).replace(/\.[^.]+$/, "");
3183
- const s = new MagicString(code);
3184
- let count = 0;
3224
+ const imports = opts.isPackage === null ? /* @__PURE__ */ new Map() : bareImports(ast);
3225
+ const found = [];
3185
3226
  const visit = (n, comp) => {
3186
3227
  const named = componentName(n, base);
3187
3228
  if (named) comp = named;
3188
- if (n.type === "JSXOpeningElement" && hostTag(n) && !hasAttr(n, "data-onsite-src")) {
3189
- const name = n.name;
3190
- const loc = n.loc.start;
3191
- let attrs = ` ${SRC_ATTR}="${attrText(`${file}:${loc.line}:${loc.column + 1}`)}"`;
3192
- if (comp) attrs += ` ${COMP_ATTR}="${attrText(comp)}"`;
3193
- s.appendLeft(name.end, attrs);
3194
- count++;
3229
+ if (n.type === "JSXOpeningElement" && !hasAttr(n, "data-onsite-src")) {
3230
+ if (hostTag(n)) found.push({
3231
+ opening: n,
3232
+ comp,
3233
+ spec: ""
3234
+ });
3235
+ else {
3236
+ const spec = imports.get(componentRoot(n));
3237
+ if (spec) found.push({
3238
+ opening: n,
3239
+ comp,
3240
+ spec
3241
+ });
3242
+ }
3195
3243
  }
3196
3244
  for (const key of Object.keys(n)) {
3197
3245
  if (skipKeys$1.has(key)) continue;
@@ -3202,6 +3250,21 @@ function stamp(code, file) {
3202
3250
  }
3203
3251
  };
3204
3252
  visit(ast, "");
3253
+ const specs = [...new Set(found.map((f) => f.spec).filter(Boolean))];
3254
+ const ask = opts.isPackage ?? (() => true);
3255
+ const answers = await Promise.all(specs.map(async (spec) => [spec, await ask(spec)]));
3256
+ const packages = new Set(answers.filter(([, yes]) => yes).map(([spec]) => spec));
3257
+ const s = new MagicString(code);
3258
+ let count = 0;
3259
+ for (const { opening, comp, spec } of found) {
3260
+ if (spec && !packages.has(spec)) continue;
3261
+ const loc = opening.loc.start;
3262
+ let attrs = ` ${SRC_ATTR}="${attrText(`${file}:${loc.line}:${loc.column + 1}`)}"`;
3263
+ if (comp) attrs += ` ${COMP_ATTR}="${attrText(comp)}"`;
3264
+ const after = opening.typeArguments ?? opening.typeParameters ?? opening.name;
3265
+ s.appendLeft(after.end, attrs);
3266
+ count++;
3267
+ }
3205
3268
  if (count === 0) return null;
3206
3269
  return {
3207
3270
  code: s.toString(),
@@ -4572,6 +4635,17 @@ function createPlugin(opts = {}, internal = {}) {
4572
4635
  const coreEntry = entry ? entryOf("@onsite/core") : "";
4573
4636
  const contracts = coreEntry ? join(dirname(coreEntry), "contracts.ts") : "";
4574
4637
  const rel = (abs) => relative(root, abs).split(sep).join("/");
4638
+ /** Whether a resolved module ID is the app's own code: a file in a source folder, outside node_modules. */
4639
+ const ownCode = (id) => {
4640
+ const file = cleanId(id);
4641
+ return inFolders(file, folders) && !file.includes(`${sep}node_modules${sep}`);
4642
+ };
4643
+ /**
4644
+ * Whether an import specifier names a package, by the importer's folder
4645
+ * and the specifier. An import that does not resolve is not kept, so it
4646
+ * is asked again after its file appears.
4647
+ */
4648
+ const packages = /* @__PURE__ */ new Map();
4575
4649
  const inScope = (path) => inFolders(resolve(root, path), folders);
4576
4650
  const log = (msg, e) => server?.config.logger.error(`[onsite] ${msg}${e ? `: ${e?.stack ?? e}` : ""}`, { timestamp: true });
4577
4651
  /** A switch's update that waits for Vite's hotUpdate hook. */
@@ -5011,10 +5085,25 @@ function createPlugin(opts = {}, internal = {}) {
5011
5085
  },
5012
5086
  transform: {
5013
5087
  order: "pre",
5014
- handler(code, id) {
5088
+ async handler(code, id) {
5015
5089
  const file = cleanId(id);
5016
5090
  if (!inFolders(file, folders) || !/\.[jt]sx$/.test(file) || file.includes(`${sep}node_modules${sep}`)) return null;
5017
- const out = stamp(code, rel(file));
5091
+ const isPackage = opts.stampPackages === false ? null : (spec) => {
5092
+ const key = `${dirname(file)}\0${spec}`;
5093
+ let answer = packages.get(key);
5094
+ if (!answer) {
5095
+ answer = this.resolve(spec, file, { skipSelf: true }).then((r) => {
5096
+ if (!r) packages.delete(key);
5097
+ return Boolean(r) && !ownCode(r.id);
5098
+ }, () => {
5099
+ packages.delete(key);
5100
+ return false;
5101
+ });
5102
+ packages.set(key, answer);
5103
+ }
5104
+ return answer;
5105
+ };
5106
+ const out = await stamp(code, rel(file), { isPackage });
5018
5107
  return out ? {
5019
5108
  code: out.code,
5020
5109
  map: out.map