@dmisdm/pandatail 0.2.0 → 0.4.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 +90 -83
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +610 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +320 -233
- package/dist/preset.d.ts +36 -13
- package/dist/project.d.ts +74 -0
- package/dist/scanCss.d.ts +30 -0
- package/dist/vite.d.ts +19 -0
- package/dist/vite.js +579 -0
- package/package.json +33 -14
- package/LICENSE +0 -21
- package/dist/extractSync.d.ts +0 -19
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type NativeCompiler } from "@pandacss/compiler";
|
|
2
|
+
import { formatDiagnostic, type Diagnostic } from "@pandacss/compiler-shared";
|
|
3
|
+
type AnyConfig = Record<string, any>;
|
|
4
|
+
export interface PandatailOptions {
|
|
5
|
+
/** Tailwind v4 entry stylesheet (`@import "tailwindcss"`, `@theme`, …). */
|
|
6
|
+
css: string;
|
|
7
|
+
/** Project root. Defaults to `process.cwd()`. */
|
|
8
|
+
cwd?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Optional escape hatch for everything CSS can't express (recipes, patterns,
|
|
11
|
+
* textStyles, custom utilities, …): a path to a `panda.config.ts`, or an
|
|
12
|
+
* inline config object. Merged on top of what pandatail derives from the CSS.
|
|
13
|
+
*/
|
|
14
|
+
config?: string | AnyConfig;
|
|
15
|
+
/** Source globs for Panda to scan. Replaces the default and the config's `include`. */
|
|
16
|
+
include?: string[];
|
|
17
|
+
/** Source globs to skip. */
|
|
18
|
+
exclude?: string[];
|
|
19
|
+
/** Where generated code goes. Defaults to the config's `outdir`, else `styled-system`. */
|
|
20
|
+
outdir?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Panda `jsxFramework` (default `"react"`). Required for JSX style props and
|
|
23
|
+
* the generated `styled-system/jsx` factory. `false` disables JSX codegen.
|
|
24
|
+
*/
|
|
25
|
+
jsxFramework?: "react" | "solid" | "preact" | "vue" | "qwik" | false;
|
|
26
|
+
/** Allow style props on JSX components, e.g. `<Box bg="brand.500" p="4" />`. Panda defaults to true. */
|
|
27
|
+
jsxStyleProps?: "all" | "minimal" | "none";
|
|
28
|
+
/** Emit Panda's CSS reset. Default false (Tailwind ships preflight). */
|
|
29
|
+
preflight?: boolean;
|
|
30
|
+
/** Panda `importMap`; where generated modules are imported from. */
|
|
31
|
+
importMap?: string | Record<string, string>;
|
|
32
|
+
/** Class name prefix. */
|
|
33
|
+
prefix?: string;
|
|
34
|
+
/** Hash class names and CSS variables. */
|
|
35
|
+
hash?: boolean | {
|
|
36
|
+
cssVar?: boolean;
|
|
37
|
+
className?: boolean;
|
|
38
|
+
};
|
|
39
|
+
/** Minify the generated stylesheet. */
|
|
40
|
+
minify?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface PandatailProject {
|
|
43
|
+
compiler: NativeCompiler;
|
|
44
|
+
/** The fully merged config the compiler was built from. */
|
|
45
|
+
config: AnyConfig;
|
|
46
|
+
cwd: string;
|
|
47
|
+
/** Absolute. */
|
|
48
|
+
outdir: string;
|
|
49
|
+
/** Files whose change means the project must be recreated (CSS + config). */
|
|
50
|
+
dependencies: string[];
|
|
51
|
+
/** Non-fatal things pandatail could not translate from the CSS. */
|
|
52
|
+
warnings: string[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build a Panda compiler in memory from a Tailwind stylesheet, with no
|
|
56
|
+
* `panda.config.ts` required.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createPandatailProject(opts: PandatailOptions): Promise<PandatailProject>;
|
|
59
|
+
export interface BuildResult {
|
|
60
|
+
files: string[];
|
|
61
|
+
artifacts: number;
|
|
62
|
+
/** Absolute path of the written stylesheet, if CSS was written. */
|
|
63
|
+
cssPath?: string;
|
|
64
|
+
diagnostics: Diagnostic[];
|
|
65
|
+
}
|
|
66
|
+
export interface BuildOptions {
|
|
67
|
+
/** Write generated code (`styled-system/`). Default true. */
|
|
68
|
+
codegen?: boolean;
|
|
69
|
+
/** Write the stylesheet here (relative to project cwd). `false` to skip. Default `<outdir>/styles.css`. */
|
|
70
|
+
outfile?: string | false;
|
|
71
|
+
}
|
|
72
|
+
/** Scan + parse sources, then write the artifacts and/or stylesheet. */
|
|
73
|
+
export declare function buildProject(project: PandatailProject, opts?: BuildOptions): BuildResult;
|
|
74
|
+
export { formatDiagnostic };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a Tailwind entry stylesheet says about the project, beyond `@theme`:
|
|
3
|
+
* where to look for class usage (`@source`) and the custom variants
|
|
4
|
+
* (`@custom-variant`) that Panda should expose as conditions.
|
|
5
|
+
*/
|
|
6
|
+
export interface CssScan {
|
|
7
|
+
/** Absolute paths / globs from `@source "…"`. */
|
|
8
|
+
sources: string[];
|
|
9
|
+
/** Absolute paths / globs from `@source not "…"`. */
|
|
10
|
+
excludes: string[];
|
|
11
|
+
/** `@custom-variant` rules, mapped to Panda condition values. */
|
|
12
|
+
conditions: Record<string, string>;
|
|
13
|
+
/** Things we saw but could not translate. */
|
|
14
|
+
warnings: string[];
|
|
15
|
+
/** Local files that were read (entry + relative `@import`s), for watching. */
|
|
16
|
+
files: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface ScanCssOptions {
|
|
19
|
+
css: string;
|
|
20
|
+
/** Directory relative paths resolve against. */
|
|
21
|
+
base: string;
|
|
22
|
+
/** Path of the entry file, if there is one. */
|
|
23
|
+
file?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Walk the entry CSS and any *relative* `@import`s (bare specifiers such as
|
|
27
|
+
* `tailwindcss` are skipped: they carry no project-specific `@source`s or
|
|
28
|
+
* variants).
|
|
29
|
+
*/
|
|
30
|
+
export declare function scanCss(opts: ScanCssOptions): CssScan;
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
import { type PandatailOptions } from "./project";
|
|
3
|
+
export interface PandatailViteOptions extends Omit<PandatailOptions, "css"> {
|
|
4
|
+
/** Tailwind v4 entry stylesheet. Resolved against Vite's `root` unless `cwd` is set. */
|
|
5
|
+
css: string;
|
|
6
|
+
/** Stylesheet to write, relative to the root. Default `<outdir>/styles.css`. */
|
|
7
|
+
outfile?: string;
|
|
8
|
+
/** Log what was generated. Default true in dev, false during `vite build`. */
|
|
9
|
+
verbose?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Vite plugin: generates `styled-system/` and the Panda stylesheet before the
|
|
13
|
+
* module graph is built, and keeps both up to date while the dev server runs.
|
|
14
|
+
*
|
|
15
|
+
* Panda config is passed as plugin options (`jsxStyleProps`, `outdir`, …);
|
|
16
|
+
* `config` accepts anything else, as an object or a `panda.config.ts` path.
|
|
17
|
+
*/
|
|
18
|
+
export declare function pandatail(options: PandatailViteOptions): Plugin;
|
|
19
|
+
export default pandatail;
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
3
|
+
import { relative as relative2, resolve as resolve5, sep } from "node:path";
|
|
4
|
+
|
|
5
|
+
// src/project.ts
|
|
6
|
+
import { bundleConfig, createConfigSnapshot, mergeConfigs } from "@pandacss/config";
|
|
7
|
+
import { createCompilerFromSnapshot } from "@pandacss/compiler";
|
|
8
|
+
import { applyConfigDefaults, formatDiagnostic } from "@pandacss/compiler-shared";
|
|
9
|
+
import presetBase from "@pandacss/preset-base";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import { existsSync as existsSync2, statSync } from "node:fs";
|
|
12
|
+
import { relative, resolve as resolve4 } from "node:path";
|
|
13
|
+
|
|
14
|
+
// src/preset.ts
|
|
15
|
+
import { definePreset } from "@pandacss/dev";
|
|
16
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
17
|
+
import { dirname as dirname3, resolve as resolve3 } from "node:path";
|
|
18
|
+
|
|
19
|
+
// src/extract.ts
|
|
20
|
+
import { __unstable__loadDesignSystem } from "@tailwindcss/node";
|
|
21
|
+
import { readFile } from "node:fs/promises";
|
|
22
|
+
import { dirname, resolve } from "node:path";
|
|
23
|
+
|
|
24
|
+
// src/namespaces.ts
|
|
25
|
+
var TAILWIND_NAMESPACES = [
|
|
26
|
+
{ prefix: "font-weight", category: "fontWeights" },
|
|
27
|
+
{ prefix: "inset-shadow", category: "shadows" },
|
|
28
|
+
{ prefix: "drop-shadow", category: "dropShadows" },
|
|
29
|
+
{ prefix: "text-shadow", category: "shadows" },
|
|
30
|
+
{ prefix: "color", category: "colors" },
|
|
31
|
+
{ prefix: "spacing", category: "spacing" },
|
|
32
|
+
{ prefix: "font", category: "fonts" },
|
|
33
|
+
{ prefix: "text", category: "fontSizes" },
|
|
34
|
+
{ prefix: "tracking", category: "letterSpacings" },
|
|
35
|
+
{ prefix: "leading", category: "lineHeights" },
|
|
36
|
+
{ prefix: "breakpoint", category: "breakpoints" },
|
|
37
|
+
{ prefix: "container", category: "sizes" },
|
|
38
|
+
{ prefix: "radius", category: "radii" },
|
|
39
|
+
{ prefix: "shadow", category: "shadows" },
|
|
40
|
+
{ prefix: "blur", category: "blurs" },
|
|
41
|
+
{ prefix: "aspect", category: "aspectRatios" },
|
|
42
|
+
{ prefix: "ease", category: "easings" },
|
|
43
|
+
{ prefix: "animate", category: "animations" },
|
|
44
|
+
{ prefix: "perspective", category: "sizes" }
|
|
45
|
+
];
|
|
46
|
+
function deriveTokenPath(rest) {
|
|
47
|
+
const lastDash = rest.lastIndexOf("-");
|
|
48
|
+
if (lastDash > 0) {
|
|
49
|
+
const after = rest.slice(lastDash + 1);
|
|
50
|
+
if (/^\d+$/.test(after)) {
|
|
51
|
+
return [rest.slice(0, lastDash), after];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return [rest];
|
|
55
|
+
}
|
|
56
|
+
function matchNamespace(themeKey) {
|
|
57
|
+
if (!themeKey.startsWith("--"))
|
|
58
|
+
return null;
|
|
59
|
+
const stripped = themeKey.slice(2);
|
|
60
|
+
if (stripped.includes("--"))
|
|
61
|
+
return null;
|
|
62
|
+
for (const mapping of TAILWIND_NAMESPACES) {
|
|
63
|
+
if (stripped === mapping.prefix) {
|
|
64
|
+
return { mapping, rest: "DEFAULT" };
|
|
65
|
+
}
|
|
66
|
+
const withDash = `${mapping.prefix}-`;
|
|
67
|
+
if (stripped.startsWith(withDash)) {
|
|
68
|
+
return { mapping, rest: stripped.slice(withDash.length) };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/derivedScales.ts
|
|
75
|
+
var TAILWIND_NUMERIC_STEPS = [
|
|
76
|
+
0,
|
|
77
|
+
0.5,
|
|
78
|
+
1,
|
|
79
|
+
1.5,
|
|
80
|
+
2,
|
|
81
|
+
2.5,
|
|
82
|
+
3,
|
|
83
|
+
3.5,
|
|
84
|
+
4,
|
|
85
|
+
5,
|
|
86
|
+
6,
|
|
87
|
+
7,
|
|
88
|
+
8,
|
|
89
|
+
9,
|
|
90
|
+
10,
|
|
91
|
+
11,
|
|
92
|
+
12,
|
|
93
|
+
14,
|
|
94
|
+
16,
|
|
95
|
+
20,
|
|
96
|
+
24,
|
|
97
|
+
28,
|
|
98
|
+
32,
|
|
99
|
+
36,
|
|
100
|
+
40,
|
|
101
|
+
44,
|
|
102
|
+
48,
|
|
103
|
+
52,
|
|
104
|
+
56,
|
|
105
|
+
60,
|
|
106
|
+
64,
|
|
107
|
+
72,
|
|
108
|
+
80,
|
|
109
|
+
96
|
|
110
|
+
];
|
|
111
|
+
var NAMED_SIZES = {
|
|
112
|
+
full: "100%",
|
|
113
|
+
screen: "100vw",
|
|
114
|
+
"screen-h": "100vh",
|
|
115
|
+
min: "min-content",
|
|
116
|
+
max: "max-content",
|
|
117
|
+
fit: "fit-content",
|
|
118
|
+
auto: "auto"
|
|
119
|
+
};
|
|
120
|
+
var NAMED_RADII = {
|
|
121
|
+
none: "0",
|
|
122
|
+
full: "9999px"
|
|
123
|
+
};
|
|
124
|
+
function synthesiseDerivedTokens(hasSpacingBase) {
|
|
125
|
+
const out = [];
|
|
126
|
+
if (hasSpacingBase) {
|
|
127
|
+
pushNumericScale(out);
|
|
128
|
+
}
|
|
129
|
+
pushNamed(out);
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
function pushNumericScale(out) {
|
|
133
|
+
for (const step of TAILWIND_NUMERIC_STEPS) {
|
|
134
|
+
const name = String(step);
|
|
135
|
+
const value = `calc(var(--spacing) * ${name})`;
|
|
136
|
+
const cssVar = `--spacing-${name.replace(".", "_")}`;
|
|
137
|
+
out.push({ cssVar, value, category: "spacing", path: [name] });
|
|
138
|
+
out.push({ cssVar, value, category: "sizes", path: [name] });
|
|
139
|
+
out.push({ cssVar, value, category: "lineHeights", path: [name] });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function pushNamed(out) {
|
|
143
|
+
for (const [name, value] of Object.entries(NAMED_SIZES)) {
|
|
144
|
+
out.push({
|
|
145
|
+
cssVar: `--sizes-${name.replace(".", "_")}`,
|
|
146
|
+
value,
|
|
147
|
+
category: "sizes",
|
|
148
|
+
path: [name]
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
for (const [name, value] of Object.entries(NAMED_RADII)) {
|
|
152
|
+
out.push({
|
|
153
|
+
cssVar: `--radii-${name}`,
|
|
154
|
+
value,
|
|
155
|
+
category: "radii",
|
|
156
|
+
path: [name]
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/extract.ts
|
|
162
|
+
async function extractTokens(opts) {
|
|
163
|
+
const base = resolve(opts.base ?? process.cwd());
|
|
164
|
+
const ds = await __unstable__loadDesignSystem(opts.css, { base });
|
|
165
|
+
const themeEntries = new Map;
|
|
166
|
+
for (const [key, entry] of ds.theme.entries())
|
|
167
|
+
themeEntries.set(key, entry.value);
|
|
168
|
+
const out = [];
|
|
169
|
+
for (const [key, rawValue] of themeEntries) {
|
|
170
|
+
const matched = matchNamespace(key);
|
|
171
|
+
if (!matched)
|
|
172
|
+
continue;
|
|
173
|
+
if (matched.rest === "DEFAULT")
|
|
174
|
+
continue;
|
|
175
|
+
out.push({
|
|
176
|
+
cssVar: key,
|
|
177
|
+
value: matched.mapping.category === "breakpoints" ? rawValue : `var(${key})`,
|
|
178
|
+
category: matched.mapping.category,
|
|
179
|
+
path: deriveTokenPath(matched.rest)
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
out.push(...synthesiseDerivedTokens(themeEntries.has("--spacing")));
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
async function extractTokensFromFile(path) {
|
|
186
|
+
const css = await readFile(path, "utf8");
|
|
187
|
+
return extractTokens({ css, base: dirname(resolve(path)) });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/scanCss.ts
|
|
191
|
+
import postcss from "postcss";
|
|
192
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
193
|
+
import { dirname as dirname2, resolve as resolve2 } from "node:path";
|
|
194
|
+
function scanCss(opts) {
|
|
195
|
+
const out = {
|
|
196
|
+
sources: [],
|
|
197
|
+
excludes: [],
|
|
198
|
+
conditions: {},
|
|
199
|
+
warnings: [],
|
|
200
|
+
files: opts.file ? [opts.file] : []
|
|
201
|
+
};
|
|
202
|
+
walk(opts.css, opts.base, out, new Set(opts.file ? [opts.file] : []));
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
function walk(css, base, out, seen) {
|
|
206
|
+
let root;
|
|
207
|
+
try {
|
|
208
|
+
root = postcss.parse(css);
|
|
209
|
+
} catch (e) {
|
|
210
|
+
out.warnings.push(`could not parse CSS in ${base}: ${e.message}`);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
root.walkAtRules((rule) => {
|
|
214
|
+
switch (rule.name) {
|
|
215
|
+
case "import": {
|
|
216
|
+
const id = quoted(rule.params) ?? urlTarget(rule.params);
|
|
217
|
+
if (!id || !id.startsWith("."))
|
|
218
|
+
return;
|
|
219
|
+
const path = resolve2(base, id);
|
|
220
|
+
if (seen.has(path) || !existsSync(path))
|
|
221
|
+
return;
|
|
222
|
+
seen.add(path);
|
|
223
|
+
out.files.push(path);
|
|
224
|
+
walk(readFileSync(path, "utf8"), dirname2(path), out, seen);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
case "source":
|
|
228
|
+
return handleSource(rule, base, out);
|
|
229
|
+
case "custom-variant":
|
|
230
|
+
return handleCustomVariant(rule, out);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
function handleSource(rule, base, out) {
|
|
235
|
+
const params = rule.params.trim();
|
|
236
|
+
if (params.startsWith("inline(") || params.startsWith("not inline(")) {
|
|
237
|
+
out.warnings.push(`@source ${params}: inline sources have no Panda equivalent`);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const isNot = /^not\s/.test(params);
|
|
241
|
+
const target = quoted(isNot ? params.replace(/^not\s+/, "") : params);
|
|
242
|
+
if (!target)
|
|
243
|
+
return;
|
|
244
|
+
(isNot ? out.excludes : out.sources).push(resolve2(base, target));
|
|
245
|
+
}
|
|
246
|
+
function handleCustomVariant(rule, out) {
|
|
247
|
+
const params = rule.params.trim();
|
|
248
|
+
const name = /^([\w-]+)/.exec(params)?.[1];
|
|
249
|
+
if (!name)
|
|
250
|
+
return;
|
|
251
|
+
if (!rule.nodes) {
|
|
252
|
+
const m = /^[\w-]+\s*\(([\s\S]+)\)$/.exec(params);
|
|
253
|
+
if (m)
|
|
254
|
+
out.conditions[name] = m[1].trim();
|
|
255
|
+
else
|
|
256
|
+
out.warnings.push(`@custom-variant ${name}: unrecognised form`);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const body = rule.nodes.filter((n) => n.type !== "comment");
|
|
260
|
+
const only = body.length === 1 ? body[0] : undefined;
|
|
261
|
+
if (only?.type === "atrule" && only.name !== "slot" && isSlotOnly(only)) {
|
|
262
|
+
out.conditions[name] = `@${only.name} ${only.params}`.trim();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (only?.type === "rule" && isSlotOnly(only)) {
|
|
266
|
+
out.conditions[name] = only.selector;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
out.warnings.push(`@custom-variant ${name}: block form is too complex to map to a Panda condition`);
|
|
270
|
+
}
|
|
271
|
+
function isSlotOnly(node) {
|
|
272
|
+
const kids = (node.nodes ?? []).filter((n) => n.type !== "comment");
|
|
273
|
+
return kids.length === 1 && kids[0].type === "atrule" && kids[0].name === "slot";
|
|
274
|
+
}
|
|
275
|
+
function quoted(params) {
|
|
276
|
+
return /^\s*["']([^"']+)["']/.exec(params)?.[1] ?? null;
|
|
277
|
+
}
|
|
278
|
+
function urlTarget(params) {
|
|
279
|
+
return /^\s*url\(\s*["']?([^"')]+)["']?\s*\)/.exec(params)?.[1] ?? null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/preset.ts
|
|
283
|
+
async function loadTailwind(opts) {
|
|
284
|
+
let css;
|
|
285
|
+
let base;
|
|
286
|
+
let file;
|
|
287
|
+
if (opts.css != null) {
|
|
288
|
+
css = opts.css;
|
|
289
|
+
base = opts.base ?? process.cwd();
|
|
290
|
+
} else if (opts.cssPath != null) {
|
|
291
|
+
file = resolve3(opts.cssPath);
|
|
292
|
+
css = await readFile2(file, "utf8");
|
|
293
|
+
base = dirname3(file);
|
|
294
|
+
} else {
|
|
295
|
+
throw new Error("pandatail: provide either `css` or `cssPath`");
|
|
296
|
+
}
|
|
297
|
+
const tokens = await extractTokens({ css, base });
|
|
298
|
+
const scan = scanCss({ css, base, file });
|
|
299
|
+
return { preset: buildPreset(tokens, scan), scan, tokens };
|
|
300
|
+
}
|
|
301
|
+
async function tailwindPreset(opts) {
|
|
302
|
+
return (await loadTailwind(opts)).preset;
|
|
303
|
+
}
|
|
304
|
+
function buildPreset(tokens, scan) {
|
|
305
|
+
const breakpoints = {};
|
|
306
|
+
const tokenTokens = [];
|
|
307
|
+
for (const t of tokens) {
|
|
308
|
+
if (t.category === "breakpoints") {
|
|
309
|
+
breakpoints[t.path.join("-")] = t.value;
|
|
310
|
+
} else {
|
|
311
|
+
tokenTokens.push(t);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const tokensByCategory = groupByCategory(tokenTokens);
|
|
315
|
+
const theme = {};
|
|
316
|
+
for (const [category, list] of Object.entries(tokensByCategory)) {
|
|
317
|
+
theme[category] = buildTree(list, category);
|
|
318
|
+
}
|
|
319
|
+
const preset = definePreset({
|
|
320
|
+
name: "tailwind",
|
|
321
|
+
theme: {
|
|
322
|
+
tokens: theme,
|
|
323
|
+
breakpoints
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
if (scan && Object.keys(scan.conditions).length > 0) {
|
|
327
|
+
preset.conditions = { extend: scan.conditions };
|
|
328
|
+
}
|
|
329
|
+
return preset;
|
|
330
|
+
}
|
|
331
|
+
function groupByCategory(tokens) {
|
|
332
|
+
const out = {};
|
|
333
|
+
for (const t of tokens) {
|
|
334
|
+
(out[t.category] ??= []).push(t);
|
|
335
|
+
}
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
function buildTree(tokens, category) {
|
|
339
|
+
const root = {};
|
|
340
|
+
for (const t of tokens) {
|
|
341
|
+
let cursor = root;
|
|
342
|
+
for (let i = 0;i < t.path.length - 1; i++) {
|
|
343
|
+
const seg = t.path[i];
|
|
344
|
+
const next = cursor[seg];
|
|
345
|
+
if (next && "value" in next) {
|
|
346
|
+
throw new Error(`pandatail: token path collision in '${category}' at ` + `'${t.path.slice(0, i + 1).join(".")}': ` + `'${t.path.join(".")}' nests under a token that is already a leaf.`);
|
|
347
|
+
}
|
|
348
|
+
if (!next) {
|
|
349
|
+
const fresh = {};
|
|
350
|
+
cursor[seg] = fresh;
|
|
351
|
+
cursor = fresh;
|
|
352
|
+
} else {
|
|
353
|
+
cursor = next;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const leaf = t.path[t.path.length - 1];
|
|
357
|
+
const existing = cursor[leaf];
|
|
358
|
+
if (existing && !("value" in existing)) {
|
|
359
|
+
throw new Error(`pandatail: token path collision in '${category}' at ` + `'${t.path.join(".")}': a leaf token clashes with an existing group.`);
|
|
360
|
+
}
|
|
361
|
+
cursor[leaf] = { value: t.value };
|
|
362
|
+
}
|
|
363
|
+
return root;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/project.ts
|
|
367
|
+
var DEFAULT_EXT = "{js,jsx,ts,tsx,mdx}";
|
|
368
|
+
async function createPandatailProject(opts) {
|
|
369
|
+
const cwd = resolve4(opts.cwd ?? process.cwd());
|
|
370
|
+
const cssPath = resolve4(cwd, opts.css);
|
|
371
|
+
if (!existsSync2(cssPath))
|
|
372
|
+
throw new Error(`pandatail: CSS entry not found: ${cssPath}`);
|
|
373
|
+
const tw = await loadTailwind({ cssPath });
|
|
374
|
+
const dependencies = [...tw.scan.files];
|
|
375
|
+
let user = {};
|
|
376
|
+
if (typeof opts.config === "string") {
|
|
377
|
+
const file = resolve4(cwd, opts.config);
|
|
378
|
+
const bundled = await bundleConfig(file, cwd);
|
|
379
|
+
user = bundled.config;
|
|
380
|
+
dependencies.push(file, ...bundled.dependencies);
|
|
381
|
+
} else if (opts.config) {
|
|
382
|
+
user = opts.config;
|
|
383
|
+
}
|
|
384
|
+
const { presets: userPresetList, ...userRest } = user;
|
|
385
|
+
const userPresets = await resolvePresets(userPresetList, cwd);
|
|
386
|
+
const include = opts.include ?? unique([
|
|
387
|
+
...userRest.include ?? [`./src/**/*.${DEFAULT_EXT}`],
|
|
388
|
+
...tw.scan.sources.map((p) => sourceToGlob(p, cwd))
|
|
389
|
+
]);
|
|
390
|
+
const exclude = opts.exclude ?? unique([
|
|
391
|
+
...userRest.exclude ?? [],
|
|
392
|
+
...tw.scan.excludes.map((p) => sourceToGlob(p, cwd))
|
|
393
|
+
]);
|
|
394
|
+
const defaults = {
|
|
395
|
+
cwd,
|
|
396
|
+
preflight: false,
|
|
397
|
+
jsxFramework: "react",
|
|
398
|
+
jsxStyleProps: "all",
|
|
399
|
+
outdir: "styled-system"
|
|
400
|
+
};
|
|
401
|
+
const finalOverrides = { include, exclude, cwd };
|
|
402
|
+
for (const key of [
|
|
403
|
+
"outdir",
|
|
404
|
+
"jsxFramework",
|
|
405
|
+
"jsxStyleProps",
|
|
406
|
+
"preflight",
|
|
407
|
+
"importMap",
|
|
408
|
+
"prefix",
|
|
409
|
+
"hash",
|
|
410
|
+
"minify"
|
|
411
|
+
]) {
|
|
412
|
+
if (opts[key] !== undefined)
|
|
413
|
+
finalOverrides[key] = opts[key];
|
|
414
|
+
}
|
|
415
|
+
const merged = mergeConfigs([
|
|
416
|
+
defaults,
|
|
417
|
+
presetBase,
|
|
418
|
+
...userPresets,
|
|
419
|
+
tw.preset,
|
|
420
|
+
userRest,
|
|
421
|
+
finalOverrides
|
|
422
|
+
]);
|
|
423
|
+
const config = applyConfigDefaults(merged, cwd);
|
|
424
|
+
const compiler = createCompilerFromSnapshot(createConfigSnapshot(config));
|
|
425
|
+
return {
|
|
426
|
+
compiler,
|
|
427
|
+
config,
|
|
428
|
+
cwd,
|
|
429
|
+
outdir: resolve4(cwd, config.outdir ?? "styled-system"),
|
|
430
|
+
dependencies: unique(dependencies),
|
|
431
|
+
warnings: tw.scan.warnings
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
function buildProject(project, opts = {}) {
|
|
435
|
+
const { compiler, cwd, outdir } = project;
|
|
436
|
+
const files = compiler.scan();
|
|
437
|
+
compiler.parseFiles(files);
|
|
438
|
+
let artifacts = 0;
|
|
439
|
+
if (opts.codegen !== false) {
|
|
440
|
+
artifacts = compiler.writeArtifacts({ outdir, cwd }).length;
|
|
441
|
+
}
|
|
442
|
+
let cssPath;
|
|
443
|
+
if (opts.outfile !== false) {
|
|
444
|
+
const outfile = opts.outfile ?? relative(cwd, resolve4(outdir, "styles.css"));
|
|
445
|
+
cssPath = compiler.writeCss({ outfile, cwd }).path;
|
|
446
|
+
}
|
|
447
|
+
return { files, artifacts, cssPath, diagnostics: compiler.diagnostics() };
|
|
448
|
+
}
|
|
449
|
+
function sourceToGlob(path, cwd) {
|
|
450
|
+
const rel = relative(cwd, path).split("\\").join("/");
|
|
451
|
+
const p = rel.startsWith(".") ? rel : `./${rel}`;
|
|
452
|
+
const hasGlob = /[*?{}\[\]]/.test(p);
|
|
453
|
+
if (!hasGlob && existsSync2(path) && statSync(path).isDirectory()) {
|
|
454
|
+
return `${p}/**/*.${DEFAULT_EXT}`;
|
|
455
|
+
}
|
|
456
|
+
return p;
|
|
457
|
+
}
|
|
458
|
+
async function resolvePresets(list, cwd) {
|
|
459
|
+
const out = [];
|
|
460
|
+
for (const entry of list ?? []) {
|
|
461
|
+
const preset = await loadPreset(entry, cwd);
|
|
462
|
+
const { presets, ...rest } = preset;
|
|
463
|
+
out.push(...await resolvePresets(presets, cwd), rest);
|
|
464
|
+
}
|
|
465
|
+
return out;
|
|
466
|
+
}
|
|
467
|
+
async function loadPreset(entry, cwd) {
|
|
468
|
+
const value = await entry;
|
|
469
|
+
if (typeof value !== "string")
|
|
470
|
+
return value;
|
|
471
|
+
const req = createRequire(resolve4(cwd, "__pandatail_anchor__"));
|
|
472
|
+
let mod;
|
|
473
|
+
try {
|
|
474
|
+
mod = await import(req.resolve(value));
|
|
475
|
+
} catch {
|
|
476
|
+
mod = await import(value);
|
|
477
|
+
}
|
|
478
|
+
return mod.default ?? mod.preset ?? mod;
|
|
479
|
+
}
|
|
480
|
+
function unique(xs) {
|
|
481
|
+
return [...new Set(xs)];
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// src/vite.ts
|
|
485
|
+
function pandatail(options) {
|
|
486
|
+
const { outfile, verbose, ...projectOptions } = options;
|
|
487
|
+
let root = process.cwd();
|
|
488
|
+
let isServe = false;
|
|
489
|
+
let project;
|
|
490
|
+
const log = (msg) => {
|
|
491
|
+
if (verbose ?? isServe)
|
|
492
|
+
console.log(`[pandatail] ${msg}`);
|
|
493
|
+
};
|
|
494
|
+
const load = async () => {
|
|
495
|
+
const p = await createPandatailProject({ cwd: root, ...projectOptions });
|
|
496
|
+
for (const w of p.warnings)
|
|
497
|
+
console.warn(`[pandatail] warning: ${w}`);
|
|
498
|
+
project = p;
|
|
499
|
+
return p;
|
|
500
|
+
};
|
|
501
|
+
const run = (p, codegen) => {
|
|
502
|
+
const r = buildProject(p, { codegen, outfile: outfile ?? undefined });
|
|
503
|
+
for (const d of r.diagnostics)
|
|
504
|
+
console.warn(formatDiagnostic(d));
|
|
505
|
+
const rel = (f) => relative2(p.cwd, f) || f;
|
|
506
|
+
log([
|
|
507
|
+
`${r.files.length} source file(s)`,
|
|
508
|
+
codegen ? `${r.artifacts} generated file(s) → ${rel(p.outdir)}/` : "",
|
|
509
|
+
r.cssPath ? `css → ${rel(r.cssPath)}` : ""
|
|
510
|
+
].filter(Boolean).join(", "));
|
|
511
|
+
};
|
|
512
|
+
return {
|
|
513
|
+
name: "pandatail",
|
|
514
|
+
enforce: "pre",
|
|
515
|
+
configResolved(config) {
|
|
516
|
+
root = config.root;
|
|
517
|
+
isServe = config.command === "serve";
|
|
518
|
+
},
|
|
519
|
+
async buildStart() {
|
|
520
|
+
const p = await load();
|
|
521
|
+
run(p, true);
|
|
522
|
+
for (const dep of p.dependencies)
|
|
523
|
+
this.addWatchFile(dep);
|
|
524
|
+
},
|
|
525
|
+
configureServer(server) {
|
|
526
|
+
const pending = new Set;
|
|
527
|
+
let timer;
|
|
528
|
+
let chain = Promise.resolve();
|
|
529
|
+
const flush = async () => {
|
|
530
|
+
const changed = [...pending];
|
|
531
|
+
pending.clear();
|
|
532
|
+
const p = project;
|
|
533
|
+
if (!p || changed.length === 0)
|
|
534
|
+
return;
|
|
535
|
+
try {
|
|
536
|
+
if (changed.some((f) => p.dependencies.includes(f))) {
|
|
537
|
+
const next = await load();
|
|
538
|
+
server.watcher.add(next.dependencies);
|
|
539
|
+
run(next, true);
|
|
540
|
+
server.ws.send({ type: "full-reload" });
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
for (const f of changed) {
|
|
544
|
+
if (existsSync3(f))
|
|
545
|
+
p.compiler.refreshFile(f);
|
|
546
|
+
else
|
|
547
|
+
p.compiler.removeFile(f);
|
|
548
|
+
}
|
|
549
|
+
run(p, false);
|
|
550
|
+
} catch (e) {
|
|
551
|
+
server.config.logger.error(`[pandatail] ${e.message}`);
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
const onFsEvent = (file) => {
|
|
555
|
+
const p = project;
|
|
556
|
+
if (!p)
|
|
557
|
+
return;
|
|
558
|
+
const abs = resolve5(file);
|
|
559
|
+
if (abs.startsWith(p.outdir + sep) || abs.split(sep).includes("node_modules"))
|
|
560
|
+
return;
|
|
561
|
+
if (!p.dependencies.includes(abs) && !p.compiler.isSourceFile(abs))
|
|
562
|
+
return;
|
|
563
|
+
pending.add(abs);
|
|
564
|
+
clearTimeout(timer);
|
|
565
|
+
timer = setTimeout(() => {
|
|
566
|
+
chain = chain.then(flush);
|
|
567
|
+
}, 40);
|
|
568
|
+
};
|
|
569
|
+
server.watcher.on("change", onFsEvent);
|
|
570
|
+
server.watcher.on("add", onFsEvent);
|
|
571
|
+
server.watcher.on("unlink", onFsEvent);
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
var vite_default = pandatail;
|
|
576
|
+
export {
|
|
577
|
+
vite_default as default,
|
|
578
|
+
pandatail
|
|
579
|
+
};
|