@pandacss/transformer 2.0.0-beta.9 → 2.0.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.
@@ -1,89 +1,240 @@
1
1
  // src/runtime/internal/cx.ts
2
2
  var bakedSeparator = typeof __PANDA_CX_SEPARATOR__ !== "undefined" && __PANDA_CX_SEPARATOR__ ? __PANDA_CX_SEPARATOR__ : "_";
3
+ var CHAR_OPEN_BRACKET = 91;
4
+ var CHAR_CLOSE_BRACKET = 93;
5
+ var CHAR_COLON = 58;
3
6
  function splitClassName(className) {
4
7
  const segments = [];
5
- let current = "";
8
+ let start = 0;
6
9
  let bracketDepth = 0;
7
10
  for (let i = 0; i < className.length; i++) {
8
- const ch = className[i];
9
- if (ch === "[") {
11
+ const code = className.charCodeAt(i);
12
+ if (code === CHAR_OPEN_BRACKET) {
10
13
  bracketDepth++;
11
- current += ch;
12
- } else if (ch === "]") {
14
+ } else if (code === CHAR_CLOSE_BRACKET) {
13
15
  bracketDepth--;
14
- current += ch;
15
- } else if (ch === ":" && bracketDepth === 0) {
16
- if (current) segments.push(current);
17
- current = "";
18
- } else {
19
- current += ch;
16
+ } else if (code === CHAR_COLON && bracketDepth === 0) {
17
+ if (i > start) segments.push(className.slice(start, i));
18
+ start = i + 1;
20
19
  }
21
20
  }
22
- if (current) segments.push(current);
21
+ if (start < className.length) segments.push(className.slice(start));
23
22
  return segments;
24
23
  }
25
24
  function getMergeKey(className, separator) {
26
- let cls = className;
27
- if (cls.endsWith("!")) {
28
- cls = cls.slice(0, -1);
25
+ let end = className.length;
26
+ if (end > 0 && className.charCodeAt(end - 1) === 33) {
27
+ end -= 1;
29
28
  }
30
- const segments = splitClassName(cls);
31
- if (segments.length === 0) return null;
32
- const last = segments[segments.length - 1];
33
- const sepIdx = last.indexOf(separator);
34
- if (sepIdx < 1) return null;
35
- const property = last.slice(0, sepIdx);
36
- if (segments.length === 1) {
37
- return property;
29
+ if (end === 0) return null;
30
+ let bracketDepth = 0;
31
+ let lastColon = -1;
32
+ for (let i = 0; i < end; i++) {
33
+ const code = className.charCodeAt(i);
34
+ if (code === CHAR_OPEN_BRACKET) {
35
+ bracketDepth++;
36
+ } else if (code === CHAR_CLOSE_BRACKET) {
37
+ bracketDepth--;
38
+ } else if (code === CHAR_COLON && bracketDepth === 0) {
39
+ lastColon = i;
40
+ }
38
41
  }
39
- const conditions = segments.slice(0, -1).join(":");
40
- return `${conditions}:${property}`;
42
+ const propStart = lastColon + 1;
43
+ const sepIdx = className.indexOf(separator, propStart);
44
+ if (sepIdx < propStart + 1 || sepIdx >= end) return null;
45
+ return className.slice(0, sepIdx);
46
+ }
47
+ var CACHE_LIMIT = 2048;
48
+ var CHAR_SPACE = 32;
49
+ var CHAR_BANG = 33;
50
+ var FIELDS = 4;
51
+ function createCache(limit = CACHE_LIMIT) {
52
+ let size = 0;
53
+ let current = /* @__PURE__ */ new Map();
54
+ let previous = /* @__PURE__ */ new Map();
55
+ const set = (key, value) => {
56
+ current.set(key, value);
57
+ if (++size > limit) {
58
+ size = 0;
59
+ previous = current;
60
+ current = /* @__PURE__ */ new Map();
61
+ }
62
+ };
63
+ return {
64
+ get(key) {
65
+ let value = current.get(key);
66
+ if (value === void 0 && (value = previous.get(key)) !== void 0) set(key, value);
67
+ return value;
68
+ },
69
+ set
70
+ };
41
71
  }
42
- function flattenParts(parts, out) {
43
- for (const part of parts) {
44
- if (!part) continue;
45
- if (Array.isArray(part)) {
46
- flattenParts(part, out);
72
+ function scanPart(part, separator, out, offset) {
73
+ let count = 0;
74
+ let clean = part.length > 0;
75
+ let start = 0;
76
+ for (let i = 0; i <= part.length; i++) {
77
+ const code = i === part.length ? CHAR_SPACE : part.charCodeAt(i);
78
+ if (code !== CHAR_SPACE && (code < 9 || code > 13)) continue;
79
+ if (code !== CHAR_SPACE || i === start) clean = false;
80
+ if (i === start) {
81
+ start = i + 1;
47
82
  continue;
48
83
  }
49
- if (part === "") continue;
50
- out.push(part);
84
+ const at = offset + count * FIELDS;
85
+ count++;
86
+ out[at] = start;
87
+ out[at + 1] = i;
88
+ out[at + 2] = -1;
89
+ let end = i;
90
+ if (part.charCodeAt(end - 1) === CHAR_BANG) end--;
91
+ let depth = 0;
92
+ let propStart = start;
93
+ for (let c = start; c < end; c++) {
94
+ const code2 = part.charCodeAt(c);
95
+ if (code2 === CHAR_OPEN_BRACKET) depth++;
96
+ else if (code2 === CHAR_CLOSE_BRACKET) depth--;
97
+ else if (code2 === CHAR_COLON && depth === 0) propStart = c + 1;
98
+ }
99
+ const sep = part.indexOf(separator, propStart);
100
+ if (sep > propStart && sep < end) {
101
+ let hash = 2166136261;
102
+ for (let c = start; c < sep; c++) hash = Math.imul(hash ^ part.charCodeAt(c), 16777619);
103
+ out[at + 2] = sep;
104
+ out[at + 3] = hash;
105
+ }
106
+ start = i + 1;
51
107
  }
108
+ return clean ? count : -count;
52
109
  }
53
- function mergeClassStrings(separator, classes) {
54
- const seen = /* @__PURE__ */ new Map();
55
- const order = [];
56
- let id = 0;
57
- for (const cls of classes) {
58
- for (const token of cls.split(" ")) {
59
- if (!token) continue;
60
- const key = getMergeKey(token, separator);
61
- if (key !== null) {
62
- if (!seen.has(key)) order.push(key);
63
- seen.set(key, token);
110
+ function createCx(options = {}) {
111
+ const separator = options.separator ?? bakedSeparator;
112
+ const scanned = createCache();
113
+ const seenOnce = createCache(512);
114
+ const parts = [];
115
+ let tokens = new Int32Array(64 * FIELDS);
116
+ let tokenPart = new Int32Array(64);
117
+ let winner = new Int32Array(64);
118
+ let tableSize = 128;
119
+ let table = new Int32Array(tableSize);
120
+ let stamps = new Int32Array(tableSize);
121
+ let stamp = 0;
122
+ function collect(value) {
123
+ if (!value) return;
124
+ if (typeof value === "string") parts.push(value);
125
+ else for (let i = 0; i < value.length; i++) collect(value[i]);
126
+ }
127
+ function reserve(tokenCount) {
128
+ if (tokenCount <= tokenPart.length) return;
129
+ let size = tokenPart.length;
130
+ while (size < tokenCount) size *= 2;
131
+ const grow = (from, length) => {
132
+ const next = new Int32Array(length);
133
+ next.set(from);
134
+ return next;
135
+ };
136
+ tokens = grow(tokens, size * FIELDS);
137
+ tokenPart = grow(tokenPart, size);
138
+ winner = grow(winner, size);
139
+ }
140
+ function sameKey(a, b) {
141
+ const aStart = tokens[a * FIELDS];
142
+ const bStart = tokens[b * FIELDS];
143
+ const length = tokens[a * FIELDS + 2] - aStart;
144
+ if (tokens[b * FIELDS + 2] - bStart !== length) return false;
145
+ const left = parts[tokenPart[a]];
146
+ const right = parts[tokenPart[b]];
147
+ for (let i = 0; i < length; i++) {
148
+ if (left.charCodeAt(aStart + i) !== right.charCodeAt(bStart + i)) return false;
149
+ }
150
+ return true;
151
+ }
152
+ function merge() {
153
+ let count = 0;
154
+ let clean = true;
155
+ for (let p = 0; p < parts.length; p++) {
156
+ const part = parts[p];
157
+ const cached = scanned.get(part);
158
+ let partCount;
159
+ if (cached !== void 0) {
160
+ partCount = cached[0];
161
+ reserve(count + Math.abs(partCount));
162
+ const base = count * FIELDS;
163
+ for (let i = 1; i < cached.length; i++) tokens[base + i - 1] = cached[i];
64
164
  } else {
65
- const uniqueKey = `__${id++}`;
66
- order.push(uniqueKey);
67
- seen.set(uniqueKey, token);
165
+ reserve(count + part.length);
166
+ partCount = scanPart(part, separator, tokens, count * FIELDS);
167
+ if (seenOnce.get(part) === void 0) seenOnce.set(part, true);
168
+ else {
169
+ const record = new Int32Array(1 + Math.abs(partCount) * FIELDS);
170
+ record[0] = partCount;
171
+ record.set(tokens.subarray(count * FIELDS, (count + Math.abs(partCount)) * FIELDS), 1);
172
+ scanned.set(part, record);
173
+ }
68
174
  }
175
+ if (partCount <= 0) clean = false;
176
+ const end = count + Math.abs(partCount);
177
+ for (; count < end; count++) tokenPart[count] = p;
69
178
  }
179
+ if (count * 2 > tableSize) {
180
+ while (count * 2 > tableSize) tableSize *= 2;
181
+ table = new Int32Array(tableSize);
182
+ stamps = new Int32Array(tableSize);
183
+ stamp = 0;
184
+ }
185
+ if (++stamp === 2147483647) {
186
+ stamp = 1;
187
+ stamps.fill(0);
188
+ }
189
+ const mask = tableSize - 1;
190
+ let replaced = false;
191
+ for (let t = 0; t < count; t++) {
192
+ winner[t] = t;
193
+ if (tokens[t * FIELDS + 2] < 0) continue;
194
+ const hash = tokens[t * FIELDS + 3];
195
+ let slot = hash & mask;
196
+ let owner = -1;
197
+ while (stamps[slot] === stamp) {
198
+ const candidate = table[slot];
199
+ if (tokens[candidate * FIELDS + 3] === hash && sameKey(candidate, t)) {
200
+ owner = candidate;
201
+ break;
202
+ }
203
+ slot = slot + 1 & mask;
204
+ }
205
+ if (owner === -1) {
206
+ stamps[slot] = stamp;
207
+ table[slot] = t;
208
+ } else {
209
+ winner[owner] = t;
210
+ winner[t] = -1;
211
+ replaced = true;
212
+ }
213
+ }
214
+ if (!replaced && clean) {
215
+ let joined = parts[0];
216
+ for (let p = 1; p < parts.length; p++) joined += " " + parts[p];
217
+ return joined;
218
+ }
219
+ let result = "";
220
+ for (let t = 0; t < count; t++) {
221
+ const w = winner[t];
222
+ if (w === -1) continue;
223
+ const piece = parts[tokenPart[w]].slice(tokens[w * FIELDS], tokens[w * FIELDS + 1]);
224
+ result = result ? result + " " + piece : piece;
225
+ }
226
+ return result;
70
227
  }
71
- let str = "";
72
- for (let i = 0; i < order.length; i++) {
73
- if (str) str += " ";
74
- str += seen.get(order[i]);
75
- }
76
- return str;
77
- }
78
- function createCx(options = {}) {
79
- const separator = options.separator ?? bakedSeparator;
80
- return function cx2(...parts) {
81
- const flat = [];
82
- flattenParts(parts, flat);
83
- return mergeClassStrings(separator, flat);
228
+ return function cx2(...args) {
229
+ parts.length = 0;
230
+ for (let i = 0; i < args.length; i++) collect(args[i]);
231
+ const count = parts.length;
232
+ if (count === 0) return "";
233
+ if (count === 1) return parts[0];
234
+ return merge();
84
235
  };
85
236
  }
86
- var cx = createCx();
237
+ var cx = /* @__PURE__ */ createCx();
87
238
  export {
88
239
  createCx,
89
240
  cx,
@@ -1,54 +1,54 @@
1
1
  export { CxOptions, CxSeparator, PandaClassPart, createCx, cx, getMergeKey, splitClassName } from './cx.js';
2
+ export { c as css } from '../css-BPeL3U6s.js';
2
3
 
3
- /** Transformed `css()` sites receive pre-encoded class strings only. */
4
- declare function css(...styles: string[]): string;
5
-
6
- type VariantMap = Record<string, Record<string, string>>;
4
+ type VariantValue = string | number | boolean;
7
5
  type CompoundVariant = Record<string, unknown> & {
8
6
  css?: string;
9
7
  className?: string;
10
8
  };
11
- interface StringCvaConfig {
12
- base?: string;
13
- variants?: VariantMap;
14
- defaultVariants?: Record<string, string>;
9
+
10
+ type Style = Record<string, unknown>;
11
+ type Props$1 = Record<string, unknown>;
12
+ interface RecipeConfig {
13
+ slots?: string[];
14
+ base?: Style;
15
+ variants?: Record<string, Record<string, Style>>;
16
+ defaultVariants?: Record<string, VariantValue>;
15
17
  compoundVariants?: CompoundVariant[];
16
18
  }
17
- /** String-branch recipe factory for transformed source (TW-style cva). */
18
- declare function cva(config: StringCvaConfig): ((props?: Record<string, unknown>) => string) & {
19
- __cva__: true;
19
+ /** The public surface of a styled-system `cva` that `merge` reads from its operand. */
20
+ interface RecipeLike {
21
+ (props?: Props$1): string;
22
+ raw(props?: Props$1): Style;
23
+ variantKeys?: string[];
24
+ variantMap?: Record<string, string[]>;
25
+ config?: RecipeConfig;
26
+ }
27
+ /**
28
+ * Attach the observable recipe surface to a compiler-specialized `cva`/`sva` function.
29
+ * A `classNameMap` marks the function as a slot recipe.
30
+ */
31
+ declare function attachRecipe<F extends (props?: Props$1) => any, C extends object>(fn: F, config: C, variantKeys: string[], variantMap: Record<string, string[]>, classNameMap?: Record<string, string>): F & {
32
+ config: C;
33
+ raw: (props?: Props$1) => any;
34
+ merge: (other: RecipeLike, cva?: (config: C) => {
35
+ merge(other: RecipeLike): unknown;
36
+ }) => unknown;
37
+ getVariantProps: (props?: Props$1) => Record<string, unknown>;
38
+ splitVariantProps: (props: Props$1) => readonly [Props$1, Props$1];
39
+ classNameMap?: Record<string, string> | undefined;
40
+ __cva__: boolean;
20
41
  variantKeys: string[];
21
42
  variantMap: Record<string, string[]>;
22
- raw: (props?: Record<string, unknown>) => string;
23
- config: StringCvaConfig;
24
- getVariantProps(props: Record<string, unknown>): Record<string, unknown>;
25
- splitVariantProps(props: Record<string, unknown>): readonly [Record<string, unknown>, Record<string, unknown>];
26
43
  };
27
44
 
28
- type SvaConfig = {
29
- slots?: string[];
30
- base?: Record<string, string>;
31
- variants?: StringCvaConfig['variants'];
32
- defaultVariants?: StringCvaConfig['defaultVariants'];
33
- compoundVariants?: Array<Record<string, unknown> & {
34
- css?: Record<string, string> | string;
35
- className?: string;
36
- classNames?: Record<string, string>;
37
- }>;
38
- className?: Record<string, string>;
39
- };
40
- /** String-branch slot recipe factory for transformed source. */
41
- declare function sva(config: SvaConfig): ((props?: Record<string, unknown>) => Record<string, string>) & {
42
- __cva__: false;
43
- raw: (props?: Record<string, unknown>) => Record<string, string>;
44
- config: SvaConfig;
45
- variantMap: {
46
- [k: string]: string[];
47
- };
48
- variantKeys: string[];
49
- classNameMap: Record<string, string>;
50
- getVariantProps(props: Record<string, unknown>): Record<string, unknown>;
51
- splitVariantProps(props: Record<string, unknown>): readonly [Record<string, unknown>, Record<string, unknown>];
52
- };
45
+ type Props = Record<string, unknown>;
46
+ type Resolve<T> = (props?: Props) => T;
47
+ type Memoized<T> = (props?: Props | null) => T;
48
+ /**
49
+ * Memoize a compiler-specialized recipe. `variantMap` lists every prop the function reads; the
50
+ * compiler only specializes recipes whose compounds select on declared variants.
51
+ */
52
+ declare function memoRecipe<T>(resolve: Resolve<T>, variantMap: Record<string, string[]>, hasCompounds?: unknown): Memoized<T>;
53
53
 
54
- export { type StringCvaConfig, css, cva, sva };
54
+ export { attachRecipe, memoRecipe };