effect-atom-svelte-devtools 0.2.0 → 0.2.2

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.
@@ -11,6 +11,7 @@ import path from "node:path";
11
11
  import { tsPlugin } from "@sveltejs/acorn-typescript";
12
12
  import { Parser } from "acorn";
13
13
  import MagicString from "magic-string";
14
+ import { parse as parseComponent } from "svelte/compiler";
14
15
  /** The id the transformed code imports `label` from; the plugin resolves it to ./label.ts. */
15
16
  export const labelModule = "virtual:effect-atom-svelte-devtools/label";
16
17
  const labelBinding = "__effectAtomSvelteLabel";
@@ -77,6 +78,30 @@ const plainValues = new Set([
77
78
  "ArrayExpression",
78
79
  "UnaryExpression",
79
80
  ]);
81
+ // What a state atom can be piped through and stay one: what is written to it is still what it
82
+ // reads. `Atom.map` and the like write through to their source, so their value can't be written
83
+ // back.
84
+ const stateKeeping = new Set([
85
+ "keepAlive",
86
+ "autoDispose",
87
+ "setIdleTTL",
88
+ "setLazy",
89
+ "withEquality",
90
+ "withLabel",
91
+ "serializable",
92
+ "withServerValue",
93
+ "withServerValueInitial",
94
+ ]);
95
+ /** Whether `Atom.keepAlive`, `Atom.setIdleTTL(...)` or the like: one of `stateKeeping`. */
96
+ const keepsState = (argument) => {
97
+ const node = unwrap(argument);
98
+ const callee = node.type === "CallExpression" ? unwrap(node.callee) : node;
99
+ return (callee.type === "MemberExpression" &&
100
+ !callee.computed &&
101
+ callee.object.type === "Identifier" &&
102
+ callee.object.name === "Atom" &&
103
+ stateKeeping.has(String(callee.property.name)));
104
+ };
80
105
  /**
81
106
  * Whether the call makes a state atom, such as `Atom.make(0)` or `Atom.make([]).pipe(...)`: one
82
107
  * that holds what is written to it, which a hot reload can carry over.
@@ -89,7 +114,9 @@ const isState = (node) => {
89
114
  const object = callee.object;
90
115
  const property = callee.property;
91
116
  if (property.type === "Identifier" && property.name === "pipe") {
92
- return object.type === "CallExpression" && isState(object);
117
+ return (object.type === "CallExpression" &&
118
+ isState(object) &&
119
+ node.arguments.every(keepsState));
93
120
  }
94
121
  const [argument] = node.arguments;
95
122
  return (object.type === "Identifier" &&
@@ -243,19 +270,67 @@ const parse = (code) => parser.parse(code, {
243
270
  });
244
271
  // Svelte's own pattern for finding scripts to preprocess (svelte/src/compiler/preprocess).
245
272
  const scriptPattern = /<!--[^]*?-->|<script(?<attributes>(?:\s+[^=>'"/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"/\s]+)*\s*)(?:\/>|>(?<code>[\S\s]*?)<\/script>)/gu;
273
+ /** Text blanked out, keeping its offsets (an astral character is two UTF-16 units) and lines. */
274
+ const blank = (text) => text.replaceAll(/[^\n]/gu, (character) => " ".repeat(character.length));
275
+ /** The component with each script and style's content blanked out, so only its markup is left. */
276
+ const markupOf = (source, matches) => {
277
+ let markup = source;
278
+ for (const match of matches) {
279
+ const { code } = match.groups ?? {};
280
+ if (code !== undefined) {
281
+ const from = match.index + match[0].length - "</script>".length - code.length;
282
+ markup =
283
+ markup.slice(0, from) + blank(code) + markup.slice(from + code.length);
284
+ }
285
+ }
286
+ return markup.replaceAll(/(?<open><style(?:\s[^>]*)?>)(?<css>[^]*?)<\/style>/gu, (_, open, css) => `${open}${blank(css)}</style>`);
287
+ };
288
+ /**
289
+ * Where the component's own scripts (its instance and module scripts) start, as Svelte's parser
290
+ * sees them: a script nested in the markup (in an element, a block or <svelte:head>) is page HTML.
291
+ * Takes the blanked markup, so a script or style that needs a preprocessor doesn't matter.
292
+ * `undefined` if the markup doesn't parse either.
293
+ */
294
+ const topLevel = (markup) => {
295
+ try {
296
+ const ast = parseComponent(markup, { modern: true });
297
+ // Svelte drops a byte order mark before parsing, so its offsets are one short of the source's.
298
+ const shift = markup.startsWith("\uFEFF") ? 1 : 0;
299
+ return new Set([ast.instance?.start, ast.module?.start]
300
+ .filter((start) => start !== undefined)
301
+ .map((start) => start + shift));
302
+ }
303
+ catch {
304
+ return undefined;
305
+ }
306
+ };
246
307
  /** The scripts of a component: where each one's code starts, the code, and if it is the module script. */
247
308
  const scripts = (source) => {
248
309
  const found = [];
249
- for (const match of source.matchAll(scriptPattern)) {
250
- const [whole] = match;
310
+ const matches = [...source.matchAll(scriptPattern)];
311
+ const markup = markupOf(source, matches);
312
+ const top = topLevel(markup);
313
+ // The fallback when the markup doesn't parse. HTML comments are matched too, and scripts and
314
+ // styles are blanked, so neither kind of comment that mentions <svelte:head> starts a head.
315
+ const heads = [
316
+ ...markup.matchAll(/<!--[^]*?-->|<svelte:head\b[^]*?<\/svelte:head>/gu),
317
+ ]
318
+ .filter((m) => m[0].startsWith("<svelte:head"))
319
+ .map((m) => [m.index, m.index + m[0].length]);
320
+ for (const match of matches) {
251
321
  const { attributes, code } = match.groups ?? {};
252
322
  if (attributes === undefined || code === undefined) {
253
323
  continue;
254
324
  }
325
+ if (top
326
+ ? !top.has(match.index)
327
+ : heads.some(([from, to]) => match.index > from && match.index < to)) {
328
+ continue;
329
+ }
255
330
  found.push({
256
331
  code,
257
332
  module: /\bmodule\b/u.test(attributes),
258
- start: match.index + whole.indexOf(">") + 1,
333
+ start: match.index + "<script".length + attributes.length + 1,
259
334
  });
260
335
  }
261
336
  return found;
package/dist/vite.js CHANGED
@@ -72,8 +72,10 @@ export const atomLabels = (options = {}) => {
72
72
  catch {
73
73
  // The app can't resolve the library, so there is none of its code to skip.
74
74
  }
75
+ // The plugin that compiles components, among the plugins as Vite runs them: "vite-plugin-svelte"
76
+ // up to version 6, "vite-plugin-svelte:compile" from 7, which runs after every "pre" plugin.
75
77
  const names = config.plugins.map((plugin) => plugin.name);
76
- const svelte = names.findIndex((name) => name.startsWith("vite-plugin-svelte"));
78
+ const svelte = names.findIndex((name) => name === "vite-plugin-svelte" || name === "vite-plugin-svelte:compile");
77
79
  if (svelte !== -1 &&
78
80
  svelte < names.indexOf("effect-atom-svelte-devtools:labels")) {
79
81
  config.logger.warn("effect-atom-svelte-devtools: atomLabels() comes after the Svelte plugin, so atoms declared in components aren't labelled. Put it before sveltekit() or svelte().");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "effect-atom-svelte-devtools",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Developer tools for effect-atom-svelte: atom names from your source, and a live view of the registry",
5
5
  "keywords": [
6
6
  "devtools",
@@ -60,7 +60,7 @@
60
60
  "@sveltejs/vite-plugin-svelte": "^7.2.0",
61
61
  "@types/node": "^24",
62
62
  "effect": "4.0.1",
63
- "effect-atom-svelte": "0.1.2",
63
+ "effect-atom-svelte": "workspace:*",
64
64
  "svelte": "^5.57.2",
65
65
  "svelte-check": "^4.6.0",
66
66
  "vite": "^8.3.0",
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "peerDependencies": {
70
70
  "effect": "~4.0.0",
71
- "effect-atom-svelte": "^0.2.0",
71
+ "effect-atom-svelte": "^0.3.0",
72
72
  "svelte": "^5.57.2",
73
73
  "vite": ">=5.0.0"
74
74
  },