deictic 0.0.1 → 0.2.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.
Files changed (176) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +167 -0
  3. package/dist/annotations/client.d.ts +4 -0
  4. package/dist/annotations/client.d.ts.map +1 -0
  5. package/dist/annotations/client.js +497 -0
  6. package/dist/annotations/client.js.map +1 -0
  7. package/dist/annotations/index.d.ts +110 -0
  8. package/dist/annotations/index.d.ts.map +1 -0
  9. package/dist/annotations/index.js +22 -0
  10. package/dist/annotations/index.js.map +1 -0
  11. package/dist/astro.d.ts +29 -0
  12. package/dist/astro.d.ts.map +1 -0
  13. package/dist/astro.js +24 -0
  14. package/dist/astro.js.map +1 -0
  15. package/dist/bun.d.ts +12 -0
  16. package/dist/bun.d.ts.map +1 -0
  17. package/dist/bun.js +67 -0
  18. package/dist/bun.js.map +1 -0
  19. package/dist/client/index.d.ts +95 -0
  20. package/dist/client/index.d.ts.map +1 -0
  21. package/dist/client/index.js +1333 -0
  22. package/dist/client/index.js.map +1 -0
  23. package/dist/index.d.ts +23 -0
  24. package/dist/index.js +10 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/loader.d.ts +15 -0
  27. package/dist/loader.d.ts.map +1 -0
  28. package/dist/loader.js +17 -0
  29. package/dist/loader.js.map +1 -0
  30. package/dist/next.d.ts +17 -0
  31. package/dist/next.d.ts.map +1 -0
  32. package/dist/next.js +150 -0
  33. package/dist/next.js.map +1 -0
  34. package/dist/recommended/index.d.ts +25 -0
  35. package/dist/recommended/index.d.ts.map +1 -0
  36. package/dist/recommended/index.js +19 -0
  37. package/dist/recommended/index.js.map +1 -0
  38. package/dist/recordings/client.d.ts +4 -0
  39. package/dist/recordings/client.d.ts.map +1 -0
  40. package/dist/recordings/client.js +376 -0
  41. package/dist/recordings/client.js.map +1 -0
  42. package/dist/recordings/index.d.ts +2 -0
  43. package/dist/recordings/index.js +3 -0
  44. package/dist/screenshots/client.d.ts +23 -0
  45. package/dist/screenshots/client.d.ts.map +1 -0
  46. package/dist/screenshots/client.js +76 -0
  47. package/dist/screenshots/client.js.map +1 -0
  48. package/dist/screenshots/dom.d.ts +4 -0
  49. package/dist/screenshots/dom.d.ts.map +1 -0
  50. package/dist/screenshots/dom.js +92 -0
  51. package/dist/screenshots/dom.js.map +1 -0
  52. package/dist/screenshots/index.d.ts +2 -0
  53. package/dist/screenshots/index.js +98 -0
  54. package/dist/screenshots/index.js.map +1 -0
  55. package/dist/screenshots/rrweb.d.ts +9 -0
  56. package/dist/screenshots/rrweb.d.ts.map +1 -0
  57. package/dist/screenshots/rrweb.js +60 -0
  58. package/dist/screenshots/rrweb.js.map +1 -0
  59. package/dist/shared/endpoints.js +354 -0
  60. package/dist/shared/endpoints.js.map +1 -0
  61. package/dist/shared/index.d.ts +226 -0
  62. package/dist/shared/index.d.ts.map +1 -0
  63. package/dist/shared/index2.d.ts +98 -0
  64. package/dist/shared/index2.d.ts.map +1 -0
  65. package/dist/shared/index3.d.ts +86 -0
  66. package/dist/shared/index3.d.ts.map +1 -0
  67. package/dist/shared/index4.d.ts +184 -0
  68. package/dist/shared/index4.d.ts.map +1 -0
  69. package/dist/shared/index5.d.ts +70 -0
  70. package/dist/shared/index5.d.ts.map +1 -0
  71. package/dist/shared/integration.d.ts +20 -0
  72. package/dist/shared/integration.d.ts.map +1 -0
  73. package/dist/shared/integration.js +279 -0
  74. package/dist/shared/integration.js.map +1 -0
  75. package/dist/shared/kept.js +23 -0
  76. package/dist/shared/kept.js.map +1 -0
  77. package/dist/shared/marks.js +14 -0
  78. package/dist/shared/marks.js.map +1 -0
  79. package/dist/shared/playwright.js +105 -0
  80. package/dist/shared/playwright.js.map +1 -0
  81. package/dist/shared/recordings.js +246 -0
  82. package/dist/shared/recordings.js.map +1 -0
  83. package/dist/shared/shortcut.js +63 -0
  84. package/dist/shared/shortcut.js.map +1 -0
  85. package/dist/shared/tasks.js +521 -0
  86. package/dist/shared/tasks.js.map +1 -0
  87. package/dist/shared/waveform.js +93 -0
  88. package/dist/shared/waveform.js.map +1 -0
  89. package/dist/tailwind/client.d.ts +4 -0
  90. package/dist/tailwind/client.d.ts.map +1 -0
  91. package/dist/tailwind/client.js +613 -0
  92. package/dist/tailwind/client.js.map +1 -0
  93. package/dist/tailwind/index.d.ts +2 -0
  94. package/dist/tailwind/index.js +557 -0
  95. package/dist/tailwind/index.js.map +1 -0
  96. package/dist/tasks/client.d.ts +6 -0
  97. package/dist/tasks/client.d.ts.map +1 -0
  98. package/dist/tasks/client.js +642 -0
  99. package/dist/tasks/client.js.map +1 -0
  100. package/dist/tasks/index.d.ts +2 -0
  101. package/dist/tasks/index.js +1 -0
  102. package/dist/vite.d.ts +16 -0
  103. package/dist/vite.d.ts.map +1 -0
  104. package/dist/vite.js +61 -0
  105. package/dist/vite.js.map +1 -0
  106. package/dist/voice/client.d.ts +4 -0
  107. package/dist/voice/client.d.ts.map +1 -0
  108. package/dist/voice/client.js +66 -0
  109. package/dist/voice/client.js.map +1 -0
  110. package/dist/voice/index.d.ts +47 -0
  111. package/dist/voice/index.d.ts.map +1 -0
  112. package/dist/voice/index.js +74 -0
  113. package/dist/voice/index.js.map +1 -0
  114. package/dist/webpack.d.ts +50 -0
  115. package/dist/webpack.d.ts.map +1 -0
  116. package/dist/webpack.js +78 -0
  117. package/dist/webpack.js.map +1 -0
  118. package/package.json +307 -7
  119. package/skills/deictic/SKILL.md +47 -0
  120. package/skills/deictic/references/install.md +96 -0
  121. package/skills/deictic/references/recordings.md +83 -0
  122. package/skills/deictic/references/tasks.md +79 -0
  123. package/skills/deictic/references/watch.md +43 -0
  124. package/skills/deictic/scripts/frame.mjs +62 -0
  125. package/skills/deictic/scripts/lib.mjs +211 -0
  126. package/skills/deictic/scripts/recording.mjs +186 -0
  127. package/skills/deictic/scripts/task.mjs +84 -0
  128. package/skills/deictic/scripts/transcribe.mjs +93 -0
  129. package/skills/deictic/scripts/voice.mjs +50 -0
  130. package/skills/deictic/scripts/watch.mjs +51 -0
  131. package/src/astro.ts +34 -0
  132. package/src/bun.ts +79 -0
  133. package/src/client/index.ts +846 -0
  134. package/src/client/lightbox.ts +256 -0
  135. package/src/client/style.ts +243 -0
  136. package/src/client/tooltip.ts +78 -0
  137. package/src/client/waveform.ts +98 -0
  138. package/src/index.ts +247 -0
  139. package/src/loader.ts +28 -0
  140. package/src/next.ts +133 -0
  141. package/src/plugins/annotations/client.ts +301 -0
  142. package/src/plugins/annotations/draw.ts +169 -0
  143. package/src/plugins/annotations/index.ts +105 -0
  144. package/src/plugins/annotations/svg.ts +61 -0
  145. package/src/plugins/recommended/index.ts +35 -0
  146. package/src/plugins/recordings/client.ts +316 -0
  147. package/src/plugins/recordings/index.ts +175 -0
  148. package/src/plugins/recordings/marks.ts +11 -0
  149. package/src/plugins/recordings/ours.ts +70 -0
  150. package/src/plugins/recordings/replay.ts +165 -0
  151. package/src/plugins/screenshots/client.ts +64 -0
  152. package/src/plugins/screenshots/dom.ts +79 -0
  153. package/src/plugins/screenshots/index.ts +194 -0
  154. package/src/plugins/screenshots/playwright.ts +119 -0
  155. package/src/plugins/screenshots/rrweb.ts +45 -0
  156. package/src/plugins/tailwind/catalog.ts +396 -0
  157. package/src/plugins/tailwind/client.ts +669 -0
  158. package/src/plugins/tailwind/index.ts +267 -0
  159. package/src/plugins/tasks/client.ts +622 -0
  160. package/src/plugins/tasks/index.ts +557 -0
  161. package/src/plugins/tasks/local.ts +170 -0
  162. package/src/plugins/voice/client.ts +58 -0
  163. package/src/plugins/voice/index.ts +114 -0
  164. package/src/shortcut.ts +83 -0
  165. package/src/tools/astro.ts +81 -0
  166. package/src/tools/editor.ts +22 -0
  167. package/src/tools/endpoints.ts +163 -0
  168. package/src/tools/history.ts +203 -0
  169. package/src/tools/integration.ts +108 -0
  170. package/src/tools/kept.ts +21 -0
  171. package/src/tools/locate.ts +94 -0
  172. package/src/tools/plugins.ts +41 -0
  173. package/src/tools/source.ts +72 -0
  174. package/src/vite.ts +81 -0
  175. package/src/webpack.ts +107 -0
  176. package/index.js +0 -1
@@ -0,0 +1,396 @@
1
+ /**
2
+ * What the project's Tailwind knows, for the panel: every class it can make (to complete what's typed), what
3
+ * each one sets (a colour to show, a value to hint), the classes of the same kind (to switch between), and a
4
+ * class's CSS (to preview one no file uses yet).
5
+ *
6
+ * All of it comes from Tailwind's own design system, loaded from the project (v4, through `@tailwindcss/node`,
7
+ * which every v4 integration brings), so the theme is the project's. Two classes are of a kind when they set the
8
+ * same properties: `text-sm` and `text-lg`, `flex` and `grid`, but not `text-sm` and `text-red-500`.
9
+ */
10
+ import { readFileSync, statSync } from 'node:fs';
11
+ import { createRequire } from 'node:module';
12
+ import { dirname, join, resolve } from 'node:path';
13
+ import { pathToFileURL } from 'node:url';
14
+
15
+ /** A class as the panel shows it. */
16
+ export interface ClassInfo {
17
+ name: string;
18
+ /** Whether Tailwind makes anything of it: a class of the project's own isn't known. */
19
+ known: boolean;
20
+ /** The colour it sets, as CSS. */
21
+ color?: string;
22
+ /** What it sets its main property to, theme values resolved: `0.875rem`. */
23
+ value?: string;
24
+ /** A variant to go on typing after (`hover:`), not a class. */
25
+ variant?: true;
26
+ }
27
+
28
+ /** The classes of one kind, and how to write one with a value of the user's own. */
29
+ export interface Alternatives {
30
+ /** The property the kind sets: `font-size`. */
31
+ label: string;
32
+ /** Whether the kind is a colour's, shown as swatches. */
33
+ colors: boolean;
34
+ items: ClassInfo[];
35
+ /** A custom value goes between these: `md:text-[` and `]`. None for a kind that takes no value (`flex`). */
36
+ custom?: { before: string; after: string; value?: string };
37
+ }
38
+
39
+ export interface Catalog {
40
+ /** Classes for what's typed, the best first: those starting with it, variants, then those holding it. */
41
+ complete(query: string, limit?: number): ClassInfo[];
42
+ info(classes: string[]): ClassInfo[];
43
+ alternatives(name: string): Alternatives | undefined;
44
+ /** CSS for the classes, in Tailwind's order, theme values written out: for a page whose build hasn't seen them. */
45
+ css(classes: string[]): string;
46
+ }
47
+
48
+ /** The part of Tailwind's design system used here. Unstable there, so everything read from it is checked. */
49
+ interface DesignSystem {
50
+ getClassList(): [string, { modifiers?: string[] }][];
51
+ getVariants(): { name: string; values: string[]; hasDash: boolean }[];
52
+ candidatesToCss(classes: string[]): (string | null)[];
53
+ parseCandidate(candidate: string): Iterable<{ kind: string; root?: string; value?: { kind: string; value: string } | null }>;
54
+ resolveThemeValue(path: string): string | undefined;
55
+ getClassOrder(classes: string[]): [string, bigint | null][];
56
+ }
57
+
58
+ interface Entry {
59
+ name: string;
60
+ /** The kind: the properties it sets, sorted. */
61
+ kind: string;
62
+ color?: string;
63
+ value?: string;
64
+ }
65
+
66
+ /** Packages that depend on `@tailwindcss/node`, where the project doesn't itself. */
67
+ const VIA = ['@tailwindcss/vite', '@tailwindcss/postcss', '@tailwindcss/cli', '@tailwindcss/webpack'];
68
+ const MOST = 400;
69
+ const natural = new Intl.Collator('en', { numeric: true }).compare;
70
+
71
+ /**
72
+ * The catalog of the project at `root`, from its stylesheet (`css`, from the root: the one that imports
73
+ * Tailwind and holds the theme), or from Tailwind's defaults without one. Read again when that file changes.
74
+ * Rejects where the project has no Tailwind v4.
75
+ */
76
+ export async function loadCatalog(root: string, css?: string): Promise<Catalog> {
77
+ const { __unstable__loadDesignSystem: load } = await tailwindOf(root);
78
+ const file = css === undefined ? undefined : resolve(root, css);
79
+ const build = async () =>
80
+ catalogOf(
81
+ file === undefined
82
+ ? await load('@import "tailwindcss";', { base: root })
83
+ : await load(readFileSync(file, 'utf8'), { base: dirname(file) }),
84
+ );
85
+ const stamp = () => (file === undefined ? 0 : statSync(file).mtimeMs);
86
+ let built = stamp();
87
+ let current = await build();
88
+ // The latest build, rebuilt when the stylesheet changed since. One that fails (a file half-saved) keeps the last.
89
+ const latest = (): Catalog => {
90
+ const now = stamp();
91
+ if (now !== built) {
92
+ built = now;
93
+ void build().then(
94
+ (next) => (current = next),
95
+ () => {},
96
+ );
97
+ }
98
+ return current;
99
+ };
100
+ return {
101
+ complete: (query, limit) => latest().complete(query, limit),
102
+ info: (classes) => latest().info(classes),
103
+ alternatives: (name) => latest().alternatives(name),
104
+ css: (classes) => latest().css(classes),
105
+ };
106
+ }
107
+
108
+ type Loader = (css: string, options: { base: string }) => Promise<DesignSystem>;
109
+
110
+ async function tailwindOf(root: string): Promise<{ __unstable__loadDesignSystem: Loader }> {
111
+ const from = createRequire(join(root, 'package.json'));
112
+ const found = [
113
+ () => from.resolve('@tailwindcss/node'),
114
+ ...VIA.map((via) => () => createRequire(from.resolve(via)).resolve('@tailwindcss/node')),
115
+ ];
116
+ for (const find of found) {
117
+ let path: string;
118
+ try {
119
+ path = find();
120
+ } catch {
121
+ continue;
122
+ }
123
+ const module = (await import(pathToFileURL(path).href)) as { __unstable__loadDesignSystem?: Loader };
124
+ if (typeof module.__unstable__loadDesignSystem === 'function')
125
+ return { __unstable__loadDesignSystem: module.__unstable__loadDesignSystem };
126
+ }
127
+ throw new Error(`no Tailwind v4 in ${root}: install @tailwindcss/node there (or one of ${VIA.join(', ')})`);
128
+ }
129
+
130
+ export function catalogOf(design: DesignSystem): Catalog {
131
+ /** A value with the theme's variables written out, and what arithmetic is left done: `calc(var(--spacing) * 4)` is `1rem`. */
132
+ const resolved = (value: string): string => {
133
+ let now = value;
134
+ for (let i = 0; i < 4; i++) {
135
+ const next = now.replace(/var\((--[\w-]+)\)/g, (all, name: string) => themed(name) ?? all);
136
+ if (next === now) break;
137
+ now = next;
138
+ }
139
+ return now.replace(/calc\(([^()]+)\)/g, (all, sum: string) => computed(sum) ?? all);
140
+ };
141
+ const themed = (name: string): string | undefined => {
142
+ try {
143
+ return design.resolveThemeValue(name) ?? undefined;
144
+ } catch {
145
+ return undefined;
146
+ }
147
+ };
148
+
149
+ const entryOf = (name: string, css: string): Entry => {
150
+ const set = declarations(css);
151
+ const kind = [...new Set(set.map(([property]) => property))].toSorted().join(',');
152
+ const color = set.findLast(([property, value]) => COLOR.test(property) && !NO_COLOR.test(value))?.[1];
153
+ const main = (set.find(([property]) => !property.startsWith('--')) ?? set[0])?.[1];
154
+ return {
155
+ name,
156
+ kind,
157
+ ...(color === undefined ? {} : { color: resolved(color) }),
158
+ ...(main === undefined ? {} : { value: resolved(main) }),
159
+ };
160
+ };
161
+
162
+ const names = design
163
+ .getClassList()
164
+ .map(([name]) => name)
165
+ .toSorted(natural);
166
+ const modifiers = new Map(design.getClassList().map(([name, { modifiers = [] }]) => [name, modifiers]));
167
+ const sheets = design.candidatesToCss(names);
168
+ const byName = new Map<string, Entry>();
169
+ const kinds = new Map<string, Entry[]>();
170
+ names.forEach((name, i) => {
171
+ const css = sheets[i];
172
+ if (css === null || css === undefined) return;
173
+ const entry = entryOf(name, css);
174
+ if (entry.kind === '') return;
175
+ byName.set(name, entry);
176
+ const kind = kinds.get(entry.kind);
177
+ if (kind === undefined) kinds.set(entry.kind, [entry]);
178
+ else kind.push(entry);
179
+ });
180
+ for (const kind of kinds.values()) kind.sort(byValue(kind));
181
+
182
+ const variants = design
183
+ .getVariants()
184
+ .flatMap(({ name, values, hasDash }) => [name, ...values.map((value) => `${name}${hasDash ? '-' : ''}${value}`)])
185
+ // `*` and `**` are variants too, and so are those that only take a value in brackets: neither completes a word.
186
+ .filter((name) => /^[@\w]/.test(name))
187
+ .toSorted(natural);
188
+
189
+ /** A class Tailwind makes on asking, not in its list: one with a value in brackets, a modifier, a fraction. */
190
+ const made = new Map<string, Entry | undefined>();
191
+ const make = (body: string): Entry | undefined => {
192
+ if (made.has(body)) return made.get(body);
193
+ const css = design.candidatesToCss([body])[0];
194
+ const entry = css === null || css === undefined ? undefined : entryOf(body, css);
195
+ made.set(body, entry?.kind === '' ? undefined : entry);
196
+ return made.get(body);
197
+ };
198
+ const rootOf = (body: string): string | undefined => {
199
+ for (const candidate of design.parseCandidate(body))
200
+ if (candidate.kind === 'functional' && candidate.root !== undefined && candidate.value) return candidate.root;
201
+ return undefined;
202
+ };
203
+ /** The entry of a class's own part (no variants, no `!`), and the kind it's of among the listed ones. */
204
+ const find = (body: string): { entry: Entry; kind: string } | undefined => {
205
+ const listed = byName.get(body);
206
+ if (listed !== undefined) return { entry: listed, kind: listed.kind };
207
+ const entry = make(body);
208
+ if (entry === undefined) return undefined;
209
+ if (kinds.has(entry.kind)) return { entry, kind: entry.kind };
210
+ // `text-[13px]` sets the size alone, `text-sm` the line height too: the kind is the nearest one of its root's.
211
+ const root = rootOf(body);
212
+ if (root === undefined) return { entry, kind: entry.kind };
213
+ const own = new Set(entry.kind.split(','));
214
+ let best: { kind: string; shared: number } | undefined;
215
+ for (const name of names) {
216
+ if (!name.startsWith(`${root}-`)) continue;
217
+ const kind = byName.get(name)?.kind;
218
+ if (kind === undefined || kind === best?.kind) continue;
219
+ const shared = kind.split(',').filter((property) => own.has(property)).length;
220
+ if (shared > (best?.shared ?? 0)) best = { kind, shared };
221
+ }
222
+ return { entry, kind: best?.kind ?? entry.kind };
223
+ };
224
+ const shown = (name: string, entry: Entry): ClassInfo => ({
225
+ name,
226
+ known: true,
227
+ ...(entry.color === undefined ? {} : { color: entry.color }),
228
+ ...(entry.value === undefined ? {} : { value: entry.value }),
229
+ });
230
+ const info = (name: string): ClassInfo => {
231
+ const found = find(parts(name).body);
232
+ return found === undefined ? { name, known: false } : shown(name, found.entry);
233
+ };
234
+
235
+ return {
236
+ complete(query, limit = 40) {
237
+ const { before: variantsBefore, bang, body, after } = parts(query.trim());
238
+ if (body === '' && variantsBefore === '') return [];
239
+ const before = `${variantsBefore}${bang}`;
240
+ const starts: ClassInfo[] = [];
241
+ const holds: ClassInfo[] = [];
242
+ const add = (to: ClassInfo[], name: string, entry: Entry) => {
243
+ if (to.length < limit) to.push(shown(`${before}${name}${after}`, entry));
244
+ };
245
+ const exact = byName.get(body);
246
+ if (exact !== undefined) add(starts, body, exact);
247
+ for (const name of names) {
248
+ if (starts.length >= limit) break;
249
+ if (name === body) continue;
250
+ const entry = byName.get(name);
251
+ if (entry === undefined) continue;
252
+ if (name.startsWith(body)) add(starts, name, entry);
253
+ else if (body.length > 1 && name.includes(body)) add(holds, name, entry);
254
+ }
255
+ // `bg-red-500/`: the modifiers the class takes.
256
+ const slash = body.lastIndexOf('/');
257
+ const base = slash < 0 ? undefined : byName.get(body.slice(0, slash));
258
+ const modified =
259
+ base === undefined || starts.length > 0
260
+ ? []
261
+ : (modifiers.get(base.name) ?? [])
262
+ .filter((modifier) => modifier.startsWith(body.slice(slash + 1)))
263
+ .map((modifier) => shown(`${before}${base.name}/${modifier}${after}`, make(`${base.name}/${modifier}`) ?? base));
264
+ const ahead =
265
+ after !== '' || bang !== '' || body === ''
266
+ ? []
267
+ : variants
268
+ .filter((name) => name.startsWith(body))
269
+ .map((name): ClassInfo => ({ name: `${before}${name}:`, known: true, variant: true }));
270
+ return [...starts, ...modified, ...ahead, ...holds].slice(0, limit);
271
+ },
272
+ info: (classes) => classes.map(info),
273
+ alternatives(name) {
274
+ const { before, bang, body, after } = parts(name);
275
+ const found = find(body);
276
+ if (found === undefined) return undefined;
277
+ const { modifier, base } = modified(body, byName);
278
+ const kind = kinds.get(found.kind) ?? [];
279
+ const properties = found.kind.split(',');
280
+ const root = rootOf(body);
281
+ const value = [...design.parseCandidate(body)].find((candidate) => candidate.value?.kind === 'arbitrary')?.value?.value;
282
+ const items = kind.slice(0, MOST).map((entry) => shown(`${before}${bang}${entry.name}${modifier}${after}`, entry));
283
+ // The class itself where it isn't listed (its value is its own), so the panel has it to show as chosen.
284
+ if (!kind.some((entry) => entry.name === base)) items.unshift(shown(name, found.entry));
285
+ return {
286
+ label: properties.find((property) => !property.startsWith('--')) ?? properties[0]!,
287
+ colors: kind.filter((entry) => entry.color !== undefined).length * 2 > kind.length,
288
+ items,
289
+ ...(root === undefined
290
+ ? {}
291
+ : { custom: { before: `${before}${bang}${root}-[`, after: `]${modifier}${after}`, ...(value === undefined ? {} : { value }) } }),
292
+ };
293
+ },
294
+ css(classes) {
295
+ const bodies = [...new Set(classes)];
296
+ const order = new Map(design.getClassOrder(bodies));
297
+ bodies.sort((a, b) => {
298
+ const [x, y] = [order.get(a) ?? null, order.get(b) ?? null];
299
+ if (x === null || y === null) return x === y ? 0 : x === null ? 1 : -1;
300
+ return x < y ? -1 : x > y ? 1 : 0;
301
+ });
302
+ const properties = new Set<string>();
303
+ const rules = design
304
+ .candidatesToCss(bodies)
305
+ .filter((css) => css !== null && css !== undefined)
306
+ // `@property` can't sit in a layer, so each is lifted out, once.
307
+ .map((css) =>
308
+ css.replace(PROPERTY, (rule) => {
309
+ properties.add(rule.trim());
310
+ return '';
311
+ }),
312
+ )
313
+ .join('');
314
+ if (rules.trim() === '') return '';
315
+ // In the layer the page's own utilities are in, so what beats them there (the page's unlayered CSS) beats these.
316
+ return `${[...properties].join('\n')}\n@layer utilities {\n${rules.replace(/var\((--[\w-]+)\)/g, (all, name: string) => themed(name) ?? all)}}\n`;
317
+ },
318
+ };
319
+ }
320
+
321
+ const COLOR = /(^|-)color$|^fill$|^stroke$|^--tw-gradient-(from|via|to)$/;
322
+ const NO_COLOR = /^(inherit|none|initial|unset)$/;
323
+ const PROPERTY = /@property [^{]+\{[^}]*\}\n?/g;
324
+
325
+ /** A rule's declarations, in order, from Tailwind's CSS for one class: one a line, `@property` rules after. */
326
+ function declarations(css: string): [property: string, value: string][] {
327
+ const rules = css.replace(PROPERTY, '');
328
+ return [...rules.matchAll(/^\s+(-{0,2}[\w-]+):\s*(.+?)(?:\s*!important)?;$/gm)].map((match) => [match[1]!, match[2]!]);
329
+ }
330
+
331
+ /** `0.25rem * 4` as `1rem`: products and quotients of numbers with one unit between them. Anything else is left. */
332
+ function computed(sum: string): string | undefined {
333
+ const tokens = sum.trim().split(/\s+/);
334
+ let total = 1;
335
+ let unit = '';
336
+ for (let i = 0; i < tokens.length; i++) {
337
+ const token = tokens[i]!;
338
+ if (i % 2 === 1) {
339
+ if (token !== '*' && token !== '/') return undefined;
340
+ continue;
341
+ }
342
+ const match = /^(-?(?:\d+\.?\d*|\.\d+))(%|[a-z]+)?$/.exec(token);
343
+ if (match === null) return undefined;
344
+ const divides = tokens[i - 1] === '/';
345
+ if (match[2] !== undefined && (unit !== '' || divides)) return undefined;
346
+ unit = match[2] ?? unit;
347
+ total = divides ? total / Number(match[1]) : total * Number(match[1]);
348
+ }
349
+ if (tokens.length % 2 === 0 || !Number.isFinite(total)) return undefined;
350
+ return `${Number(total.toFixed(4))}${unit}`;
351
+ }
352
+
353
+ /** A length as a number to order by, where the value is one: `1rem` is 16. */
354
+ function measure(value: string | undefined): number | undefined {
355
+ const match = value === undefined ? null : /^(-?(?:\d+\.?\d*|\.\d+))(px|rem|em|%)?$/.exec(value);
356
+ if (match === null) return undefined;
357
+ return Number(match[1]) * (match[2] === 'rem' || match[2] === 'em' ? 16 : 1);
358
+ }
359
+
360
+ /** A kind's order: by size where most of it has one (`text-xs` before `text-sm`), by name else, as it already is. */
361
+ function byValue(kind: Entry[]): (a: Entry, b: Entry) => number {
362
+ const sizes = new Map(kind.map((entry) => [entry, measure(entry.value)]));
363
+ const sized = [...sizes.values()].filter((size) => size !== undefined).length;
364
+ if (sized * 10 < kind.length * 7) return () => 0;
365
+ return (a, b) => {
366
+ const [x, y] = [sizes.get(a), sizes.get(b)];
367
+ if (x === undefined || y === undefined) return x === y ? 0 : x === undefined ? 1 : -1;
368
+ return x - y;
369
+ };
370
+ }
371
+
372
+ /**
373
+ * A class as written, apart: its variants (`md:hover:`), `!` where it leads, its own part, and `!` where it
374
+ * trails. A `:` inside brackets or parentheses is the value's.
375
+ */
376
+ function parts(name: string): { before: string; bang: string; body: string; after: string } {
377
+ let depth = 0;
378
+ let cut = 0;
379
+ for (let i = 0; i < name.length; i++) {
380
+ const char = name[i]!;
381
+ if (char === '[' || char === '(') depth++;
382
+ else if (char === ']' || char === ')') depth--;
383
+ else if (char === ':' && depth === 0) cut = i + 1;
384
+ }
385
+ const own = name.slice(cut);
386
+ const bang = own.startsWith('!') ? '!' : '';
387
+ const after = !bang && own.endsWith('!') ? '!' : '';
388
+ return { before: name.slice(0, cut), bang, body: own.slice(bang.length, own.length - after.length), after };
389
+ }
390
+
391
+ /** A class's modifier (`/50`) and what it's on, where the whole isn't a class itself, as `w-1/2` is. */
392
+ function modified(body: string, listed: Map<string, unknown>): { base: string; modifier: string } {
393
+ const slash = body.lastIndexOf('/');
394
+ if (listed.has(body) || slash < 0 || body.indexOf(']', slash) >= 0) return { base: body, modifier: '' };
395
+ return { base: body.slice(0, slash), modifier: body.slice(slash) };
396
+ }