@tenphi/tasty 3.17.1 → 3.19.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.
- package/README.md +14 -0
- package/dist/chunks/{build-config-D9Iq8QQm.js → build-config--0qoqt_P.js} +4 -4
- package/dist/chunks/{build-config-D9Iq8QQm.js.map → build-config--0qoqt_P.js.map} +1 -1
- package/dist/chunks/{collector-Cb8ah2JF.js → collector-zp5BrLF0.js} +4 -4
- package/dist/chunks/{collector-Cb8ah2JF.js.map → collector-zp5BrLF0.js.map} +1 -1
- package/dist/chunks/{config-engine-CMYFZYQh.js → config-engine-BaLq8HrX.js} +3 -3
- package/dist/chunks/{config-engine-CMYFZYQh.js.map → config-engine-BaLq8HrX.js.map} +1 -1
- package/dist/chunks/{css-definitions-B4aWBkH3.js → css-definitions-CfXZ3tU9.js} +2 -2
- package/dist/chunks/{css-definitions-B4aWBkH3.js.map → css-definitions-CfXZ3tU9.js.map} +1 -1
- package/dist/chunks/{debug-D2Pu7Xhy.js → debug--OC-WA-Q.js} +4 -4
- package/dist/chunks/{debug-D2Pu7Xhy.js.map → debug--OC-WA-Q.js.map} +1 -1
- package/dist/chunks/{react-runtime-DHiopXCu.js → react-runtime-DnZ6M1Iv.js} +5 -5
- package/dist/chunks/{react-runtime-DHiopXCu.js.map → react-runtime-DnZ6M1Iv.js.map} +1 -1
- package/dist/chunks/{runtime-engine-NGCofNXy.js → runtime-engine-J9X_h-Pw.js} +5 -5
- package/dist/chunks/{runtime-engine-NGCofNXy.js.map → runtime-engine-J9X_h-Pw.js.map} +1 -1
- package/dist/chunks/{runtime-state-lZjMbvwV.js → runtime-state-o_v3Bj7b.js} +2 -2
- package/dist/chunks/{runtime-state-lZjMbvwV.js.map → runtime-state-o_v3Bj7b.js.map} +1 -1
- package/dist/chunks/{style-engine-DnkV_4Jg.js → style-engine-D6lC3O3G.js} +37 -13
- package/dist/chunks/style-engine-D6lC3O3G.js.map +1 -0
- package/dist/chunks/{zero-engine-DcwaCvFs.js → zero-engine-BnjoV04F.js} +5 -5
- package/dist/chunks/{zero-engine-DcwaCvFs.js.map → zero-engine-BnjoV04F.js.map} +1 -1
- package/dist/core/index.js +6 -6
- package/dist/index.js +7 -7
- package/dist/ssr/astro.js +2 -2
- package/dist/ssr/index.js +2 -2
- package/dist/ssr/next-config.js +3 -3
- package/dist/ssr/next.js +3 -3
- package/dist/zero/babel.js +4 -4
- package/dist/zero/index.js +3 -3
- package/docs/ai-agents.md +1 -1
- package/docs/design-system.md +12 -0
- package/docs/dsl.md +20 -0
- package/docs/llms.txt +31 -0
- package/docs/pipeline.md +1 -1
- package/harness/dev.harnesspm/rules/usage.md +12 -0
- package/harness/dev.harnesspm/scripts/render.mjs +320 -0
- package/harness/dev.harnesspm/scripts/styles.mjs +299 -0
- package/harness/plugin.json +20 -0
- package/package.json +9 -6
- package/dist/chunks/style-engine-DnkV_4Jg.js.map +0 -1
|
@@ -0,0 +1,320 @@
|
|
|
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
|
+
/** Whether two `configSummary()` results differ. */
|
|
103
|
+
export function configChanged(before, after) {
|
|
104
|
+
return JSON.stringify(before) !== JSON.stringify(after);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The CSS Tasty generates for a styles object, through `computeStyles()`:
|
|
109
|
+
* the render core `tasty()` and `useStyles()` share. Recipes are resolved,
|
|
110
|
+
* each style chunk gets its own class, and keyframes get their final names.
|
|
111
|
+
*/
|
|
112
|
+
export function renderCss(t, styles) {
|
|
113
|
+
const { className, css } = t.computeStyles(styles);
|
|
114
|
+
return {
|
|
115
|
+
classNames: (className ?? '').split(' ').filter(Boolean),
|
|
116
|
+
css: css ?? '',
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Splits CSS text into a tree: `{ prelude, children }` for a block and
|
|
122
|
+
* `{ property, value }` for a declaration. Strings and parentheses are kept
|
|
123
|
+
* whole, so a `;` or `{` inside them does not split.
|
|
124
|
+
*/
|
|
125
|
+
export function parseCss(css) {
|
|
126
|
+
const root = { prelude: '', children: [] };
|
|
127
|
+
const stack = [root];
|
|
128
|
+
let buffer = '';
|
|
129
|
+
let quote = '';
|
|
130
|
+
let depth = 0;
|
|
131
|
+
|
|
132
|
+
const flush = () => {
|
|
133
|
+
const text = buffer.trim();
|
|
134
|
+
buffer = '';
|
|
135
|
+
if (!text) return;
|
|
136
|
+
const colon = text.indexOf(':');
|
|
137
|
+
const node =
|
|
138
|
+
colon > 0
|
|
139
|
+
? {
|
|
140
|
+
property: text.slice(0, colon).trim(),
|
|
141
|
+
value: text.slice(colon + 1).trim(),
|
|
142
|
+
}
|
|
143
|
+
: { property: '', value: text };
|
|
144
|
+
stack.at(-1).children.push(node);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
for (const char of css) {
|
|
148
|
+
if (quote) {
|
|
149
|
+
buffer += char;
|
|
150
|
+
if (char === quote) quote = '';
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (char === '"' || char === "'") {
|
|
154
|
+
quote = char;
|
|
155
|
+
buffer += char;
|
|
156
|
+
} else if (char === '(') {
|
|
157
|
+
depth++;
|
|
158
|
+
buffer += char;
|
|
159
|
+
} else if (char === ')') {
|
|
160
|
+
depth = Math.max(0, depth - 1);
|
|
161
|
+
buffer += char;
|
|
162
|
+
} else if (depth > 0) {
|
|
163
|
+
buffer += char;
|
|
164
|
+
} else if (char === '{') {
|
|
165
|
+
const block = { prelude: buffer.trim(), children: [] };
|
|
166
|
+
buffer = '';
|
|
167
|
+
stack.at(-1).children.push(block);
|
|
168
|
+
stack.push(block);
|
|
169
|
+
} else if (char === ';') {
|
|
170
|
+
flush();
|
|
171
|
+
} else if (char === '}') {
|
|
172
|
+
flush();
|
|
173
|
+
if (stack.length > 1) stack.pop();
|
|
174
|
+
} else {
|
|
175
|
+
buffer += char;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
flush();
|
|
179
|
+
return root.children;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** CSS text, one declaration per line, blocks indented. */
|
|
183
|
+
export function formatCss(css) {
|
|
184
|
+
const lines = [];
|
|
185
|
+
const write = (nodes, indent) => {
|
|
186
|
+
for (const node of nodes) {
|
|
187
|
+
if ('prelude' in node) {
|
|
188
|
+
if (indent === '' && lines.length > 0) lines.push('');
|
|
189
|
+
lines.push(`${indent}${node.prelude} {`);
|
|
190
|
+
write(node.children, `${indent} `);
|
|
191
|
+
lines.push(`${indent}}`);
|
|
192
|
+
} else if (node.property) {
|
|
193
|
+
lines.push(`${indent}${node.property}: ${node.value};`);
|
|
194
|
+
} else {
|
|
195
|
+
lines.push(`${indent}${node.value}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
write(parseCss(css), '');
|
|
200
|
+
return lines.join('\n');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Every declaration in CSS text, at any depth. */
|
|
204
|
+
function declarationsOf(css) {
|
|
205
|
+
const found = [];
|
|
206
|
+
const walk = (nodes) => {
|
|
207
|
+
for (const node of nodes) {
|
|
208
|
+
if ('prelude' in node) walk(node.children);
|
|
209
|
+
else if (node.property) found.push(node);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
walk(parseCss(css));
|
|
213
|
+
return found;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The string values of one style property in a styles object and its
|
|
218
|
+
* sub-elements, including each value of a state map.
|
|
219
|
+
*/
|
|
220
|
+
function valuesOf(styles, property) {
|
|
221
|
+
const found = [];
|
|
222
|
+
const collect = (value) => {
|
|
223
|
+
if (typeof value === 'string') found.push(value);
|
|
224
|
+
else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
225
|
+
for (const inner of Object.values(value)) collect(inner);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const walk = (object) => {
|
|
229
|
+
for (const [key, value] of Object.entries(object)) {
|
|
230
|
+
if (key === property) collect(value);
|
|
231
|
+
else if (
|
|
232
|
+
/^[A-Z]/.test(key) &&
|
|
233
|
+
value &&
|
|
234
|
+
typeof value === 'object' &&
|
|
235
|
+
!Array.isArray(value)
|
|
236
|
+
) {
|
|
237
|
+
walk(value);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
walk(styles);
|
|
242
|
+
return found;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Values the CSS shows Tasty did not resolve, and names the loaded
|
|
247
|
+
* configuration does not define. Each issue is one sentence that says what to
|
|
248
|
+
* do. Tasty's own warnings are collected by the tool, not here.
|
|
249
|
+
*/
|
|
250
|
+
export function findIssues(t, styles, css) {
|
|
251
|
+
const issues = [];
|
|
252
|
+
|
|
253
|
+
// Once any recipe is configured, Tasty warns about an unknown one itself.
|
|
254
|
+
// With none configured, it drops every recipe silently.
|
|
255
|
+
if (!t.hasGlobalRecipes()) {
|
|
256
|
+
const names = new Set(
|
|
257
|
+
valuesOf(styles, 'recipe')
|
|
258
|
+
.flatMap((value) => value.split(/[\s/]+/))
|
|
259
|
+
.filter((name) => name && name !== 'none'),
|
|
260
|
+
);
|
|
261
|
+
for (const name of names) {
|
|
262
|
+
issues.push(
|
|
263
|
+
`Recipe "${name}" was dropped: no recipes are configured. Load the module that configures recipes with --setup.`,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const tokens = Object.keys(globalThis[CONFIG_TOKENS_KEY] ?? {});
|
|
269
|
+
const presets = new Set(
|
|
270
|
+
tokens
|
|
271
|
+
.filter((token) => token.endsWith('-font-size'))
|
|
272
|
+
.map((token) => token.slice(1, -'-font-size'.length)),
|
|
273
|
+
);
|
|
274
|
+
// Without typography tokens in the config, presets come from CSS this tool
|
|
275
|
+
// cannot see, so their names are not checked.
|
|
276
|
+
if (presets.size > 0) {
|
|
277
|
+
const missingPresets = new Set();
|
|
278
|
+
for (const value of valuesOf(styles, 'preset')) {
|
|
279
|
+
const name = value
|
|
280
|
+
.split(/[\s/]+/)
|
|
281
|
+
.find((word) => word && !PRESET_MODIFIERS.has(word));
|
|
282
|
+
if (name && name !== 'inherit' && !presets.has(name)) {
|
|
283
|
+
missingPresets.add(name);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
for (const name of missingPresets) {
|
|
287
|
+
issues.push(
|
|
288
|
+
`Preset "${name}" is not in the loaded config, so its font falls back to the default preset. Known presets: ${[...presets].join(', ')}.`,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const rawUnits = new Map();
|
|
294
|
+
for (const { property, value } of declarationsOf(css)) {
|
|
295
|
+
if (property.startsWith('--')) continue;
|
|
296
|
+
for (const match of value.matchAll(
|
|
297
|
+
/(?<![\w#.$-])(-?(?:\d+\.?\d*|\.\d+))([a-zA-Z]+)(?![\w(-])/g,
|
|
298
|
+
)) {
|
|
299
|
+
if (!CSS_UNITS.has(match[2].toLowerCase())) {
|
|
300
|
+
rawUnits.set(`${match[0]} ${property}`, [match[0], match[2], property]);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
for (const [value, unit, property] of rawUnits.values()) {
|
|
305
|
+
issues.push(
|
|
306
|
+
`"${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.`,
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return issues;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** How a unit list reads in the tool's header: `x = var(--gap), r = 6px`. */
|
|
314
|
+
export function describeUnits(units) {
|
|
315
|
+
return Object.entries(units)
|
|
316
|
+
.map(([name, value]) =>
|
|
317
|
+
typeof value === 'string' ? `${name} = ${value}` : `${name} = (function)`,
|
|
318
|
+
)
|
|
319
|
+
.join(', ');
|
|
320
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
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 { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
8
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
9
|
+
import {
|
|
10
|
+
configChanged,
|
|
11
|
+
configSummary,
|
|
12
|
+
describeUnits,
|
|
13
|
+
findIssues,
|
|
14
|
+
formatCss,
|
|
15
|
+
renderCss,
|
|
16
|
+
} from './render.mjs';
|
|
17
|
+
|
|
18
|
+
const USAGE = `Usage: npx harnesspm run tasty:styles '<styles JSON>' [--setup <module>]... [--json]
|
|
19
|
+
|
|
20
|
+
Prints the CSS Tasty generates for a styles object, as tasty() and useStyles()
|
|
21
|
+
render it: recipes resolved, one class per style chunk, and every state as a
|
|
22
|
+
selector or an at-rule. To see what a state key becomes, put it in a state map.
|
|
23
|
+
|
|
24
|
+
<styles JSON> the styles object as JSON, such as '{"fill":"#surface","padding":"2x"}'.
|
|
25
|
+
Use - to read it from stdin.
|
|
26
|
+
--setup a module that configures Tasty, imported before rendering: one that
|
|
27
|
+
calls configure() when imported, or one that default-exports a
|
|
28
|
+
configure() object, such as a tasty-zero.config.ts. A file path, or
|
|
29
|
+
a path inside a package, such as @acme/ui/dist/setup.js. Repeat it
|
|
30
|
+
for several modules. TypeScript needs Node 22.18 or later.
|
|
31
|
+
--json print the result as JSON
|
|
32
|
+
|
|
33
|
+
Without --setup, the CSS uses Tasty's defaults (x = 8px, r = 6px, ...) and none
|
|
34
|
+
of the app's units, presets, recipes or state aliases.`;
|
|
35
|
+
|
|
36
|
+
const PACKAGE_ROOT = fileURLToPath(new URL('../../../', import.meta.url));
|
|
37
|
+
const BUILD = join(PACKAGE_ROOT, 'dist/core/index.js');
|
|
38
|
+
const projectDir = process.env.HARNESSPM_PROJECT_DIR || process.cwd();
|
|
39
|
+
|
|
40
|
+
function fail(message) {
|
|
41
|
+
console.error(`tasty: ${message}`);
|
|
42
|
+
process.exit(2);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseArgs(argv) {
|
|
46
|
+
const options = { setup: [] };
|
|
47
|
+
const positional = [];
|
|
48
|
+
for (let i = 0; i < argv.length; i++) {
|
|
49
|
+
const arg = argv[i];
|
|
50
|
+
if (arg === '-' || !arg.startsWith('--')) {
|
|
51
|
+
positional.push(arg);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const [key, inline] = arg.slice(2).split(/=(.*)/s);
|
|
55
|
+
if (key === 'json' || key === 'help') options[key] = true;
|
|
56
|
+
else if (key === 'setup') {
|
|
57
|
+
const value = inline ?? argv[++i];
|
|
58
|
+
if (value === undefined) fail('--setup needs a module');
|
|
59
|
+
options.setup.push(value);
|
|
60
|
+
} else fail(`unknown option --${key}`);
|
|
61
|
+
}
|
|
62
|
+
return { options, positional };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { options, positional } = parseArgs(process.argv.slice(2));
|
|
66
|
+
if (options.help || positional.length !== 1) {
|
|
67
|
+
console.log(USAGE);
|
|
68
|
+
process.exit(options.help ? 0 : 2);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const source = positional[0] === '-' ? readFileSync(0, 'utf8') : positional[0];
|
|
72
|
+
let styles;
|
|
73
|
+
try {
|
|
74
|
+
styles = JSON.parse(source);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
fail(
|
|
77
|
+
`the styles must be JSON, with keys and strings in double quotes: ${error instanceof Error ? error.message : error}`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
if (!styles || typeof styles !== 'object' || Array.isArray(styles)) {
|
|
81
|
+
fail('the styles must be a JSON object');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!existsSync(BUILD)) {
|
|
85
|
+
fail(
|
|
86
|
+
`cannot find the Tasty build at ${BUILD}. In the Tasty repository, run \`pnpm build\` first.`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Tasty warns only in development, and the warnings are part of the answer.
|
|
91
|
+
process.env.NODE_ENV = 'development';
|
|
92
|
+
|
|
93
|
+
// Tasty reports problems with console.warn, also while a setup module is
|
|
94
|
+
// imported, so collect them from the start.
|
|
95
|
+
const tastyWarnings = [];
|
|
96
|
+
const warn = console.warn;
|
|
97
|
+
console.warn = (...args) => tastyWarnings.push(args.map(String).join(' '));
|
|
98
|
+
|
|
99
|
+
const t = await import(pathToFileURL(BUILD).href);
|
|
100
|
+
const version = JSON.parse(
|
|
101
|
+
readFileSync(join(PACKAGE_ROOT, 'package.json'), 'utf8'),
|
|
102
|
+
).version;
|
|
103
|
+
|
|
104
|
+
const before = configSummary(t);
|
|
105
|
+
const loaded = [];
|
|
106
|
+
for (const spec of options.setup) {
|
|
107
|
+
const file = resolveSetup(spec);
|
|
108
|
+
let mod;
|
|
109
|
+
try {
|
|
110
|
+
mod = await import(pathToFileURL(file).href);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.warn = warn;
|
|
113
|
+
fail(`cannot import --setup ${spec}: ${importHint(file, error)}`);
|
|
114
|
+
}
|
|
115
|
+
const config = mod.default;
|
|
116
|
+
if (config && Object.getPrototypeOf(config) === Object.prototype) {
|
|
117
|
+
try {
|
|
118
|
+
t.configure(config);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.warn = warn;
|
|
121
|
+
fail(
|
|
122
|
+
`configure() rejected the default export of ${spec}: ${error instanceof Error ? error.message : error}`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
loaded.push({ module: spec, configured: 'default export' });
|
|
126
|
+
} else {
|
|
127
|
+
loaded.push({ module: spec, configured: 'on import' });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const config = configSummary(t);
|
|
131
|
+
|
|
132
|
+
const warnings = [];
|
|
133
|
+
if (loaded.length === 0) {
|
|
134
|
+
const candidates = findSetupCandidates(projectDir);
|
|
135
|
+
warnings.push(
|
|
136
|
+
candidates.length > 0
|
|
137
|
+
? `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>.`
|
|
138
|
+
: "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.",
|
|
139
|
+
);
|
|
140
|
+
} else if (!configChanged(before, config)) {
|
|
141
|
+
warnings.push(
|
|
142
|
+
`The --setup modules did not change the configuration of the Tasty copy this tool uses (${relative(projectDir, PACKAGE_ROOT) || '.'}). They may configure another copy of @tenphi/tasty, or call configure() only inside a function.`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let result;
|
|
147
|
+
try {
|
|
148
|
+
result = renderCss(t, styles);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.warn = warn;
|
|
151
|
+
fail(
|
|
152
|
+
`Tasty could not render these styles: ${error instanceof Error ? error.message : error}`,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
warnings.push(...findIssues(t, styles, result.css));
|
|
156
|
+
console.warn = warn;
|
|
157
|
+
warnings.push(
|
|
158
|
+
...new Set(tastyWarnings.map((w) => w.trim().replace(/\s*\n\s*/g, ' '))),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
if (options.json) {
|
|
162
|
+
console.log(
|
|
163
|
+
JSON.stringify(
|
|
164
|
+
{
|
|
165
|
+
tasty: { version, path: relative(projectDir, PACKAGE_ROOT) || '.' },
|
|
166
|
+
setup: loaded,
|
|
167
|
+
config,
|
|
168
|
+
classNames: result.classNames,
|
|
169
|
+
css: result.css,
|
|
170
|
+
warnings,
|
|
171
|
+
},
|
|
172
|
+
null,
|
|
173
|
+
2,
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
} else {
|
|
177
|
+
console.log(render());
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function render() {
|
|
181
|
+
const out = [];
|
|
182
|
+
const setup =
|
|
183
|
+
loaded.length > 0
|
|
184
|
+
? `setup ${loaded.map((l) => l.module).join(', ')}`
|
|
185
|
+
: 'no setup, Tasty defaults';
|
|
186
|
+
out.push(`Tasty ${version}, ${setup}`);
|
|
187
|
+
const parts = [`units ${describeUnits(config.units)}`];
|
|
188
|
+
if (config.states.length > 0) parts.push(`states ${list(config.states)}`);
|
|
189
|
+
if (config.recipes.length > 0) parts.push(`recipes ${list(config.recipes)}`);
|
|
190
|
+
out.push(`Config: ${parts.join('; ')}`);
|
|
191
|
+
if (result.classNames.length > 0) {
|
|
192
|
+
out.push(
|
|
193
|
+
`Classes: ${result.classNames.join(' ')} (one per style chunk, doubled in selectors as at runtime)`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
out.push('', result.css ? formatCss(result.css) : '(no CSS)');
|
|
197
|
+
if (warnings.length > 0) {
|
|
198
|
+
out.push('', 'Warnings:', ...warnings.map((w) => `- ${w}`));
|
|
199
|
+
}
|
|
200
|
+
return out.join('\n');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function list(names) {
|
|
204
|
+
return names.length <= 8
|
|
205
|
+
? names.join(', ')
|
|
206
|
+
: `${names.slice(0, 8).join(', ')} and ${names.length - 8} more`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** A file path, or a path inside a package installed in the project. */
|
|
210
|
+
function resolveSetup(spec) {
|
|
211
|
+
if (
|
|
212
|
+
/(^|\/)tasty\.config\.[cm]?[jt]s$|(^|\/)tasty\.config\.json$/.test(spec)
|
|
213
|
+
) {
|
|
214
|
+
fail(
|
|
215
|
+
`${spec} only lists names for the ESLint plugin and the editor. Pass the module that calls configure(), or one that default-exports its config.`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const asPath = resolve(spec);
|
|
219
|
+
if (spec.startsWith('.') || isAbsolute(spec) || existsSync(asPath)) {
|
|
220
|
+
if (!existsSync(asPath)) fail(`cannot find --setup ${spec}`);
|
|
221
|
+
return asPath;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
return createRequire(join(projectDir, 'package.json')).resolve(spec);
|
|
225
|
+
} catch {
|
|
226
|
+
// A path the package's exports do not list, such as a file in its dist/.
|
|
227
|
+
}
|
|
228
|
+
for (let dir = projectDir; ; dir = dirname(dir)) {
|
|
229
|
+
const candidate = join(dir, 'node_modules', spec);
|
|
230
|
+
if (existsSync(candidate)) return candidate;
|
|
231
|
+
if (dirname(dir) === dir) break;
|
|
232
|
+
}
|
|
233
|
+
return fail(`cannot resolve --setup ${spec} from ${projectDir}`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function importHint(file, error) {
|
|
237
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
238
|
+
const code = error && typeof error === 'object' ? error.code : undefined;
|
|
239
|
+
if (code === 'ERR_UNKNOWN_FILE_EXTENSION') {
|
|
240
|
+
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.`;
|
|
241
|
+
}
|
|
242
|
+
if (code === 'ERR_MODULE_NOT_FOUND' && /\.[cm]?tsx?$/.test(file)) {
|
|
243
|
+
return `${message}. Node needs full file extensions in relative imports, and does not read tsconfig paths.`;
|
|
244
|
+
}
|
|
245
|
+
return message;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Up to five project files that import Tasty and configure it. */
|
|
249
|
+
function findSetupCandidates(root) {
|
|
250
|
+
const SKIP = new Set([
|
|
251
|
+
'node_modules',
|
|
252
|
+
'dist',
|
|
253
|
+
'build',
|
|
254
|
+
'out',
|
|
255
|
+
'coverage',
|
|
256
|
+
'storybook-static',
|
|
257
|
+
]);
|
|
258
|
+
const found = [];
|
|
259
|
+
let budget = 5000;
|
|
260
|
+
const walk = (dir, depth) => {
|
|
261
|
+
if (depth > 6 || found.length >= 5) return;
|
|
262
|
+
let entries;
|
|
263
|
+
try {
|
|
264
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
265
|
+
} catch {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
for (const entry of entries) {
|
|
269
|
+
if (found.length >= 5 || --budget < 0) return;
|
|
270
|
+
const path = join(dir, entry.name);
|
|
271
|
+
if (entry.isDirectory()) {
|
|
272
|
+
if (!entry.name.startsWith('.') && !SKIP.has(entry.name)) {
|
|
273
|
+
walk(path, depth + 1);
|
|
274
|
+
}
|
|
275
|
+
} else if (
|
|
276
|
+
/\.[cm]?[jt]sx?$/.test(entry.name) &&
|
|
277
|
+
!/\.(test|spec|stories|d)\.[cm]?[jt]sx?$/.test(entry.name) &&
|
|
278
|
+
!/^tasty\.config\./.test(entry.name)
|
|
279
|
+
) {
|
|
280
|
+
try {
|
|
281
|
+
if (statSync(path).size > 256 * 1024) continue;
|
|
282
|
+
const text = readFileSync(path, 'utf8');
|
|
283
|
+
if (
|
|
284
|
+
/['"]@tenphi\/tasty(\/[\w/-]*)?['"]/.test(text) &&
|
|
285
|
+
/\b(configure|setGlobalPredefinedStates)\s*\(/.test(text)
|
|
286
|
+
) {
|
|
287
|
+
found.push(relative(root, path));
|
|
288
|
+
} else if (/^tasty[\w.-]*\.config\.[cm]?[jt]s$/.test(entry.name)) {
|
|
289
|
+
found.push(relative(root, path));
|
|
290
|
+
}
|
|
291
|
+
} catch {
|
|
292
|
+
// Unreadable files are not candidates.
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
walk(root, 0);
|
|
298
|
+
return found;
|
|
299
|
+
}
|
|
@@ -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": "3.
|
|
3
|
+
"version": "3.19.0",
|
|
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": [
|
|
@@ -174,7 +175,8 @@
|
|
|
174
175
|
"@eslint/js": "^10.0.1",
|
|
175
176
|
"@size-limit/esbuild": "^14.1.0",
|
|
176
177
|
"@size-limit/file": "^14.1.0",
|
|
177
|
-
"@tenphi/eslint-plugin-tasty": "^1.
|
|
178
|
+
"@tenphi/eslint-plugin-tasty": "^1.17.0",
|
|
179
|
+
"@tenphi/harnesspm": "^0.1.0",
|
|
178
180
|
"@testing-library/jest-dom": "^7.0.1",
|
|
179
181
|
"@testing-library/react": "^16.3.3",
|
|
180
182
|
"@types/node": "^24.19.1",
|
|
@@ -299,10 +301,10 @@
|
|
|
299
301
|
"test:watch": "vitest",
|
|
300
302
|
"test:coverage": "vitest run --coverage",
|
|
301
303
|
"typecheck": "tsc --noEmit",
|
|
302
|
-
"lint": "eslint src",
|
|
303
|
-
"lint:fix": "eslint src --fix",
|
|
304
|
-
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
|
|
305
|
-
"format:check": "prettier --check \"src/**/*.{ts,tsx}\"",
|
|
304
|
+
"lint": "eslint src harness",
|
|
305
|
+
"lint:fix": "eslint src harness --fix",
|
|
306
|
+
"format": "prettier --write \"src/**/*.{ts,tsx}\" \"harness/**/*.mjs\"",
|
|
307
|
+
"format:check": "prettier --check \"src/**/*.{ts,tsx}\" \"harness/**/*.mjs\"",
|
|
306
308
|
"size": "size-limit",
|
|
307
309
|
"bench": "vitest bench --project \"node (bench)\"",
|
|
308
310
|
"bench:browser": "vitest bench --project \"browser (chromium) (bench)\"",
|
|
@@ -315,6 +317,7 @@
|
|
|
315
317
|
"version": "changeset version",
|
|
316
318
|
"release": "changeset publish",
|
|
317
319
|
"knip": "knip",
|
|
320
|
+
"check:harness": "harnesspm check",
|
|
318
321
|
"check:build": "node scripts/check-built-entries.mjs",
|
|
319
322
|
"check:react-compat": "node scripts/check-react-compat.mjs",
|
|
320
323
|
"check:babel-compat": "node scripts/check-babel-compat.mjs",
|