@tenphi/tasty 0.0.0-snapshot.e0bc54a → 0.0.0-snapshot.e276af7
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
|
@@ -585,6 +585,20 @@ See [Debug Utilities](docs/debug.md).
|
|
|
585
585
|
|
|
586
586
|
Style definitions are declarative and fully typed, so agents can read and write them reliably. [Style Rules for AI Agents](docs/ai-agents.md) is a one-page brief to add to an agent's context.
|
|
587
587
|
|
|
588
|
+
The package also ships a harness for coding agents such as Claude Code and Codex, in [`harness/`](https://github.com/tenphi/tasty/tree/main/harness). With [harnesspm](https://www.npmjs.com/package/@tenphi/harnesspm), a project that depends on Tasty gets:
|
|
589
|
+
|
|
590
|
+
- a short rule: read the brief and the project's config first, use tokens and units, write state maps right;
|
|
591
|
+
- the docs index, so an agent reads the pages in `docs/` of the installed version;
|
|
592
|
+
- the `tasty:styles` tool: the CSS a styles object produces, with the app's `configure()` loaded through `--setup`, and a warning for every value that stayed unresolved.
|
|
593
|
+
|
|
594
|
+
```bash
|
|
595
|
+
npm i -D @tenphi/harnesspm
|
|
596
|
+
npx harnesspm init
|
|
597
|
+
npx harnesspm run tasty:styles '{"fill":"#surface","padding":"2x"}' --setup ./src/tasty-setup.ts
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
The tool runs only in projects that trust the package: `"trust": { "@tenphi/tasty": true }` in `harnesspm.json`.
|
|
601
|
+
|
|
588
602
|
## Performance
|
|
589
603
|
|
|
590
604
|
### Bundle size
|
package/docs/llms.txt
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Tasty
|
|
2
|
+
|
|
3
|
+
> Docs for `@tenphi/tasty`, the version installed in this project. Read the style rules for AI agents before you write or review styles.
|
|
4
|
+
|
|
5
|
+
## Start here
|
|
6
|
+
|
|
7
|
+
- [Tasty Style Rules for AI Agents](ai-agents.md): the rules for correct styles, with a checklist. Read it before you write or review styles.
|
|
8
|
+
- [Getting Started](getting-started.md): installation, a first component, configuration, the ESLint plugin, and choosing a rendering mode.
|
|
9
|
+
|
|
10
|
+
## Guides
|
|
11
|
+
|
|
12
|
+
- [Methodology](methodology.md): root and sub-elements, `styleProps` as the public API, `tokens`, `styles` vs `style`, wrapping and extending, anti-patterns.
|
|
13
|
+
- [Building a Design System](design-system.md): token vocabularies, state aliases, recipes, layout primitives, compound components, override contracts.
|
|
14
|
+
|
|
15
|
+
## Reference
|
|
16
|
+
|
|
17
|
+
- [Style DSL Reference](dsl.md): state maps and state keys, color tokens, units, replace tokens, recipes, advanced states (`@media`, `@parent`, `@root`, `:has`), keyframes, `@property`, `@font-face`, `@function`.
|
|
18
|
+
- [Style Properties Reference](styles.md): every Tasty style property (`fill`, `padding`, `border`, `radius`, `flow`, `preset`, …) and its syntax.
|
|
19
|
+
- [React API](react-api.md): `tasty()`, extending, `styleProps`, `modProps`, `tokenProps`, variants, sub-elements, `useStyles` and the other style functions.
|
|
20
|
+
- [Configuration](configuration.md): `configure()`: state aliases, units, tokens, replace tokens, recipes, presets, handlers, plugins.
|
|
21
|
+
- [Plugins & Extension Points](plugins.md): choosing between functions, units, states, handlers, prop handlers, recipes and base style props.
|
|
22
|
+
|
|
23
|
+
## Rendering modes
|
|
24
|
+
|
|
25
|
+
- [Build-Time Extraction (`tastyStatic`)](tasty-static.md): zero-runtime CSS through the Babel plugin, Next.js setup, config files, limitations.
|
|
26
|
+
- [Server-Side Rendering (SSR)](ssr.md): the style collector, hydration, Next.js App Router and Astro.
|
|
27
|
+
|
|
28
|
+
## Help
|
|
29
|
+
|
|
30
|
+
- [Tasty Debug Utilities](debug.md): `tastyDebug` in the browser console: the CSS on the page, cache metrics, chunks.
|
|
31
|
+
- [Migration Guide: v2 → v3](migration-v3.md): every breaking change from v2, with a search-and-replace cheat sheet.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Tasty styles
|
|
2
|
+
|
|
3
|
+
## Writing styles
|
|
4
|
+
|
|
5
|
+
- Before you write or review styles, read `ai-agents.md` from the Tasty docs index.
|
|
6
|
+
- Use only the tokens, units, presets, recipes and states the project defines in `tasty.config.ts` or `configure()`.
|
|
7
|
+
- Style with `tasty()` and `styles`, not the `style` prop. Colors are `#tokens`, sizes are units (`2x`, `1r`): no hex, `rgb()` or `px`.
|
|
8
|
+
- One state map per property, `''` first. Sub-elements are capitalized keys, not nested selectors.
|
|
9
|
+
|
|
10
|
+
## Checking styles
|
|
11
|
+
|
|
12
|
+
- To see the CSS that styles produce, run `tasty:styles '<json>' --setup <module that calls configure()>`.
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `tasty:styles` computes. Every function that reads Tasty takes the
|
|
3
|
+
* Tasty module as its first argument: the tool passes the package's `dist/`
|
|
4
|
+
* build, and `src/harness.test.ts` passes the source, so the tests need no
|
|
5
|
+
* build.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** CSS units. A number with any other unit in the output stayed raw. */
|
|
9
|
+
const CSS_UNITS = new Set([
|
|
10
|
+
'px',
|
|
11
|
+
'em',
|
|
12
|
+
'rem',
|
|
13
|
+
'ex',
|
|
14
|
+
'rex',
|
|
15
|
+
'ch',
|
|
16
|
+
'rch',
|
|
17
|
+
'cap',
|
|
18
|
+
'rcap',
|
|
19
|
+
'ic',
|
|
20
|
+
'ric',
|
|
21
|
+
'lh',
|
|
22
|
+
'rlh',
|
|
23
|
+
'vw',
|
|
24
|
+
'vh',
|
|
25
|
+
'vi',
|
|
26
|
+
'vb',
|
|
27
|
+
'vmin',
|
|
28
|
+
'vmax',
|
|
29
|
+
'svw',
|
|
30
|
+
'svh',
|
|
31
|
+
'svi',
|
|
32
|
+
'svb',
|
|
33
|
+
'svmin',
|
|
34
|
+
'svmax',
|
|
35
|
+
'lvw',
|
|
36
|
+
'lvh',
|
|
37
|
+
'lvi',
|
|
38
|
+
'lvb',
|
|
39
|
+
'lvmin',
|
|
40
|
+
'lvmax',
|
|
41
|
+
'dvw',
|
|
42
|
+
'dvh',
|
|
43
|
+
'dvi',
|
|
44
|
+
'dvb',
|
|
45
|
+
'dvmin',
|
|
46
|
+
'dvmax',
|
|
47
|
+
'cqw',
|
|
48
|
+
'cqh',
|
|
49
|
+
'cqi',
|
|
50
|
+
'cqb',
|
|
51
|
+
'cqmin',
|
|
52
|
+
'cqmax',
|
|
53
|
+
'cm',
|
|
54
|
+
'mm',
|
|
55
|
+
'q',
|
|
56
|
+
'in',
|
|
57
|
+
'pt',
|
|
58
|
+
'pc',
|
|
59
|
+
'deg',
|
|
60
|
+
'grad',
|
|
61
|
+
'rad',
|
|
62
|
+
'turn',
|
|
63
|
+
's',
|
|
64
|
+
'ms',
|
|
65
|
+
'hz',
|
|
66
|
+
'khz',
|
|
67
|
+
'dpi',
|
|
68
|
+
'dpcm',
|
|
69
|
+
'dppx',
|
|
70
|
+
'x',
|
|
71
|
+
'fr',
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
/** Words in a `preset` value that are modifiers, not preset names. */
|
|
75
|
+
const PRESET_MODIFIERS = new Set([
|
|
76
|
+
'strong',
|
|
77
|
+
'bold',
|
|
78
|
+
'italic',
|
|
79
|
+
'icon',
|
|
80
|
+
'tight',
|
|
81
|
+
'normal',
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Where `configure()` keeps its `tokens` (and the typography tokens that
|
|
86
|
+
* `presets` generate). Tasty shares them through `globalThis` so that every
|
|
87
|
+
* copy of the engine sees them; the tool reads them only to check preset
|
|
88
|
+
* names. `src/harness.test.ts` fails if the key changes.
|
|
89
|
+
*/
|
|
90
|
+
const CONFIG_TOKENS_KEY = '__tasty_cfg_tokens__';
|
|
91
|
+
|
|
92
|
+
/** The parts of the configuration that a setup module changes. */
|
|
93
|
+
export function configSummary(t) {
|
|
94
|
+
return {
|
|
95
|
+
units: { ...(t.getGlobalParser().getUnits() ?? {}) },
|
|
96
|
+
states: Object.keys(t.getGlobalPredefinedStates() ?? {}),
|
|
97
|
+
recipes: Object.keys(t.getGlobalRecipes() ?? {}),
|
|
98
|
+
tokens: Object.keys(globalThis[CONFIG_TOKENS_KEY] ?? {}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The parts of the configuration that belong to one copy of Tasty. Tokens are
|
|
104
|
+
* left out: Tasty shares them between copies through `globalThis`.
|
|
105
|
+
*/
|
|
106
|
+
export function configFingerprint(t) {
|
|
107
|
+
const { units, states, recipes } = configSummary(t);
|
|
108
|
+
return { units, states, recipes };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Whether two `configSummary()` or `configFingerprint()` results differ. */
|
|
112
|
+
export function configChanged(before, after) {
|
|
113
|
+
return JSON.stringify(before) !== JSON.stringify(after);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The CSS Tasty generates for a styles object, through `computeStyles()`:
|
|
118
|
+
* the render core `tasty()` and `useStyles()` share. Recipes are resolved,
|
|
119
|
+
* each style chunk gets its own class, and keyframes get their final names.
|
|
120
|
+
*/
|
|
121
|
+
export function renderCss(t, styles) {
|
|
122
|
+
const { className, css } = t.computeStyles(styles);
|
|
123
|
+
return {
|
|
124
|
+
classNames: (className ?? '').split(' ').filter(Boolean),
|
|
125
|
+
css: css ?? '',
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Splits CSS text into a tree: `{ prelude, children }` for a block and
|
|
131
|
+
* `{ property, value }` for a declaration. Strings and parentheses are kept
|
|
132
|
+
* whole, so a `;` or `{` inside them does not split.
|
|
133
|
+
*/
|
|
134
|
+
export function parseCss(css) {
|
|
135
|
+
const root = { prelude: '', children: [] };
|
|
136
|
+
const stack = [root];
|
|
137
|
+
let buffer = '';
|
|
138
|
+
let quote = '';
|
|
139
|
+
let depth = 0;
|
|
140
|
+
|
|
141
|
+
const flush = () => {
|
|
142
|
+
const text = buffer.trim();
|
|
143
|
+
buffer = '';
|
|
144
|
+
if (!text) return;
|
|
145
|
+
const colon = text.indexOf(':');
|
|
146
|
+
const node =
|
|
147
|
+
colon > 0
|
|
148
|
+
? {
|
|
149
|
+
property: text.slice(0, colon).trim(),
|
|
150
|
+
value: text.slice(colon + 1).trim(),
|
|
151
|
+
}
|
|
152
|
+
: { property: '', value: text };
|
|
153
|
+
stack.at(-1).children.push(node);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
for (const char of css) {
|
|
157
|
+
if (quote) {
|
|
158
|
+
buffer += char;
|
|
159
|
+
if (char === quote) quote = '';
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (char === '"' || char === "'") {
|
|
163
|
+
quote = char;
|
|
164
|
+
buffer += char;
|
|
165
|
+
} else if (char === '(') {
|
|
166
|
+
depth++;
|
|
167
|
+
buffer += char;
|
|
168
|
+
} else if (char === ')') {
|
|
169
|
+
depth = Math.max(0, depth - 1);
|
|
170
|
+
buffer += char;
|
|
171
|
+
} else if (depth > 0) {
|
|
172
|
+
buffer += char;
|
|
173
|
+
} else if (char === '{') {
|
|
174
|
+
const block = { prelude: buffer.trim(), children: [] };
|
|
175
|
+
buffer = '';
|
|
176
|
+
stack.at(-1).children.push(block);
|
|
177
|
+
stack.push(block);
|
|
178
|
+
} else if (char === ';') {
|
|
179
|
+
flush();
|
|
180
|
+
} else if (char === '}') {
|
|
181
|
+
flush();
|
|
182
|
+
if (stack.length > 1) stack.pop();
|
|
183
|
+
} else {
|
|
184
|
+
buffer += char;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
flush();
|
|
188
|
+
return root.children;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** CSS text, one declaration per line, blocks indented. */
|
|
192
|
+
export function formatCss(css) {
|
|
193
|
+
const lines = [];
|
|
194
|
+
const write = (nodes, indent) => {
|
|
195
|
+
for (const node of nodes) {
|
|
196
|
+
if ('prelude' in node) {
|
|
197
|
+
if (indent === '' && lines.length > 0) lines.push('');
|
|
198
|
+
lines.push(`${indent}${node.prelude} {`);
|
|
199
|
+
write(node.children, `${indent} `);
|
|
200
|
+
lines.push(`${indent}}`);
|
|
201
|
+
} else if (node.property) {
|
|
202
|
+
lines.push(`${indent}${node.property}: ${node.value};`);
|
|
203
|
+
} else {
|
|
204
|
+
lines.push(`${indent}${node.value}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
write(parseCss(css), '');
|
|
209
|
+
return lines.join('\n');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Every declaration in CSS text, at any depth. */
|
|
213
|
+
function declarationsOf(css) {
|
|
214
|
+
const found = [];
|
|
215
|
+
const walk = (nodes) => {
|
|
216
|
+
for (const node of nodes) {
|
|
217
|
+
if ('prelude' in node) walk(node.children);
|
|
218
|
+
else if (node.property) found.push(node);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
walk(parseCss(css));
|
|
222
|
+
return found;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The string values of one style property in a styles object and its
|
|
227
|
+
* sub-elements, including each value of a state map.
|
|
228
|
+
*/
|
|
229
|
+
function valuesOf(styles, property) {
|
|
230
|
+
const found = [];
|
|
231
|
+
const collect = (value) => {
|
|
232
|
+
if (typeof value === 'string') found.push(value);
|
|
233
|
+
else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
234
|
+
for (const inner of Object.values(value)) collect(inner);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const walk = (object) => {
|
|
238
|
+
for (const [key, value] of Object.entries(object)) {
|
|
239
|
+
if (key === property) collect(value);
|
|
240
|
+
else if (
|
|
241
|
+
/^[A-Z]/.test(key) &&
|
|
242
|
+
value &&
|
|
243
|
+
typeof value === 'object' &&
|
|
244
|
+
!Array.isArray(value)
|
|
245
|
+
) {
|
|
246
|
+
walk(value);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
walk(styles);
|
|
251
|
+
return found;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Values the CSS shows Tasty did not resolve, and names the loaded
|
|
256
|
+
* configuration does not define. Each issue is one sentence that says what to
|
|
257
|
+
* do. Tasty's own warnings are collected by the tool, not here.
|
|
258
|
+
*/
|
|
259
|
+
export function findIssues(t, styles, css) {
|
|
260
|
+
const issues = [];
|
|
261
|
+
|
|
262
|
+
// Once any recipe is configured, Tasty warns about an unknown one itself.
|
|
263
|
+
// With none configured, it drops every recipe silently.
|
|
264
|
+
if (!t.hasGlobalRecipes()) {
|
|
265
|
+
const names = new Set(
|
|
266
|
+
valuesOf(styles, 'recipe')
|
|
267
|
+
.flatMap((value) => value.split(/[\s/]+/))
|
|
268
|
+
.filter((name) => name && name !== 'none'),
|
|
269
|
+
);
|
|
270
|
+
for (const name of names) {
|
|
271
|
+
issues.push(
|
|
272
|
+
`Recipe "${name}" was dropped: no recipes are configured. Load the module that configures recipes with --setup.`,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const tokens = Object.keys(globalThis[CONFIG_TOKENS_KEY] ?? {});
|
|
278
|
+
const presets = new Set(
|
|
279
|
+
tokens
|
|
280
|
+
.filter((token) => token.endsWith('-font-size'))
|
|
281
|
+
.map((token) => token.slice(1, -'-font-size'.length)),
|
|
282
|
+
);
|
|
283
|
+
// Without typography tokens in the config, presets come from CSS this tool
|
|
284
|
+
// cannot see, so their names are not checked.
|
|
285
|
+
if (presets.size > 0) {
|
|
286
|
+
const missingPresets = new Set();
|
|
287
|
+
for (const value of valuesOf(styles, 'preset')) {
|
|
288
|
+
const name = value
|
|
289
|
+
.split(/[\s/]+/)
|
|
290
|
+
.find((word) => word && !PRESET_MODIFIERS.has(word));
|
|
291
|
+
if (name && name !== 'inherit' && !presets.has(name)) {
|
|
292
|
+
missingPresets.add(name);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
for (const name of missingPresets) {
|
|
296
|
+
issues.push(
|
|
297
|
+
`Preset "${name}" is not in the loaded config, so its font falls back to the default preset. Known presets: ${[...presets].join(', ')}.`,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const rawUnits = new Map();
|
|
303
|
+
for (const { property, value } of declarationsOf(css)) {
|
|
304
|
+
if (property.startsWith('--')) continue;
|
|
305
|
+
for (const match of value.matchAll(
|
|
306
|
+
/(?<![\w#.$-])(-?(?:\d+\.?\d*|\.\d+))([a-zA-Z]+)(?![\w(-])/g,
|
|
307
|
+
)) {
|
|
308
|
+
if (!CSS_UNITS.has(match[2].toLowerCase())) {
|
|
309
|
+
rawUnits.set(`${match[0]} ${property}`, [match[0], match[2], property]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
for (const [value, unit, property] of rawUnits.values()) {
|
|
314
|
+
issues.push(
|
|
315
|
+
`"${value}" in ${property} reached the CSS unchanged: "${unit}" is not a Tasty or CSS unit. Check the unit, or load the module that configures units with --setup.`,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return issues;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** How a unit list reads in the tool's header: `x = var(--gap), r = 6px`. */
|
|
323
|
+
export function describeUnits(units) {
|
|
324
|
+
return Object.entries(units)
|
|
325
|
+
.map(([name, value]) =>
|
|
326
|
+
typeof value === 'string' ? `${name} = ${value}` : `${name} = (function)`,
|
|
327
|
+
)
|
|
328
|
+
.join(', ');
|
|
329
|
+
}
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tasty:styles — prints the CSS Tasty generates for a styles object, with the
|
|
3
|
+
* app's configuration loaded first, and flags values that stayed unresolved.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
existsSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
realpathSync,
|
|
10
|
+
statSync,
|
|
11
|
+
} from 'node:fs';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
14
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
15
|
+
import {
|
|
16
|
+
configChanged,
|
|
17
|
+
configFingerprint,
|
|
18
|
+
configSummary,
|
|
19
|
+
describeUnits,
|
|
20
|
+
findIssues,
|
|
21
|
+
formatCss,
|
|
22
|
+
renderCss,
|
|
23
|
+
} from './render.mjs';
|
|
24
|
+
|
|
25
|
+
const USAGE = `Usage: npx harnesspm run tasty:styles '<styles JSON>' [--setup <module>]... [--json]
|
|
26
|
+
|
|
27
|
+
Prints the CSS Tasty generates for a styles object, as tasty() and useStyles()
|
|
28
|
+
render it: recipes resolved, one class per style chunk, and every state as a
|
|
29
|
+
selector or an at-rule. To see what a state key becomes, put it in a state map.
|
|
30
|
+
|
|
31
|
+
<styles JSON> the styles object as JSON, such as '{"fill":"#surface","padding":"2x"}'.
|
|
32
|
+
Use - to read it from stdin.
|
|
33
|
+
--setup a module that configures Tasty, imported before rendering: one that
|
|
34
|
+
calls configure() when imported, or one that default-exports a
|
|
35
|
+
configure() object, such as a tasty-zero.config.ts. A file path, or
|
|
36
|
+
a path inside a package, such as @acme/ui/dist/setup.js. Repeat it
|
|
37
|
+
for several modules. TypeScript needs Node 22.18 or later.
|
|
38
|
+
--json print the result as JSON
|
|
39
|
+
|
|
40
|
+
Run it from the package whose styles you check. Package paths in --setup resolve
|
|
41
|
+
from there, and the CSS comes from the copy of @tenphi/tasty that the setup
|
|
42
|
+
modules configure, which in a monorepo can differ from package to package.
|
|
43
|
+
|
|
44
|
+
Without --setup, the CSS uses Tasty's defaults (x = 8px, r = 6px, ...) and none
|
|
45
|
+
of the app's units, presets, recipes or state aliases.`;
|
|
46
|
+
|
|
47
|
+
const PACKAGE_ROOT = realpathSync(
|
|
48
|
+
fileURLToPath(new URL('../../../', import.meta.url)),
|
|
49
|
+
);
|
|
50
|
+
const BUILD = join(PACKAGE_ROOT, 'dist/core/index.js');
|
|
51
|
+
const cwd = process.cwd();
|
|
52
|
+
const projectDir = process.env.HARNESSPM_PROJECT_DIR || cwd;
|
|
53
|
+
|
|
54
|
+
function fail(message) {
|
|
55
|
+
console.error(`tasty: ${message}`);
|
|
56
|
+
process.exit(2);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function parseArgs(argv) {
|
|
60
|
+
const options = { setup: [] };
|
|
61
|
+
const positional = [];
|
|
62
|
+
for (let i = 0; i < argv.length; i++) {
|
|
63
|
+
const arg = argv[i];
|
|
64
|
+
if (arg === '-' || !arg.startsWith('--')) {
|
|
65
|
+
positional.push(arg);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const [key, inline] = arg.slice(2).split(/=(.*)/s);
|
|
69
|
+
if (key === 'json' || key === 'help') options[key] = true;
|
|
70
|
+
else if (key === 'setup') {
|
|
71
|
+
const value = inline ?? argv[++i];
|
|
72
|
+
if (value === undefined) fail('--setup needs a module');
|
|
73
|
+
options.setup.push(value);
|
|
74
|
+
} else fail(`unknown option --${key}`);
|
|
75
|
+
}
|
|
76
|
+
return { options, positional };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const { options, positional } = parseArgs(process.argv.slice(2));
|
|
80
|
+
if (options.help || positional.length !== 1) {
|
|
81
|
+
console.log(USAGE);
|
|
82
|
+
process.exit(options.help ? 0 : 2);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const source = positional[0] === '-' ? readFileSync(0, 'utf8') : positional[0];
|
|
86
|
+
let styles;
|
|
87
|
+
try {
|
|
88
|
+
styles = JSON.parse(source);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
fail(
|
|
91
|
+
`the styles must be JSON, with keys and strings in double quotes: ${error instanceof Error ? error.message : error}`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (!styles || typeof styles !== 'object' || Array.isArray(styles)) {
|
|
95
|
+
fail('the styles must be a JSON object');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!existsSync(BUILD)) {
|
|
99
|
+
fail(
|
|
100
|
+
`cannot find the Tasty build at ${BUILD}. In the Tasty repository, run \`pnpm build\` first.`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Tasty warns only in development, and the warnings are part of the answer.
|
|
105
|
+
process.env.NODE_ENV = 'development';
|
|
106
|
+
|
|
107
|
+
// Tasty reports problems with console.warn, also while a setup module is
|
|
108
|
+
// imported, so collect them from the start.
|
|
109
|
+
const tastyWarnings = [];
|
|
110
|
+
const warn = console.warn;
|
|
111
|
+
console.warn = (...args) => tastyWarnings.push(args.map(String).join(' '));
|
|
112
|
+
|
|
113
|
+
// Every copy of @tenphi/tasty this run can see, by its real folder. A setup
|
|
114
|
+
// module configures the copy it imports, which in a monorepo or with a second
|
|
115
|
+
// version installed need not be this tool's own.
|
|
116
|
+
const copies = new Map();
|
|
117
|
+
const own = await copyAt(PACKAGE_ROOT);
|
|
118
|
+
const appRoot = tastyRootFrom(cwd);
|
|
119
|
+
const app = appRoot === undefined ? undefined : await copyAt(appRoot);
|
|
120
|
+
|
|
121
|
+
const loaded = [];
|
|
122
|
+
for (const spec of options.setup) {
|
|
123
|
+
const file = resolveSetup(spec);
|
|
124
|
+
const nearRoot = tastyRootFrom(dirname(realpathSync(file)));
|
|
125
|
+
const near = nearRoot === undefined ? undefined : await copyAt(nearRoot);
|
|
126
|
+
const before = new Map(
|
|
127
|
+
[...copies.values()].map((c) => [c.root, configFingerprint(c.t)]),
|
|
128
|
+
);
|
|
129
|
+
let mod;
|
|
130
|
+
try {
|
|
131
|
+
mod = await import(pathToFileURL(file).href);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
console.warn = warn;
|
|
134
|
+
fail(`cannot import --setup ${spec}: ${importHint(file, error)}`);
|
|
135
|
+
}
|
|
136
|
+
const config = mod.default;
|
|
137
|
+
const isConfig =
|
|
138
|
+
config !== null &&
|
|
139
|
+
typeof config === 'object' &&
|
|
140
|
+
Object.getPrototypeOf(config) === Object.prototype;
|
|
141
|
+
let changed;
|
|
142
|
+
if (isConfig) {
|
|
143
|
+
const target = near ?? app ?? own;
|
|
144
|
+
try {
|
|
145
|
+
target.t.configure(config);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
console.warn = warn;
|
|
148
|
+
fail(
|
|
149
|
+
`configure() rejected the default export of ${spec}: ${error instanceof Error ? error.message : error}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
changed = [target];
|
|
153
|
+
} else {
|
|
154
|
+
changed = [...copies.values()].filter((c) =>
|
|
155
|
+
configChanged(before.get(c.root), configFingerprint(c.t)),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
loaded.push({
|
|
159
|
+
module: spec,
|
|
160
|
+
configured: isConfig
|
|
161
|
+
? 'default export'
|
|
162
|
+
: changed.length > 0
|
|
163
|
+
? 'on import'
|
|
164
|
+
: 'nothing',
|
|
165
|
+
tasty: changed.map((c) => ({ version: c.version, path: show(c.root) })),
|
|
166
|
+
});
|
|
167
|
+
for (const c of changed) c.setups.push(spec);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const configured = [...copies.values()].filter((c) => c.setups.length > 0);
|
|
171
|
+
const chosen =
|
|
172
|
+
configured.length === 0
|
|
173
|
+
? (app ?? own)
|
|
174
|
+
: configured.includes(app)
|
|
175
|
+
? app
|
|
176
|
+
: configured.reduce((a, b) =>
|
|
177
|
+
b.setups.length > a.setups.length ? b : a,
|
|
178
|
+
);
|
|
179
|
+
const t = chosen.t;
|
|
180
|
+
const version = chosen.version;
|
|
181
|
+
const config = configSummary(t);
|
|
182
|
+
|
|
183
|
+
const warnings = [];
|
|
184
|
+
if (loaded.length === 0) {
|
|
185
|
+
const candidates = findSetupCandidates(
|
|
186
|
+
cwd.startsWith(projectDir) ? cwd : projectDir,
|
|
187
|
+
);
|
|
188
|
+
warnings.push(
|
|
189
|
+
candidates.length > 0
|
|
190
|
+
? `No --setup: this is the CSS for Tasty's defaults, not the app's config. These files configure Tasty: ${candidates.join(', ')}. Run again with --setup <file>.`
|
|
191
|
+
: "No --setup: this is the CSS for Tasty's defaults. No file in this project calls configure() from @tenphi/tasty. If the app configures Tasty elsewhere, such as in a UI library, pass that module with --setup.",
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
for (const entry of loaded.filter((l) => l.configured === 'nothing')) {
|
|
195
|
+
warnings.push(
|
|
196
|
+
`--setup ${entry.module} changed no Tasty config this tool can see. It may call configure() only inside a function, or configure a copy of @tenphi/tasty that neither this package nor the other setup modules use.`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
if (configured.length > 1) {
|
|
200
|
+
const parts = configured.map(
|
|
201
|
+
(c) => `${c.version} (${show(c.root)}) got ${c.setups.join(', ')}`,
|
|
202
|
+
);
|
|
203
|
+
warnings.push(
|
|
204
|
+
`The --setup modules configure ${configured.length} copies of @tenphi/tasty, so this CSS has only part of the config: ${parts.join('; ')}. This CSS comes from ${chosen.version} (${show(chosen.root)}). The app runs these copies too, each with part of the config: make every package use one version of @tenphi/tasty.`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let result;
|
|
209
|
+
try {
|
|
210
|
+
result = renderCss(t, styles);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
console.warn = warn;
|
|
213
|
+
fail(
|
|
214
|
+
`Tasty could not render these styles: ${error instanceof Error ? error.message : error}`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
warnings.push(...findIssues(t, styles, result.css));
|
|
218
|
+
console.warn = warn;
|
|
219
|
+
warnings.push(
|
|
220
|
+
...new Set(tastyWarnings.map((w) => w.trim().replace(/\s*\n\s*/g, ' '))),
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
if (options.json) {
|
|
224
|
+
console.log(
|
|
225
|
+
JSON.stringify(
|
|
226
|
+
{
|
|
227
|
+
tasty: { version, path: show(chosen.root) },
|
|
228
|
+
setup: loaded,
|
|
229
|
+
config,
|
|
230
|
+
classNames: result.classNames,
|
|
231
|
+
css: result.css,
|
|
232
|
+
warnings,
|
|
233
|
+
},
|
|
234
|
+
null,
|
|
235
|
+
2,
|
|
236
|
+
),
|
|
237
|
+
);
|
|
238
|
+
} else {
|
|
239
|
+
console.log(render());
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function render() {
|
|
243
|
+
const out = [];
|
|
244
|
+
const setup =
|
|
245
|
+
loaded.length > 0
|
|
246
|
+
? `setup ${loaded.map((l) => l.module).join(', ')}`
|
|
247
|
+
: 'no setup, Tasty defaults';
|
|
248
|
+
const from = chosen === own ? '' : ` (${show(chosen.root)})`;
|
|
249
|
+
out.push(`Tasty ${version}${from}, ${setup}`);
|
|
250
|
+
const parts = [`units ${describeUnits(config.units)}`];
|
|
251
|
+
if (config.states.length > 0) parts.push(`states ${list(config.states)}`);
|
|
252
|
+
if (config.recipes.length > 0) parts.push(`recipes ${list(config.recipes)}`);
|
|
253
|
+
out.push(`Config: ${parts.join('; ')}`);
|
|
254
|
+
if (result.classNames.length > 0) {
|
|
255
|
+
out.push(
|
|
256
|
+
`Classes: ${result.classNames.join(' ')} (one per style chunk, doubled in selectors as at runtime)`,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
out.push('', result.css ? formatCss(result.css) : '(no CSS)');
|
|
260
|
+
if (warnings.length > 0) {
|
|
261
|
+
out.push('', 'Warnings:', ...warnings.map((w) => `- ${w}`));
|
|
262
|
+
}
|
|
263
|
+
return out.join('\n');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function list(names) {
|
|
267
|
+
return names.length <= 8
|
|
268
|
+
? names.join(', ')
|
|
269
|
+
: `${names.slice(0, 8).join(', ')} and ${names.length - 8} more`;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A file path, or a path inside a package installed in the project. */
|
|
273
|
+
function resolveSetup(spec) {
|
|
274
|
+
if (
|
|
275
|
+
/(^|\/)tasty\.config\.[cm]?[jt]s$|(^|\/)tasty\.config\.json$/.test(spec)
|
|
276
|
+
) {
|
|
277
|
+
fail(
|
|
278
|
+
`${spec} only lists names for the ESLint plugin and the editor. Pass the module that calls configure(), or one that default-exports its config.`,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
const asPath = resolve(spec);
|
|
282
|
+
if (spec.startsWith('.') || isAbsolute(spec) || existsSync(asPath)) {
|
|
283
|
+
if (!existsSync(asPath)) fail(`cannot find --setup ${spec}`);
|
|
284
|
+
return asPath;
|
|
285
|
+
}
|
|
286
|
+
// From the current package first: in a monorepo, each package can install
|
|
287
|
+
// its own version.
|
|
288
|
+
for (const base of new Set([cwd, projectDir])) {
|
|
289
|
+
try {
|
|
290
|
+
return createRequire(join(base, 'noop.js')).resolve(spec);
|
|
291
|
+
} catch {
|
|
292
|
+
// A path the package's exports do not list, such as a file in its dist/.
|
|
293
|
+
}
|
|
294
|
+
for (let dir = base; ; dir = dirname(dir)) {
|
|
295
|
+
const candidate = join(dir, 'node_modules', spec);
|
|
296
|
+
if (existsSync(candidate)) return candidate;
|
|
297
|
+
if (dirname(dir) === dir) break;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return fail(`cannot resolve --setup ${spec} from ${cwd}`);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** The real folder of the @tenphi/tasty that code in `dir` imports. */
|
|
304
|
+
function tastyRootFrom(dir) {
|
|
305
|
+
let entry;
|
|
306
|
+
try {
|
|
307
|
+
entry = createRequire(join(dir, 'noop.js')).resolve('@tenphi/tasty');
|
|
308
|
+
} catch {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
for (let d = dirname(entry); dirname(d) !== d; d = dirname(d)) {
|
|
312
|
+
const manifest = join(d, 'package.json');
|
|
313
|
+
if (
|
|
314
|
+
existsSync(manifest) &&
|
|
315
|
+
JSON.parse(readFileSync(manifest, 'utf8')).name === '@tenphi/tasty'
|
|
316
|
+
) {
|
|
317
|
+
return realpathSync(d);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Loads a copy of Tasty once; undefined when it has no usable build. */
|
|
324
|
+
async function copyAt(root) {
|
|
325
|
+
if (copies.has(root)) return copies.get(root);
|
|
326
|
+
const build = join(root, 'dist/core/index.js');
|
|
327
|
+
if (!existsSync(build)) return undefined;
|
|
328
|
+
const core = await import(pathToFileURL(build).href);
|
|
329
|
+
if (typeof core.computeStyles !== 'function') return undefined;
|
|
330
|
+
const copy = {
|
|
331
|
+
root,
|
|
332
|
+
version: JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
|
|
333
|
+
.version,
|
|
334
|
+
t: core,
|
|
335
|
+
setups: [],
|
|
336
|
+
};
|
|
337
|
+
copies.set(root, copy);
|
|
338
|
+
return copy;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** A path as the person sees it: relative to the current folder. */
|
|
342
|
+
function show(path) {
|
|
343
|
+
return relative(cwd, path) || '.';
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function importHint(file, error) {
|
|
347
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
348
|
+
const code = error && typeof error === 'object' ? error.code : undefined;
|
|
349
|
+
if (code === 'ERR_UNKNOWN_FILE_EXTENSION') {
|
|
350
|
+
return `${message}. Node cannot import this file type. Pass a module without JSX, a built .js file, or a .ts file on Node 22.18 or later.`;
|
|
351
|
+
}
|
|
352
|
+
if (code === 'ERR_MODULE_NOT_FOUND' && /\.[cm]?tsx?$/.test(file)) {
|
|
353
|
+
return `${message}. Node needs full file extensions in relative imports, and does not read tsconfig paths.`;
|
|
354
|
+
}
|
|
355
|
+
return message;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Up to five project files that import Tasty and configure it. */
|
|
359
|
+
function findSetupCandidates(root) {
|
|
360
|
+
const SKIP = new Set([
|
|
361
|
+
'node_modules',
|
|
362
|
+
'dist',
|
|
363
|
+
'build',
|
|
364
|
+
'out',
|
|
365
|
+
'coverage',
|
|
366
|
+
'storybook-static',
|
|
367
|
+
]);
|
|
368
|
+
const found = [];
|
|
369
|
+
let budget = 5000;
|
|
370
|
+
const walk = (dir, depth) => {
|
|
371
|
+
if (depth > 6 || found.length >= 5) return;
|
|
372
|
+
let entries;
|
|
373
|
+
try {
|
|
374
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
375
|
+
} catch {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
for (const entry of entries) {
|
|
379
|
+
if (found.length >= 5 || --budget < 0) return;
|
|
380
|
+
const path = join(dir, entry.name);
|
|
381
|
+
if (entry.isDirectory()) {
|
|
382
|
+
if (!entry.name.startsWith('.') && !SKIP.has(entry.name)) {
|
|
383
|
+
walk(path, depth + 1);
|
|
384
|
+
}
|
|
385
|
+
} else if (
|
|
386
|
+
/\.[cm]?[jt]sx?$/.test(entry.name) &&
|
|
387
|
+
!/\.(test|spec|stories|d)\.[cm]?[jt]sx?$/.test(entry.name) &&
|
|
388
|
+
!/^tasty\.config\./.test(entry.name)
|
|
389
|
+
) {
|
|
390
|
+
try {
|
|
391
|
+
if (statSync(path).size > 256 * 1024) continue;
|
|
392
|
+
const text = readFileSync(path, 'utf8');
|
|
393
|
+
if (
|
|
394
|
+
/['"]@tenphi\/tasty(\/[\w/-]*)?['"]/.test(text) &&
|
|
395
|
+
/\b(configure|setGlobalPredefinedStates)\s*\(/.test(text)
|
|
396
|
+
) {
|
|
397
|
+
found.push(relative(root, path));
|
|
398
|
+
} else if (/^tasty[\w.-]*\.config\.[cm]?[jt]s$/.test(entry.name)) {
|
|
399
|
+
found.push(relative(root, path));
|
|
400
|
+
}
|
|
401
|
+
} catch {
|
|
402
|
+
// Unreadable files are not candidates.
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
walk(root, 0);
|
|
408
|
+
return found;
|
|
409
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json",
|
|
3
|
+
"name": "tasty",
|
|
4
|
+
"description": "Tasty styles: the style rules, the docs of the installed version, and a tool that prints the CSS a styles object produces",
|
|
5
|
+
"homepage": "https://tasty.style",
|
|
6
|
+
"repository": "https://github.com/tenphi/tasty",
|
|
7
|
+
"keywords": ["tasty", "css-in-js", "styles", "design-system", "react"],
|
|
8
|
+
"extensions": {
|
|
9
|
+
"dev.harnesspm": {
|
|
10
|
+
"docs": "../docs",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"styles": {
|
|
13
|
+
"file": "./dev.harnesspm/scripts/styles.mjs",
|
|
14
|
+
"description": "Print the CSS a Tasty styles object produces, with the app's configure() loaded through --setup, and flag values that stayed unresolved",
|
|
15
|
+
"allow": true
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tenphi/tasty",
|
|
3
|
-
"version": "0.0.0-snapshot.
|
|
3
|
+
"version": "0.0.0-snapshot.e276af7",
|
|
4
4
|
"description": "A design-system-integrated styling system and DSL for concise, state-aware UI styling",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -97,6 +97,7 @@
|
|
|
97
97
|
"files": [
|
|
98
98
|
"dist",
|
|
99
99
|
"docs",
|
|
100
|
+
"harness",
|
|
100
101
|
"tasty.config.ts"
|
|
101
102
|
],
|
|
102
103
|
"sideEffects": [
|
|
@@ -121,10 +122,10 @@
|
|
|
121
122
|
"test:watch": "vitest",
|
|
122
123
|
"test:coverage": "vitest run --coverage",
|
|
123
124
|
"typecheck": "tsc --noEmit",
|
|
124
|
-
"lint": "eslint src",
|
|
125
|
-
"lint:fix": "eslint src --fix",
|
|
126
|
-
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
|
|
127
|
-
"format:check": "prettier --check \"src/**/*.{ts,tsx}\"",
|
|
125
|
+
"lint": "eslint src harness",
|
|
126
|
+
"lint:fix": "eslint src harness --fix",
|
|
127
|
+
"format": "prettier --write \"src/**/*.{ts,tsx}\" \"harness/**/*.mjs\"",
|
|
128
|
+
"format:check": "prettier --check \"src/**/*.{ts,tsx}\" \"harness/**/*.mjs\"",
|
|
128
129
|
"size": "size-limit",
|
|
129
130
|
"bench": "vitest bench --project \"node (bench)\"",
|
|
130
131
|
"bench:browser": "vitest bench --project \"browser (chromium) (bench)\"",
|
|
@@ -138,6 +139,7 @@
|
|
|
138
139
|
"version": "changeset version",
|
|
139
140
|
"release": "changeset publish",
|
|
140
141
|
"knip": "knip",
|
|
142
|
+
"check:harness": "harnesspm check",
|
|
141
143
|
"check:build": "node scripts/check-built-entries.mjs",
|
|
142
144
|
"check:react-compat": "node scripts/check-react-compat.mjs",
|
|
143
145
|
"check:babel-compat": "node scripts/check-babel-compat.mjs",
|
|
@@ -207,7 +209,8 @@
|
|
|
207
209
|
"@eslint/js": "^10.0.1",
|
|
208
210
|
"@size-limit/esbuild": "^14.1.0",
|
|
209
211
|
"@size-limit/file": "^14.1.0",
|
|
210
|
-
"@tenphi/eslint-plugin-tasty": "^1.
|
|
212
|
+
"@tenphi/eslint-plugin-tasty": "^1.19.1",
|
|
213
|
+
"@tenphi/harnesspm": "^0.3.0",
|
|
211
214
|
"@testing-library/jest-dom": "^7.0.1",
|
|
212
215
|
"@testing-library/react": "^16.3.3",
|
|
213
216
|
"@types/node": "^24.19.1",
|