@cssxio/compiler 0.2.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.
@@ -0,0 +1,356 @@
1
+ /** Style groups that one utility writes and clears. */
2
+ interface UtilityConflictRecord {
3
+ /** Variant and importance scope where this record applies. */
4
+ readonly scope: string;
5
+ /** Semantic write group owned by the utility. */
6
+ readonly group: string;
7
+ /** Groups cleared when this utility is applied later. */
8
+ readonly conflicts: readonly string[];
9
+ }
10
+ /**
11
+ * A utility record created by the compiler.
12
+ *
13
+ * A null class name clears related style groups without adding CSS.
14
+ */
15
+ type CompiledUtility = readonly [
16
+ className: string | null,
17
+ scope: string,
18
+ group: string,
19
+ ...conflicts: readonly string[]
20
+ ];
21
+ /** A compiled style that contains utility records. */
22
+ interface CompiledStyle {
23
+ /** Marker used to identify a compiled CSSX style. */
24
+ readonly $$css: 2;
25
+ /** Composite class used when this style can be applied as one unit. */
26
+ readonly c: string;
27
+ /** Ordered utility records used by the runtime merge operation. */
28
+ readonly _: readonly CompiledUtility[];
29
+ }
30
+ /** A compiled style map and its generated class names. */
31
+ interface CompiledStyleRecordMap {
32
+ /** Compiled styles keyed by input style name. */
33
+ readonly styles: Readonly<Record<string, CompiledStyle>>;
34
+ /** Generated classes keyed by utility candidate. */
35
+ readonly classes: Readonly<Record<string, string>>;
36
+ /** Source candidates keyed by input style name. */
37
+ readonly candidates: Readonly<Record<string, readonly string[]>>;
38
+ /** Composite class names keyed by input style name. */
39
+ readonly classNames: Readonly<Record<string, string>>;
40
+ /** Winning atomic classes keyed by composite class name. */
41
+ readonly composites: Readonly<Record<string, readonly string[]>>;
42
+ }
43
+ /** Several compiled style maps that share generated class names. */
44
+ interface CompiledStyleRecordMaps {
45
+ /** Compiled style maps keyed by map name. */
46
+ readonly styleMaps: Readonly<Record<string, CompiledStyleRecordMap>>;
47
+ /** Generated classes shared by every map. */
48
+ readonly classes: Readonly<Record<string, string>>;
49
+ /** Winning atomic classes shared by every composite class. */
50
+ readonly composites: Readonly<Record<string, readonly string[]>>;
51
+ }
52
+ /** Options for creating compiled style records. */
53
+ interface StyleCompilerOptions {
54
+ /** CSS theme input used when generating class names. */
55
+ readonly theme?: string;
56
+ /** Options that control generated atomic and composite class names. */
57
+ readonly className?: ClassNameOptions;
58
+ /** Shared allocator used to keep class names unique across compiler calls. */
59
+ readonly classNameAllocator?: ClassNameAllocator;
60
+ /** Controls how aggressively static styles share generated class fragments. */
61
+ readonly reusabilityBudget?: ReusabilityBudget;
62
+ }
63
+ /** Percentage of winning atomic occurrences eligible for reusable fragments. */
64
+ type ReusabilityBudget = number | 'auto';
65
+ /** Options that control generated CSS class names. */
66
+ interface ClassNameOptions {
67
+ /** Naming algorithm. Defaults to `serial`; `random` is a stable content hash. */
68
+ readonly variant?: 'random' | 'serial';
69
+ /** Text prepended to every generated class. Defaults to `s`. */
70
+ readonly prefix?: string;
71
+ /** Text appended to every generated class. Defaults to `x`. */
72
+ readonly suffix?: string;
73
+ /** Length of the hash fragment when `variant` is `random`. */
74
+ readonly length?: number;
75
+ }
76
+ /** Stateful allocator that keeps generated classes unique across compiler calls. */
77
+ interface ClassNameAllocator {
78
+ /** Allocates one unique class for every supplied identity. */
79
+ allocate(identities: readonly string[]): ReadonlyMap<string, string>;
80
+ /** Reserves class names that were allocated outside this allocator. */
81
+ reserve(classNames: readonly string[]): void;
82
+ }
83
+ /**
84
+ * Finds the style groups used by one static utility.
85
+ *
86
+ * @param candidate A static utility string.
87
+ * @returns Its style groups, or null when CSSX does not support it.
88
+ */
89
+ declare function classifyUtility(candidate: string): UtilityConflictRecord | null;
90
+ /**
91
+ * Compiles one static style map to records for the runtime.
92
+ *
93
+ * @param input Style names and their utility strings.
94
+ * @param options Compiler options.
95
+ * @returns The compiled styles, class names, and source candidates.
96
+ */
97
+ declare function compileStyleRecords(input: Readonly<Record<string, string>>, options?: StyleCompilerOptions): CompiledStyleRecordMap;
98
+ /**
99
+ * Compiles several static style maps with shared class names.
100
+ *
101
+ * @param inputs Map names and their utility maps.
102
+ * @param options Compiler options.
103
+ * @returns The compiled maps and their shared class names.
104
+ */
105
+ declare function compileStyleRecordMaps(inputs: Readonly<Record<string, Readonly<Record<string, string>>>>, options?: StyleCompilerOptions): CompiledStyleRecordMaps;
106
+ /**
107
+ * Assigns unique names to identities in one compilation namespace.
108
+ *
109
+ * Serial names use a compact case-sensitive base-62 counter. Random names use
110
+ * a stable hash and deterministic probing, so choosing a shorter hash cannot
111
+ * silently collide.
112
+ *
113
+ * @param options User-supplied naming options.
114
+ * @returns A stateful class-name allocator.
115
+ */
116
+ declare function createClassNameAllocator(options?: ClassNameOptions): ClassNameAllocator;
117
+ /**
118
+ * Merges compiled styles from left to right.
119
+ *
120
+ * @param styles Compiled styles to merge.
121
+ * @returns The final class string.
122
+ */
123
+ declare function mergeCompiledStyles(styles: readonly CompiledStyle[]): string;
124
+ /** A composite class and the atomic classes that implement it. */
125
+ interface StyleComposition {
126
+ /** Stable class for the complete reduced style. */
127
+ readonly className: string;
128
+ /** Winning atomic classes in their source order. */
129
+ readonly atomicClasses: readonly string[];
130
+ }
131
+ /**
132
+ * Creates one composite class for a list of compiled styles.
133
+ *
134
+ * @param styles Compiled styles to compose from left to right.
135
+ * @param classNameAllocator Optional allocator shared with the styles' compilation.
136
+ * @returns The composite class and its winning atomic classes.
137
+ */
138
+ declare function composeCompiledStyles(styles: readonly CompiledStyle[], classNameAllocator?: ClassNameAllocator): StyleComposition;
139
+
140
+ /** One CSS declaration emitted by a utility recipe. */
141
+ interface UtilityDeclaration {
142
+ /** CSS property to emit. */
143
+ readonly property: string;
144
+ /** CSS value to emit. */
145
+ value: string;
146
+ /** Optional selector suffix shared by declarations in one atom. */
147
+ readonly selectorSuffix?: string;
148
+ /** Optional at-rule that wraps this declaration. */
149
+ readonly atRule?: string;
150
+ /** Semantic write group used when compiled styles are merged. */
151
+ readonly semanticGroup?: string;
152
+ /** Semantic groups cleared before this declaration is applied. */
153
+ readonly semanticConflicts?: readonly string[];
154
+ }
155
+
156
+ /** Resolved theme data used while compiling utilities. */
157
+ interface CssxTheme {
158
+ /** Token values, including default values and user overrides. */
159
+ readonly tokens: Readonly<Record<string, string>>;
160
+ /** Complete keyframe rules keyed by animation name. */
161
+ readonly keyframes: Readonly<Record<string, string>>;
162
+ /** Controls whether utility values are inlined or emitted as variables. */
163
+ readonly mode: ThemeOutputMode;
164
+ /** Optional variable prefix used by reference output. */
165
+ readonly prefix: string;
166
+ }
167
+ /** Controls how resolved theme tokens appear in generated CSS. */
168
+ type ThemeOutputMode = 'inline' | 'reference' | 'static';
169
+
170
+ /**
171
+ * Parses CSSX `@theme` blocks and combines them with the built-in theme.
172
+ *
173
+ * Parsing is deliberately narrow: only top-level theme blocks, declarations,
174
+ * namespace resets, and validated keyframes are accepted. The result is frozen
175
+ * so every later compilation phase reads one stable theme snapshot.
176
+ *
177
+ * @param source Optional CSSX theme source.
178
+ * @returns Resolved immutable theme data.
179
+ */
180
+ declare function parseTheme(source?: string): CssxTheme;
181
+
182
+ /** Controls how the `dark` variant is activated. */
183
+ type DarkMode = 'media' | 'selector';
184
+ /** Options that affect how variants are rendered. */
185
+ interface VariantOptions {
186
+ /** Activates `dark` variants with a media query or a `[data-theme=dark]` selector. */
187
+ readonly darkMode?: DarkMode;
188
+ }
189
+
190
+ /** CSS and metadata created from utility strings. */
191
+ interface UtilityCompilation {
192
+ /** Complete generated CSS, including prefix CSS. */
193
+ readonly css: string;
194
+ /** Theme CSS and shared CSS resources. */
195
+ readonly prefixCss: string;
196
+ /** Utility CSS entries in output order. */
197
+ readonly entries: readonly UtilityCssEntry[];
198
+ /** Generated class string keyed by source candidate. */
199
+ readonly classes: Readonly<Record<string, string>>;
200
+ }
201
+ /** One utility string and its generated CSS. */
202
+ interface UtilityCssEntry {
203
+ /** Source utility candidate. */
204
+ readonly candidate: string;
205
+ /** CSS emitted for this candidate and one generated class. */
206
+ readonly css: string;
207
+ }
208
+ /** Shared CSS resources needed by a utility. */
209
+ interface UtilityRecipeResources {
210
+ /** Keyframe names required by the utility. */
211
+ readonly keyframes: readonly string[];
212
+ /** Custom properties that must be registered before emitting CSS. */
213
+ readonly properties: readonly string[];
214
+ }
215
+ /** Style groups written by one utility part. */
216
+ interface UtilityWriteSet {
217
+ /** Semantic group written by this atom. */
218
+ readonly group: string;
219
+ /** Semantic groups cleared by this atom. */
220
+ readonly conflicts: readonly string[];
221
+ }
222
+ /** The compiled parts and metadata for one utility. */
223
+ interface UtilityRecipe {
224
+ /** Source utility candidate. */
225
+ readonly candidate: string;
226
+ /** Separate declaration atoms that can receive separate class names. */
227
+ readonly atoms: readonly (readonly UtilityDeclaration[])[];
228
+ /** Shared CSS resources required by the utility. */
229
+ readonly resources: UtilityRecipeResources;
230
+ /** Semantic write behavior for each declaration atom. */
231
+ readonly writes: readonly UtilityWriteSet[];
232
+ }
233
+ /**
234
+ * Describes the CSS created for one utility.
235
+ *
236
+ * @param candidateSource A static utility string.
237
+ * @param theme The active CSSX theme.
238
+ * @returns Its CSS declaration groups, resources, and style groups.
239
+ */
240
+ declare function describeUtilityRecipe(candidateSource: string, theme: CssxTheme): UtilityRecipe;
241
+ /**
242
+ * Compiles static utility strings to CSS.
243
+ *
244
+ * @param candidates The utility strings to compile.
245
+ * @param className Creates class names for utility strings.
246
+ * @param themeCss Optional CSS theme input.
247
+ * @returns The generated CSS, class names, and CSS entries.
248
+ *
249
+ * The function rejects unsupported utilities, unsafe class names, and more
250
+ * than 50,000 utility strings.
251
+ */
252
+ declare function compileUtilities(candidates: readonly string[], className: (candidate: string) => string, themeCss?: string, selectorAliases?: Readonly<Record<string, readonly string[]>>, includedClasses?: ReadonlySet<string>, variantOptions?: VariantOptions): Promise<UtilityCompilation>;
253
+ /**
254
+ * Compiles utilities using their original source class names as selectors.
255
+ *
256
+ * @param candidates The utility strings to compile.
257
+ * @param themeCss Optional CSS theme input.
258
+ * @param variantOptions Options that affect variant rendering.
259
+ * @returns Generated CSS whose selectors match the source utility strings.
260
+ */
261
+ declare function compileSourceUtilities(candidates: readonly string[], themeCss?: string, variantOptions?: VariantOptions): Promise<UtilityCompilation>;
262
+ /**
263
+ * Checks that a utility has CSS for the active theme.
264
+ *
265
+ * @param candidate A static utility string.
266
+ * @param theme The active CSSX theme.
267
+ * @returns Nothing.
268
+ */
269
+ declare function validateUtilityCandidate(candidate: string, theme: CssxTheme): void;
270
+
271
+ /**
272
+ * Splits a static utility list without treating whitespace in brackets as separators.
273
+ *
274
+ * @param source Whitespace-separated utility source.
275
+ * @returns Individual utility candidates.
276
+ */
277
+ declare function splitCandidateList(source: string): readonly string[];
278
+
279
+ /** One generated class name and the CSS rule it identifies. */
280
+ interface CssxRule {
281
+ /** Stable class name for this generated CSS payload. */
282
+ readonly className: string;
283
+ /** Complete CSS emitted for the class name. */
284
+ readonly css: string;
285
+ }
286
+ /** Options for compiling style maps. */
287
+ interface CompilerOptions {
288
+ /** CSS theme input added to the default theme before compilation. */
289
+ readonly theme?: string;
290
+ /** Options that control generated atomic and composite class names. */
291
+ readonly className?: ClassNameOptions;
292
+ /** Shared allocator used to keep class names unique across compiler calls. */
293
+ readonly classNameAllocator?: ClassNameAllocator;
294
+ /** Controls how aggressively static styles share generated class fragments. */
295
+ readonly reusabilityBudget?: ReusabilityBudget;
296
+ /** Controls how the `dark` variant is activated. */
297
+ readonly darkMode?: DarkMode;
298
+ }
299
+ /** The output from one compiled style map. */
300
+ interface CompileResult {
301
+ /** Compiled runtime styles keyed by the input style name. */
302
+ readonly styles: Readonly<Record<string, CompiledStyle>>;
303
+ /** CSS rules generated for all candidates in this input. */
304
+ readonly rules: readonly CssxRule[];
305
+ /** Generated class names keyed by source candidate. */
306
+ readonly classes: Readonly<Record<string, string>>;
307
+ /** Parsed source candidates keyed by input style name. */
308
+ readonly candidates: Readonly<Record<string, readonly string[]>>;
309
+ /** Composite class names keyed by input style name. */
310
+ readonly classNames: Readonly<Record<string, string>>;
311
+ /** Winning atomic classes keyed by composite class name. */
312
+ readonly composites: Readonly<Record<string, readonly string[]>>;
313
+ }
314
+ /** The output from several compiled style maps. */
315
+ interface CompileMapsResult {
316
+ /** Compiled results keyed by the input map name. */
317
+ readonly styleMaps: Readonly<Record<string, CompiledStyleRecordMap>>;
318
+ /** Shared CSS rules generated across every input map. */
319
+ readonly rules: readonly CssxRule[];
320
+ }
321
+ /**
322
+ * Compiles one map of static utility strings.
323
+ *
324
+ * @param input Style names and their utility strings.
325
+ * @param options Compiler options.
326
+ * @returns The compiled styles, class names, candidates, and CSS rules.
327
+ *
328
+ * The function rejects invalid utilities and invalid theme input.
329
+ */
330
+ declare function compileStyleMap(input: Readonly<Record<string, string>>, options?: CompilerOptions): Promise<CompileResult>;
331
+ /**
332
+ * Compiles several style maps as one set of CSS.
333
+ *
334
+ * @param inputs Map names and their static utility maps.
335
+ * @param options Compiler options.
336
+ * @returns A compiled map for each input and the shared CSS rules.
337
+ *
338
+ * Shared resources, such as keyframes, are added once. The function rejects
339
+ * invalid utilities and invalid theme input.
340
+ */
341
+ declare function compileStyleMaps(inputs: Readonly<Record<string, Readonly<Record<string, string>>>>, options?: CompilerOptions): Promise<CompileMapsResult>;
342
+ /**
343
+ * Joins unique CSS rules in a stable order.
344
+ *
345
+ * @param rules Generated CSS rules.
346
+ * @param options Optional CSS layer settings.
347
+ * @param options.layer CSS layer that wraps the generated rules.
348
+ * @returns The final CSS string.
349
+ */
350
+ declare function serializeCss(rules: readonly CssxRule[], options?: {
351
+ readonly layer?: string;
352
+ }): string;
353
+ /** Inverts composite-to-atom metadata for selector serialization. */
354
+ declare function createSelectorAliases(composites: Readonly<Record<string, readonly string[]>>): Readonly<Record<string, readonly string[]>>;
355
+
356
+ export { type ClassNameAllocator, type ClassNameOptions, type CompileMapsResult, type CompileResult, type CompiledStyle, type CompiledStyleRecordMap, type CompiledStyleRecordMaps, type CompiledUtility, type CompilerOptions, type CssxRule, type CssxTheme, type DarkMode, type ReusabilityBudget, type StyleCompilerOptions, type StyleComposition, type ThemeOutputMode, type UtilityCompilation, type UtilityConflictRecord, type UtilityCssEntry, type UtilityDeclaration, type UtilityRecipe, type UtilityRecipeResources, type UtilityWriteSet, classifyUtility, compileSourceUtilities, compileStyleMap, compileStyleMaps, compileStyleRecordMaps, compileStyleRecords, compileUtilities, composeCompiledStyles, createClassNameAllocator, createSelectorAliases, describeUtilityRecipe, mergeCompiledStyles, parseTheme, serializeCss, splitCandidateList, validateUtilityCandidate };