@tenphi/tasty 3.19.0 → 3.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -99,7 +99,16 @@ export function configSummary(t) {
99
99
  };
100
100
  }
101
101
 
102
- /** Whether two `configSummary()` results differ. */
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. */
103
112
  export function configChanged(before, after) {
104
113
  return JSON.stringify(before) !== JSON.stringify(after);
105
114
  }
@@ -2,12 +2,19 @@
2
2
  * tasty:styles — prints the CSS Tasty generates for a styles object, with the
3
3
  * app's configuration loaded first, and flags values that stayed unresolved.
4
4
  */
5
- import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
5
+ import {
6
+ existsSync,
7
+ readFileSync,
8
+ readdirSync,
9
+ realpathSync,
10
+ statSync,
11
+ } from 'node:fs';
6
12
  import { createRequire } from 'node:module';
7
13
  import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
8
14
  import { fileURLToPath, pathToFileURL } from 'node:url';
9
15
  import {
10
16
  configChanged,
17
+ configFingerprint,
11
18
  configSummary,
12
19
  describeUnits,
13
20
  findIssues,
@@ -30,12 +37,19 @@ selector or an at-rule. To see what a state key becomes, put it in a state map.
30
37
  for several modules. TypeScript needs Node 22.18 or later.
31
38
  --json print the result as JSON
32
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
+
33
44
  Without --setup, the CSS uses Tasty's defaults (x = 8px, r = 6px, ...) and none
34
45
  of the app's units, presets, recipes or state aliases.`;
35
46
 
36
- const PACKAGE_ROOT = fileURLToPath(new URL('../../../', import.meta.url));
47
+ const PACKAGE_ROOT = realpathSync(
48
+ fileURLToPath(new URL('../../../', import.meta.url)),
49
+ );
37
50
  const BUILD = join(PACKAGE_ROOT, 'dist/core/index.js');
38
- const projectDir = process.env.HARNESSPM_PROJECT_DIR || process.cwd();
51
+ const cwd = process.cwd();
52
+ const projectDir = process.env.HARNESSPM_PROJECT_DIR || cwd;
39
53
 
40
54
  function fail(message) {
41
55
  console.error(`tasty: ${message}`);
@@ -96,15 +110,22 @@ const tastyWarnings = [];
96
110
  const warn = console.warn;
97
111
  console.warn = (...args) => tastyWarnings.push(args.map(String).join(' '));
98
112
 
99
- const t = await import(pathToFileURL(BUILD).href);
100
- const version = JSON.parse(
101
- readFileSync(join(PACKAGE_ROOT, 'package.json'), 'utf8'),
102
- ).version;
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);
103
120
 
104
- const before = configSummary(t);
105
121
  const loaded = [];
106
122
  for (const spec of options.setup) {
107
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
+ );
108
129
  let mod;
109
130
  try {
110
131
  mod = await import(pathToFileURL(file).href);
@@ -113,33 +134,74 @@ for (const spec of options.setup) {
113
134
  fail(`cannot import --setup ${spec}: ${importHint(file, error)}`);
114
135
  }
115
136
  const config = mod.default;
116
- if (config && Object.getPrototypeOf(config) === Object.prototype) {
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;
117
144
  try {
118
- t.configure(config);
145
+ target.t.configure(config);
119
146
  } catch (error) {
120
147
  console.warn = warn;
121
148
  fail(
122
149
  `configure() rejected the default export of ${spec}: ${error instanceof Error ? error.message : error}`,
123
150
  );
124
151
  }
125
- loaded.push({ module: spec, configured: 'default export' });
152
+ changed = [target];
126
153
  } else {
127
- loaded.push({ module: spec, configured: 'on import' });
154
+ changed = [...copies.values()].filter((c) =>
155
+ configChanged(before.get(c.root), configFingerprint(c.t)),
156
+ );
128
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);
129
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;
130
181
  const config = configSummary(t);
131
182
 
132
183
  const warnings = [];
133
184
  if (loaded.length === 0) {
134
- const candidates = findSetupCandidates(projectDir);
185
+ const candidates = findSetupCandidates(
186
+ cwd.startsWith(projectDir) ? cwd : projectDir,
187
+ );
135
188
  warnings.push(
136
189
  candidates.length > 0
137
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>.`
138
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.",
139
192
  );
140
- } else if (!configChanged(before, config)) {
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
+ );
141
203
  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.`,
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.`,
143
205
  );
144
206
  }
145
207
 
@@ -162,7 +224,7 @@ if (options.json) {
162
224
  console.log(
163
225
  JSON.stringify(
164
226
  {
165
- tasty: { version, path: relative(projectDir, PACKAGE_ROOT) || '.' },
227
+ tasty: { version, path: show(chosen.root) },
166
228
  setup: loaded,
167
229
  config,
168
230
  classNames: result.classNames,
@@ -183,7 +245,8 @@ function render() {
183
245
  loaded.length > 0
184
246
  ? `setup ${loaded.map((l) => l.module).join(', ')}`
185
247
  : 'no setup, Tasty defaults';
186
- out.push(`Tasty ${version}, ${setup}`);
248
+ const from = chosen === own ? '' : ` (${show(chosen.root)})`;
249
+ out.push(`Tasty ${version}${from}, ${setup}`);
187
250
  const parts = [`units ${describeUnits(config.units)}`];
188
251
  if (config.states.length > 0) parts.push(`states ${list(config.states)}`);
189
252
  if (config.recipes.length > 0) parts.push(`recipes ${list(config.recipes)}`);
@@ -220,17 +283,64 @@ function resolveSetup(spec) {
220
283
  if (!existsSync(asPath)) fail(`cannot find --setup ${spec}`);
221
284
  return asPath;
222
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;
223
306
  try {
224
- return createRequire(join(projectDir, 'package.json')).resolve(spec);
307
+ entry = createRequire(join(dir, 'noop.js')).resolve('@tenphi/tasty');
225
308
  } catch {
226
- // A path the package's exports do not list, such as a file in its dist/.
309
+ return undefined;
227
310
  }
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;
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
+ }
232
319
  }
233
- return fail(`cannot resolve --setup ${spec} from ${projectDir}`);
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) || '.';
234
344
  }
235
345
 
236
346
  function importHint(file, error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenphi/tasty",
3
- "version": "3.19.0",
3
+ "version": "3.19.1",
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",