@dmisdm/pandatail 0.3.0 → 0.5.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 CHANGED
@@ -27,9 +27,13 @@ Panda v2 (ESM, Node >= 22) and Tailwind v4.
27
27
 
28
28
  ```sh
29
29
  npm i -D @dmisdm/pandatail @pandacss/dev @pandacss/compiler @pandacss/config \
30
- @pandacss/preset-base @pandacss/compiler-shared tailwindcss
30
+ @pandacss/preset-base @pandacss/compiler-shared tailwindcss @tailwindcss/node
31
31
  ```
32
32
 
33
+ Keep `@tailwindcss/node` on the same version as `tailwindcss` and your Tailwind
34
+ build plugin (such as `@tailwindcss/vite`). Pandatail uses Tailwind's Node adapter to resolve CSS imports and
35
+ load plugins, then reads the resolved theme before Tailwind emits CSS.
36
+
33
37
  ## CLI
34
38
 
35
39
  ```sh
@@ -37,6 +41,7 @@ pandatail build --css src/app.css # codegen + stylesheet (default)
37
41
  pandatail codegen --css src/app.css # styled-system/ only
38
42
  pandatail cssgen --css src/app.css -o out.css # stylesheet only
39
43
  pandatail watch --css src/app.css # rebuild on change
44
+ pandatail studio --css src/app.css # live Panda Studio (Panda >= 2.4)
40
45
  ```
41
46
 
42
47
  | Option | Meaning |
@@ -50,6 +55,18 @@ pandatail watch --css src/app.css # rebuild on change
50
55
 
51
56
  Import the generated `styled-system/styles.css` after Tailwind's CSS.
52
57
 
58
+ ### Panda Studio
59
+
60
+ `pandatail studio --css src/app.css` opens [Panda Studio](https://panda-css.com/docs/theming/studio)
61
+ with your Tailwind-derived tokens and live usage of Panda styles. It uses Panda's local Studio server
62
+ and accepts `--cwd`, `--include`, `--outdir`, and optional `--config`. It doesn't create a project
63
+ `panda.config.ts` or write build artifacts. Use `--no-open` to print the link, or `--port 3001` to
64
+ choose a port (defaults to `6420`). The link is for local development, not a public URL.
65
+
66
+ Studio previews resolve simple aliases from `@theme` and unconditional `:root` declarations in local
67
+ stylesheets. They don't run your app's CSS cascade, so conditional overrides and unresolved CSS
68
+ variables may not render. Normal builds still reference Tailwind's variables.
69
+
53
70
  ## Vite plugin
54
71
 
55
72
  Runs codegen and the stylesheet before your modules build, then watches CSS, config and source files during `vite dev`.
@@ -75,14 +92,14 @@ Options: everything `createPandatailProject` takes (`css`, `config`, `include`,
75
92
 
76
93
  ## What maps, and what doesn't
77
94
 
78
- | Tailwind | Panda |
79
- | --- | --- |
80
- | `@theme` | `theme.tokens` + `breakpoints`, as `var(--…)` references |
81
- | `@source "x"` / `@source not "x"` | `include` / `exclude` |
82
- | `@custom-variant n (sel);` | `conditions.extend.n` |
83
- | `@custom-variant n { &:hover, &:focus { @slot } }` | same (single selector or at-rule wrapping one `@slot`) |
84
- | `@plugin` | loaded for token extraction; plugin-defined utilities are not Panda utilities |
85
- | `@source inline(...)`, complex `@custom-variant` blocks, `@utility` | warned about, not mapped |
95
+ | Tailwind | Panda |
96
+ | ------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
97
+ | `@theme` | `theme.tokens` + `breakpoints`, as `var(--…)` references |
98
+ | `@source "x"` / `@source not "x"` | `include` / `exclude` |
99
+ | `@custom-variant n (sel);` | `conditions.extend.n` |
100
+ | `@custom-variant n { &:hover, &:focus { @slot } }` | same (single selector or at-rule wrapping one `@slot`) |
101
+ | `@plugin` | loaded for token extraction; plugin-defined utilities are not Panda utilities |
102
+ | `@source inline(...)`, complex `@custom-variant` blocks, `@utility` | warned about, not mapped |
86
103
 
87
104
  Anything CSS can't express (text styles, recipes, patterns, utilities) goes in an optional
88
105
  `panda.config.ts` passed with `--config`. It is merged after Tailwind's tokens, so it wins.
@@ -106,6 +123,10 @@ Prefer your own `panda.config.ts`? `tailwindPreset({ cssPath })` is async and re
106
123
  - `@theme` changes need a rebuild; `watch` reloads the whole project when the CSS or config changes.
107
124
  - Panda's cascade layers (`reset, base, tokens, recipes, utilities`) are separate from Tailwind's; check ordering if both emit a `utilities` layer.
108
125
 
126
+ ## Changes in 0.4.0
127
+
128
+ Tailwind's Node adapter now resolves stylesheet imports and loads plugins and configs. This supports CSS-only package exports such as `tw-animate-css`. Add the `@tailwindcss/node` peer dependency at the same version as your Tailwind installation.
129
+
109
130
  ## Changes in 0.3.0
110
131
 
111
132
  Breaking: sync API (`tailwindPresetSync`) and the demo site removed; Panda peers are `^2.0.0`.
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { existsSync as existsSync3, watch } from "node:fs";
5
- import { relative as relative2, resolve as resolve5, sep } from "node:path";
5
+ import { relative as relative2, resolve as resolve6, sep } from "node:path";
6
6
  import { parseArgs } from "node:util";
7
7
 
8
8
  // src/project.ts
@@ -10,7 +10,7 @@ import { bundleConfig, createConfigSnapshot, mergeConfigs } from "@pandacss/conf
10
10
  import { createCompilerFromSnapshot } from "@pandacss/compiler";
11
11
  import { applyConfigDefaults, formatDiagnostic } from "@pandacss/compiler-shared";
12
12
  import presetBase from "@pandacss/preset-base";
13
- import { createRequire as createRequire2 } from "node:module";
13
+ import { createRequire } from "node:module";
14
14
  import { existsSync as existsSync2, statSync } from "node:fs";
15
15
  import { relative, resolve as resolve4 } from "node:path";
16
16
 
@@ -20,11 +20,9 @@ import { readFile as readFile2 } from "node:fs/promises";
20
20
  import { dirname as dirname3, resolve as resolve3 } from "node:path";
21
21
 
22
22
  // src/extract.ts
23
- import { __unstable__loadDesignSystem } from "tailwindcss";
23
+ import { __unstable__loadDesignSystem } from "@tailwindcss/node";
24
24
  import { readFile } from "node:fs/promises";
25
- import { createRequire } from "node:module";
26
25
  import { dirname, resolve } from "node:path";
27
- import { pathToFileURL } from "node:url";
28
26
 
29
27
  // src/namespaces.ts
30
28
  var TAILWIND_NAMESPACES = [
@@ -164,37 +162,18 @@ function pushNamed(out) {
164
162
  }
165
163
 
166
164
  // src/extract.ts
167
- var requireFromHere = createRequire(`${process.cwd()}/__pandatail_anchor__`);
168
- function resolveStylesheet(id, basedir) {
169
- if (id.startsWith(".") || id.startsWith("/")) {
170
- return resolve(basedir, id);
171
- }
172
- const candidates = [`${id}/index.css`, `${id}.css`, id];
173
- for (const candidate of candidates) {
174
- try {
175
- return requireFromHere.resolve(candidate, { paths: [basedir] });
176
- } catch {}
177
- }
178
- throw new Error(`pandatail: cannot resolve stylesheet '${id}' from ${basedir}`);
179
- }
180
165
  async function extractTokens(opts) {
181
- const base = opts.base ?? process.cwd();
182
- const ds = await __unstable__loadDesignSystem(opts.css, {
183
- base,
184
- loadStylesheet: async (id, basedir) => {
185
- const path = resolveStylesheet(id, basedir);
186
- const content = await readFile(path, "utf8");
187
- return { path, base: dirname(path), content };
188
- },
189
- loadModule: async (id, basedir) => {
190
- const path = resolveStylesheet(id, basedir);
191
- const mod = await import(pathToFileURL(path).href);
192
- return { path, base: dirname(path), module: mod.default ?? mod };
193
- }
194
- });
166
+ return tokensFromTheme(await loadThemeEntries(opts));
167
+ }
168
+ async function loadThemeEntries(opts) {
169
+ const base = resolve(opts.base ?? process.cwd());
170
+ const ds = await __unstable__loadDesignSystem(opts.css, { base });
195
171
  const themeEntries = new Map;
196
172
  for (const [key, entry] of ds.theme.entries())
197
173
  themeEntries.set(key, entry.value);
174
+ return themeEntries;
175
+ }
176
+ function tokensFromTheme(themeEntries) {
198
177
  const out = [];
199
178
  for (const [key, rawValue] of themeEntries) {
200
179
  const matched = matchNamespace(key);
@@ -221,7 +200,7 @@ async function extractTokensFromFile(path) {
221
200
  import postcss from "postcss";
222
201
  import { existsSync, readFileSync } from "node:fs";
223
202
  import { dirname as dirname2, resolve as resolve2 } from "node:path";
224
- function scanCss(opts) {
203
+ function scanCss(opts, rootVariables) {
225
204
  const out = {
226
205
  sources: [],
227
206
  excludes: [],
@@ -229,10 +208,10 @@ function scanCss(opts) {
229
208
  warnings: [],
230
209
  files: opts.file ? [opts.file] : []
231
210
  };
232
- walk(opts.css, opts.base, out, new Set(opts.file ? [opts.file] : []));
211
+ walk(opts.css, opts.base, out, new Set(opts.file ? [opts.file] : []), rootVariables);
233
212
  return out;
234
213
  }
235
- function walk(css, base, out, seen) {
214
+ function walk(css, base, out, seen, rootVariables) {
236
215
  let root;
237
216
  try {
238
217
  root = postcss.parse(css);
@@ -240,7 +219,11 @@ function walk(css, base, out, seen) {
240
219
  out.warnings.push(`could not parse CSS in ${base}: ${e.message}`);
241
220
  return;
242
221
  }
243
- root.walkAtRules((rule) => {
222
+ root.walk((rule) => {
223
+ if (rule.type === "rule" && rootVariables)
224
+ collectRootVariables(rule, rootVariables);
225
+ if (rule.type !== "atrule")
226
+ return;
244
227
  switch (rule.name) {
245
228
  case "import": {
246
229
  const id = quoted(rule.params) ?? urlTarget(rule.params);
@@ -251,7 +234,7 @@ function walk(css, base, out, seen) {
251
234
  return;
252
235
  seen.add(path);
253
236
  out.files.push(path);
254
- walk(readFileSync(path, "utf8"), dirname2(path), out, seen);
237
+ walk(readFileSync(path, "utf8"), dirname2(path), out, seen, rootVariables);
255
238
  return;
256
239
  }
257
240
  case "source":
@@ -261,6 +244,17 @@ function walk(css, base, out, seen) {
261
244
  }
262
245
  });
263
246
  }
247
+ function collectRootVariables(rule, variables) {
248
+ let parent = rule.parent;
249
+ while (parent?.type === "atrule" && parent.name === "layer")
250
+ parent = parent.parent;
251
+ if (parent?.type !== "root" || !rule.selectors.includes(":root"))
252
+ return;
253
+ for (const node of rule.nodes) {
254
+ if (node.type === "decl" && node.prop.startsWith("--"))
255
+ variables.set(node.prop, node.value);
256
+ }
257
+ }
264
258
  function handleSource(rule, base, out) {
265
259
  const params = rule.params.trim();
266
260
  if (params.startsWith("inline(") || params.startsWith("not inline(")) {
@@ -310,7 +304,7 @@ function urlTarget(params) {
310
304
  }
311
305
 
312
306
  // src/preset.ts
313
- async function loadTailwind(opts) {
307
+ async function loadTailwind(opts, preview = false) {
314
308
  let css;
315
309
  let base;
316
310
  let file;
@@ -324,13 +318,32 @@ async function loadTailwind(opts) {
324
318
  } else {
325
319
  throw new Error("pandatail: provide either `css` or `cssPath`");
326
320
  }
327
- const tokens = await extractTokens({ css, base });
328
- const scan = scanCss({ css, base, file });
329
- return { preset: buildPreset(tokens, scan), scan, tokens };
321
+ const theme = await loadThemeEntries({ css, base });
322
+ const tokens = tokensFromTheme(theme);
323
+ const variables = preview ? new Map(theme) : undefined;
324
+ const scan = scanCss({ css, base, file }, variables);
325
+ const presetTokens = variables ? tokens.map((token) => ({
326
+ ...token,
327
+ value: resolvePreviewValue(token.value, variables)
328
+ })) : tokens;
329
+ return { preset: buildPreset(presetTokens, scan), scan, tokens };
330
330
  }
331
331
  async function tailwindPreset(opts) {
332
332
  return (await loadTailwind(opts)).preset;
333
333
  }
334
+ function resolvePreviewValue(value, variables, resolving = new Set) {
335
+ return value.replace(/"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|(?<![\w-])var\(\s*(--[\w-]+)\s*\)/gi, (match, name) => {
336
+ if (name === undefined || resolving.has(name))
337
+ return match;
338
+ const definition = variables.get(name);
339
+ if (definition === undefined)
340
+ return match;
341
+ resolving.add(name);
342
+ const resolved = resolvePreviewValue(definition, variables, resolving);
343
+ resolving.delete(name);
344
+ return resolved;
345
+ });
346
+ }
334
347
  function buildPreset(tokens, scan) {
335
348
  const breakpoints = {};
336
349
  const tokenTokens = [];
@@ -396,11 +409,16 @@ function buildTree(tokens, category) {
396
409
  // src/project.ts
397
410
  var DEFAULT_EXT = "{js,jsx,ts,tsx,mdx}";
398
411
  async function createPandatailProject(opts) {
412
+ const project = await resolvePandatailConfig(opts);
413
+ const compiler = createCompilerFromSnapshot(createConfigSnapshot(project.config));
414
+ return { compiler, ...project };
415
+ }
416
+ async function resolvePandatailConfig(opts, preview = false) {
399
417
  const cwd = resolve4(opts.cwd ?? process.cwd());
400
418
  const cssPath = resolve4(cwd, opts.css);
401
419
  if (!existsSync2(cssPath))
402
420
  throw new Error(`pandatail: CSS entry not found: ${cssPath}`);
403
- const tw = await loadTailwind({ cssPath });
421
+ const tw = await loadTailwind({ cssPath }, preview);
404
422
  const dependencies = [...tw.scan.files];
405
423
  let user = {};
406
424
  if (typeof opts.config === "string") {
@@ -451,9 +469,7 @@ async function createPandatailProject(opts) {
451
469
  finalOverrides
452
470
  ]);
453
471
  const config = applyConfigDefaults(merged, cwd);
454
- const compiler = createCompilerFromSnapshot(createConfigSnapshot(config));
455
472
  return {
456
- compiler,
457
473
  config,
458
474
  cwd,
459
475
  outdir: resolve4(cwd, config.outdir ?? "styled-system"),
@@ -498,7 +514,7 @@ async function loadPreset(entry, cwd) {
498
514
  const value = await entry;
499
515
  if (typeof value !== "string")
500
516
  return value;
501
- const req = createRequire2(resolve4(cwd, "__pandatail_anchor__"));
517
+ const req = createRequire(resolve4(cwd, "__pandatail_anchor__"));
502
518
  let mod;
503
519
  try {
504
520
  mod = await import(req.resolve(value));
@@ -511,6 +527,59 @@ function unique(xs) {
511
527
  return [...new Set(xs)];
512
528
  }
513
529
 
530
+ // src/studio.ts
531
+ import { spawn } from "node:child_process";
532
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
533
+ import { createRequire as createRequire2 } from "node:module";
534
+ import { constants, tmpdir } from "node:os";
535
+ import { dirname as dirname4, join, resolve as resolve5 } from "node:path";
536
+ async function studioConfig(options) {
537
+ const { config, dependencies, warnings } = await resolvePandatailConfig(options, true);
538
+ for (const warning of warnings)
539
+ console.warn(`pandatail: warning: ${warning}`);
540
+ return { ...config, dependencies: [...config.dependencies ?? [], ...dependencies] };
541
+ }
542
+ async function runPandatailStudio(options, { port, noOpen }) {
543
+ const require2 = createRequire2(import.meta.url);
544
+ const manifestPath = require2.resolve("@pandacss/dev/package.json");
545
+ const { bin, version } = require2(manifestPath);
546
+ const [major, minor] = version.split(".");
547
+ if (major !== "2" || !(Number(minor) >= 4)) {
548
+ throw new Error(`studio requires Panda CSS 2.4 or newer (found ${version})`);
549
+ }
550
+ const cwd = resolve5(options.cwd ?? process.cwd());
551
+ const dir = mkdtempSync(join(tmpdir(), "pandatail-studio-"));
552
+ try {
553
+ const config = join(dir, "panda.config.mjs");
554
+ const entry = new URL(import.meta.url.endsWith(".ts") ? "./studio.ts" : "./studio.js", import.meta.url);
555
+ writeFileSync(config, `
556
+ const { studioConfig } = await import(new URL(${JSON.stringify(entry.href)}).href);
557
+ export default await studioConfig(${JSON.stringify({ ...options, cwd })});
558
+ `);
559
+ const args = [resolve5(dirname4(manifestPath), bin.panda), "studio", "--cwd", cwd, "--config", config];
560
+ if (port !== undefined)
561
+ args.push(`--port=${port}`);
562
+ if (noOpen)
563
+ args.push("--no-open");
564
+ const child = spawn(process.execPath, args, { stdio: "inherit" });
565
+ const interrupt = () => child.kill("SIGINT");
566
+ const terminate = () => child.kill("SIGTERM");
567
+ process.on("SIGINT", interrupt);
568
+ process.on("SIGTERM", terminate);
569
+ try {
570
+ return await new Promise((done, reject) => {
571
+ child.once("error", reject);
572
+ child.once("exit", (code, signal) => done(code ?? (signal ? 128 + constants.signals[signal] : 1)));
573
+ });
574
+ } finally {
575
+ process.off("SIGINT", interrupt);
576
+ process.off("SIGTERM", terminate);
577
+ }
578
+ } finally {
579
+ rmSync(dir, { recursive: true, force: true });
580
+ }
581
+ }
582
+
514
583
  // src/cli.ts
515
584
  var HELP = `pandatail: run Panda from a Tailwind v4 stylesheet, no panda.config.ts needed
516
585
 
@@ -522,6 +591,7 @@ Commands
522
591
  codegen generate styled-system/ only
523
592
  cssgen write the stylesheet only
524
593
  watch build, then rebuild on change
594
+ studio open Panda Studio with live tokens and source usage (Panda >= 2.4)
525
595
 
526
596
  Options
527
597
  -c, --css <file> Tailwind entry stylesheet (required)
@@ -530,6 +600,8 @@ Options
530
600
  --outdir <dir> generated code directory (default: styled-system)
531
601
  -i, --include <glob> source glob to scan; repeatable
532
602
  --cwd <dir> project root (default: current directory)
603
+ --port <port> Studio port (default: 6420)
604
+ --no-open print the Studio link without opening a browser
533
605
  -h, --help
534
606
  `;
535
607
  async function main() {
@@ -542,11 +614,13 @@ async function main() {
542
614
  outdir: { type: "string" },
543
615
  include: { type: "string", short: "i", multiple: true },
544
616
  cwd: { type: "string" },
617
+ port: { type: "string" },
618
+ "no-open": { type: "boolean" },
545
619
  help: { type: "boolean", short: "h" }
546
620
  }
547
621
  });
548
622
  const command = positionals[0] ?? "build";
549
- if (values.help || !["build", "codegen", "cssgen", "watch"].includes(command)) {
623
+ if (values.help || !["build", "codegen", "cssgen", "watch", "studio"].includes(command)) {
550
624
  console.log(HELP);
551
625
  process.exit(values.help ? 0 : 1);
552
626
  }
@@ -563,6 +637,13 @@ async function main() {
563
637
  outdir: values.outdir,
564
638
  include: values.include
565
639
  };
640
+ if (command === "studio") {
641
+ process.exitCode = await runPandatailStudio(options, {
642
+ port: values.port,
643
+ noOpen: values["no-open"]
644
+ });
645
+ return;
646
+ }
566
647
  const buildOpts = {
567
648
  codegen: command !== "cssgen",
568
649
  outfile: command === "codegen" ? false : values.out
@@ -598,7 +679,7 @@ async function main() {
598
679
  watch(project.cwd, { recursive: true }, (_event, name) => {
599
680
  if (!name)
600
681
  return;
601
- const abs = resolve5(project.cwd, name.toString());
682
+ const abs = resolve6(project.cwd, name.toString());
602
683
  if (ignored(abs))
603
684
  return;
604
685
  const isDep = project.dependencies.includes(abs);
package/dist/extract.d.ts CHANGED
@@ -43,5 +43,7 @@ export interface ExtractOptions {
43
43
  * theme into a flat list of {@link ExtractedToken}s ready for Panda.
44
44
  */
45
45
  export declare function extractTokens(opts: ExtractOptions): Promise<ExtractedToken[]>;
46
+ export declare function loadThemeEntries(opts: ExtractOptions): Promise<Map<string, string>>;
47
+ export declare function tokensFromTheme(themeEntries: ReadonlyMap<string, string>): ExtractedToken[];
46
48
  /** Convenience: load CSS from a file path then extract. */
47
49
  export declare function extractTokensFromFile(path: string): Promise<ExtractedToken[]>;
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import { bundleConfig, createConfigSnapshot, mergeConfigs } from "@pandacss/conf
3
3
  import { createCompilerFromSnapshot } from "@pandacss/compiler";
4
4
  import { applyConfigDefaults, formatDiagnostic } from "@pandacss/compiler-shared";
5
5
  import presetBase from "@pandacss/preset-base";
6
- import { createRequire as createRequire2 } from "node:module";
6
+ import { createRequire } from "node:module";
7
7
  import { existsSync as existsSync2, statSync } from "node:fs";
8
8
  import { relative, resolve as resolve4 } from "node:path";
9
9
 
@@ -13,11 +13,9 @@ import { readFile as readFile2 } from "node:fs/promises";
13
13
  import { dirname as dirname3, resolve as resolve3 } from "node:path";
14
14
 
15
15
  // src/extract.ts
16
- import { __unstable__loadDesignSystem } from "tailwindcss";
16
+ import { __unstable__loadDesignSystem } from "@tailwindcss/node";
17
17
  import { readFile } from "node:fs/promises";
18
- import { createRequire } from "node:module";
19
18
  import { dirname, resolve } from "node:path";
20
- import { pathToFileURL } from "node:url";
21
19
 
22
20
  // src/namespaces.ts
23
21
  var TAILWIND_NAMESPACES = [
@@ -157,37 +155,18 @@ function pushNamed(out) {
157
155
  }
158
156
 
159
157
  // src/extract.ts
160
- var requireFromHere = createRequire(`${process.cwd()}/__pandatail_anchor__`);
161
- function resolveStylesheet(id, basedir) {
162
- if (id.startsWith(".") || id.startsWith("/")) {
163
- return resolve(basedir, id);
164
- }
165
- const candidates = [`${id}/index.css`, `${id}.css`, id];
166
- for (const candidate of candidates) {
167
- try {
168
- return requireFromHere.resolve(candidate, { paths: [basedir] });
169
- } catch {}
170
- }
171
- throw new Error(`pandatail: cannot resolve stylesheet '${id}' from ${basedir}`);
172
- }
173
158
  async function extractTokens(opts) {
174
- const base = opts.base ?? process.cwd();
175
- const ds = await __unstable__loadDesignSystem(opts.css, {
176
- base,
177
- loadStylesheet: async (id, basedir) => {
178
- const path = resolveStylesheet(id, basedir);
179
- const content = await readFile(path, "utf8");
180
- return { path, base: dirname(path), content };
181
- },
182
- loadModule: async (id, basedir) => {
183
- const path = resolveStylesheet(id, basedir);
184
- const mod = await import(pathToFileURL(path).href);
185
- return { path, base: dirname(path), module: mod.default ?? mod };
186
- }
187
- });
159
+ return tokensFromTheme(await loadThemeEntries(opts));
160
+ }
161
+ async function loadThemeEntries(opts) {
162
+ const base = resolve(opts.base ?? process.cwd());
163
+ const ds = await __unstable__loadDesignSystem(opts.css, { base });
188
164
  const themeEntries = new Map;
189
165
  for (const [key, entry] of ds.theme.entries())
190
166
  themeEntries.set(key, entry.value);
167
+ return themeEntries;
168
+ }
169
+ function tokensFromTheme(themeEntries) {
191
170
  const out = [];
192
171
  for (const [key, rawValue] of themeEntries) {
193
172
  const matched = matchNamespace(key);
@@ -214,7 +193,7 @@ async function extractTokensFromFile(path) {
214
193
  import postcss from "postcss";
215
194
  import { existsSync, readFileSync } from "node:fs";
216
195
  import { dirname as dirname2, resolve as resolve2 } from "node:path";
217
- function scanCss(opts) {
196
+ function scanCss(opts, rootVariables) {
218
197
  const out = {
219
198
  sources: [],
220
199
  excludes: [],
@@ -222,10 +201,10 @@ function scanCss(opts) {
222
201
  warnings: [],
223
202
  files: opts.file ? [opts.file] : []
224
203
  };
225
- walk(opts.css, opts.base, out, new Set(opts.file ? [opts.file] : []));
204
+ walk(opts.css, opts.base, out, new Set(opts.file ? [opts.file] : []), rootVariables);
226
205
  return out;
227
206
  }
228
- function walk(css, base, out, seen) {
207
+ function walk(css, base, out, seen, rootVariables) {
229
208
  let root;
230
209
  try {
231
210
  root = postcss.parse(css);
@@ -233,7 +212,11 @@ function walk(css, base, out, seen) {
233
212
  out.warnings.push(`could not parse CSS in ${base}: ${e.message}`);
234
213
  return;
235
214
  }
236
- root.walkAtRules((rule) => {
215
+ root.walk((rule) => {
216
+ if (rule.type === "rule" && rootVariables)
217
+ collectRootVariables(rule, rootVariables);
218
+ if (rule.type !== "atrule")
219
+ return;
237
220
  switch (rule.name) {
238
221
  case "import": {
239
222
  const id = quoted(rule.params) ?? urlTarget(rule.params);
@@ -244,7 +227,7 @@ function walk(css, base, out, seen) {
244
227
  return;
245
228
  seen.add(path);
246
229
  out.files.push(path);
247
- walk(readFileSync(path, "utf8"), dirname2(path), out, seen);
230
+ walk(readFileSync(path, "utf8"), dirname2(path), out, seen, rootVariables);
248
231
  return;
249
232
  }
250
233
  case "source":
@@ -254,6 +237,17 @@ function walk(css, base, out, seen) {
254
237
  }
255
238
  });
256
239
  }
240
+ function collectRootVariables(rule, variables) {
241
+ let parent = rule.parent;
242
+ while (parent?.type === "atrule" && parent.name === "layer")
243
+ parent = parent.parent;
244
+ if (parent?.type !== "root" || !rule.selectors.includes(":root"))
245
+ return;
246
+ for (const node of rule.nodes) {
247
+ if (node.type === "decl" && node.prop.startsWith("--"))
248
+ variables.set(node.prop, node.value);
249
+ }
250
+ }
257
251
  function handleSource(rule, base, out) {
258
252
  const params = rule.params.trim();
259
253
  if (params.startsWith("inline(") || params.startsWith("not inline(")) {
@@ -303,7 +297,7 @@ function urlTarget(params) {
303
297
  }
304
298
 
305
299
  // src/preset.ts
306
- async function loadTailwind(opts) {
300
+ async function loadTailwind(opts, preview = false) {
307
301
  let css;
308
302
  let base;
309
303
  let file;
@@ -317,13 +311,32 @@ async function loadTailwind(opts) {
317
311
  } else {
318
312
  throw new Error("pandatail: provide either `css` or `cssPath`");
319
313
  }
320
- const tokens = await extractTokens({ css, base });
321
- const scan = scanCss({ css, base, file });
322
- return { preset: buildPreset(tokens, scan), scan, tokens };
314
+ const theme = await loadThemeEntries({ css, base });
315
+ const tokens = tokensFromTheme(theme);
316
+ const variables = preview ? new Map(theme) : undefined;
317
+ const scan = scanCss({ css, base, file }, variables);
318
+ const presetTokens = variables ? tokens.map((token) => ({
319
+ ...token,
320
+ value: resolvePreviewValue(token.value, variables)
321
+ })) : tokens;
322
+ return { preset: buildPreset(presetTokens, scan), scan, tokens };
323
323
  }
324
324
  async function tailwindPreset(opts) {
325
325
  return (await loadTailwind(opts)).preset;
326
326
  }
327
+ function resolvePreviewValue(value, variables, resolving = new Set) {
328
+ return value.replace(/"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|(?<![\w-])var\(\s*(--[\w-]+)\s*\)/gi, (match, name) => {
329
+ if (name === undefined || resolving.has(name))
330
+ return match;
331
+ const definition = variables.get(name);
332
+ if (definition === undefined)
333
+ return match;
334
+ resolving.add(name);
335
+ const resolved = resolvePreviewValue(definition, variables, resolving);
336
+ resolving.delete(name);
337
+ return resolved;
338
+ });
339
+ }
327
340
  function buildPreset(tokens, scan) {
328
341
  const breakpoints = {};
329
342
  const tokenTokens = [];
@@ -389,11 +402,16 @@ function buildTree(tokens, category) {
389
402
  // src/project.ts
390
403
  var DEFAULT_EXT = "{js,jsx,ts,tsx,mdx}";
391
404
  async function createPandatailProject(opts) {
405
+ const project = await resolvePandatailConfig(opts);
406
+ const compiler = createCompilerFromSnapshot(createConfigSnapshot(project.config));
407
+ return { compiler, ...project };
408
+ }
409
+ async function resolvePandatailConfig(opts, preview = false) {
392
410
  const cwd = resolve4(opts.cwd ?? process.cwd());
393
411
  const cssPath = resolve4(cwd, opts.css);
394
412
  if (!existsSync2(cssPath))
395
413
  throw new Error(`pandatail: CSS entry not found: ${cssPath}`);
396
- const tw = await loadTailwind({ cssPath });
414
+ const tw = await loadTailwind({ cssPath }, preview);
397
415
  const dependencies = [...tw.scan.files];
398
416
  let user = {};
399
417
  if (typeof opts.config === "string") {
@@ -444,9 +462,7 @@ async function createPandatailProject(opts) {
444
462
  finalOverrides
445
463
  ]);
446
464
  const config = applyConfigDefaults(merged, cwd);
447
- const compiler = createCompilerFromSnapshot(createConfigSnapshot(config));
448
465
  return {
449
- compiler,
450
466
  config,
451
467
  cwd,
452
468
  outdir: resolve4(cwd, config.outdir ?? "styled-system"),
@@ -491,7 +507,7 @@ async function loadPreset(entry, cwd) {
491
507
  const value = await entry;
492
508
  if (typeof value !== "string")
493
509
  return value;
494
- const req = createRequire2(resolve4(cwd, "__pandatail_anchor__"));
510
+ const req = createRequire(resolve4(cwd, "__pandatail_anchor__"));
495
511
  let mod;
496
512
  try {
497
513
  mod = await import(req.resolve(value));
package/dist/preset.d.ts CHANGED
@@ -30,7 +30,7 @@ export interface LoadedTailwind {
30
30
  * `@import`, `@theme` and `@plugin` behave exactly as they do in Tailwind) and
31
31
  * project it into Panda.
32
32
  */
33
- export declare function loadTailwind(opts: TailwindPresetOptions): Promise<LoadedTailwind>;
33
+ export declare function loadTailwind(opts: TailwindPresetOptions, preview?: boolean): Promise<LoadedTailwind>;
34
34
  /**
35
35
  * Build a PandaCSS preset whose token layer references the CSS custom
36
36
  * properties that Tailwind v4 emits from the user's `@theme` blocks, and whose
package/dist/project.d.ts CHANGED
@@ -56,6 +56,13 @@ export interface PandatailProject {
56
56
  * `panda.config.ts` required.
57
57
  */
58
58
  export declare function createPandatailProject(opts: PandatailOptions): Promise<PandatailProject>;
59
+ export declare function resolvePandatailConfig(opts: PandatailOptions, preview?: boolean): Promise<{
60
+ config: AnyConfig;
61
+ cwd: string;
62
+ outdir: string;
63
+ dependencies: string[];
64
+ warnings: string[];
65
+ }>;
59
66
  export interface BuildResult {
60
67
  files: string[];
61
68
  artifacts: number;
package/dist/scanCss.d.ts CHANGED
@@ -27,4 +27,4 @@ export interface ScanCssOptions {
27
27
  * `tailwindcss` are skipped: they carry no project-specific `@source`s or
28
28
  * variants).
29
29
  */
30
- export declare function scanCss(opts: ScanCssOptions): CssScan;
30
+ export declare function scanCss(opts: ScanCssOptions, rootVariables?: Map<string, string>): CssScan;
@@ -0,0 +1,8 @@
1
+ import { type PandatailOptions } from "./project";
2
+ export declare function studioConfig(options: PandatailOptions): Promise<{
3
+ dependencies: any[];
4
+ }>;
5
+ export declare function runPandatailStudio(options: PandatailOptions, { port, noOpen }: {
6
+ port?: string;
7
+ noOpen?: boolean;
8
+ }): Promise<number>;