@master/css-compiler 2.0.0-rc.72 → 2.0.0-rc.74

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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/dist/browser.d.ts +23 -19
  3. package/dist/browser.js +42 -0
  4. package/dist/core.d.ts +43 -41
  5. package/dist/core.js +1639 -0
  6. package/dist/css-transform.d.ts +10 -6
  7. package/dist/css-transform.js +17 -0
  8. package/dist/diagnostics.d.ts +11 -0
  9. package/dist/diagnostics.js +12 -0
  10. package/dist/index.d.ts +41 -38
  11. package/dist/index.js +330 -0
  12. package/dist/lexer/imports.d.ts +16 -12
  13. package/dist/lexer/imports.js +38 -0
  14. package/dist/lexer/source.d.ts +6 -3
  15. package/dist/lexer/source.js +8 -0
  16. package/dist/lexer/standalone-master.d.ts +23 -19
  17. package/dist/lexer/standalone-master.js +219 -0
  18. package/dist/lower-css-directives.d.ts +18 -13
  19. package/dist/lower-css-directives.js +753 -0
  20. package/dist/master-css-manifest.d.ts +18 -12
  21. package/dist/master-css-manifest.js +546 -0
  22. package/dist/native-declaration.d.ts +6 -2
  23. package/dist/native-declaration.js +23 -0
  24. package/dist/utils/selectors.d.ts +6 -3
  25. package/dist/utils/selectors.js +49 -0
  26. package/dist/utils/unquote.d.ts +4 -1
  27. package/dist/utils/unquote.js +9 -0
  28. package/dist/utils/wrap-at-rules.d.ts +4 -1
  29. package/dist/utils/wrap-at-rules.js +11 -0
  30. package/package.json +69 -1
  31. package/dist/browser.mjs +0 -47
  32. package/dist/core.mjs +0 -2146
  33. package/dist/css-transform.mjs +0 -18
  34. package/dist/index.mjs +0 -395
  35. package/dist/lexer/imports.mjs +0 -44
  36. package/dist/lexer/source.mjs +0 -9
  37. package/dist/lexer/standalone-master.mjs +0 -233
  38. package/dist/lower-css-directives.mjs +0 -969
  39. package/dist/master-css-manifest.mjs +0 -760
  40. package/dist/native-declaration.mjs +0 -25
  41. package/dist/utils/selectors.mjs +0 -55
  42. package/dist/utils/unquote.mjs +0 -10
  43. package/dist/utils/wrap-at-rules.mjs +0 -10
@@ -1,14 +1,20 @@
1
- import type { CSSDirectiveManifestInput as SharedCSSDirectiveManifestInput, CSSDirectiveVariableDefinition } from '@master/css-schema/css-directives';
2
- import type { MasterCSSManifest } from '@master/css-schema/manifest';
3
- export type CSSDirectiveManifestInput = SharedCSSDirectiveManifestInput;
4
- export interface CreateMasterCSSManifestOptions {
5
- baseManifest?: MasterCSSManifest;
1
+ import { CSSDirectiveManifestInput as CSSDirectiveManifestInput$1, CSSDirectiveVariableDefinition } from "@master/css-schema/css-directives";
2
+ import { MasterCSSManifest } from "@master/css-schema/manifest";
3
+ import { CompilerDiagnosticRecorder } from "./diagnostics.js";
4
+
5
+ //#region src/master-css-manifest.d.ts
6
+ type CSSDirectiveManifestInput = CSSDirectiveManifestInput$1;
7
+ interface CreateMasterCSSManifestOptions {
8
+ baseManifest?: MasterCSSManifest;
9
+ diagnostics?: CompilerDiagnosticRecorder;
6
10
  }
7
- export interface ResolvedCSSDirectiveVariableName {
8
- name: string;
9
- key: string;
10
- namespace?: string;
11
+ interface ResolvedCSSDirectiveVariableName {
12
+ name: string;
13
+ key: string;
14
+ namespace?: string;
11
15
  }
12
- export type CSSDirectiveVariableNameResolver = (variable: CSSDirectiveVariableDefinition) => ResolvedCSSDirectiveVariableName;
13
- export declare function createVariableNameResolver(input?: CSSDirectiveManifestInput, options?: CreateMasterCSSManifestOptions): CSSDirectiveVariableNameResolver;
14
- export declare function createMasterCSSManifest(input?: CSSDirectiveManifestInput, options?: CreateMasterCSSManifestOptions): MasterCSSManifest;
16
+ type CSSDirectiveVariableNameResolver = (variable: CSSDirectiveVariableDefinition) => ResolvedCSSDirectiveVariableName;
17
+ declare function createVariableNameResolver(input?: CSSDirectiveManifestInput, options?: CreateMasterCSSManifestOptions): CSSDirectiveVariableNameResolver;
18
+ declare function createMasterCSSManifest(input?: CSSDirectiveManifestInput, options?: CreateMasterCSSManifestOptions): MasterCSSManifest;
19
+ //#endregion
20
+ export { CSSDirectiveManifestInput, CSSDirectiveVariableNameResolver, CreateMasterCSSManifestOptions, ResolvedCSSDirectiveVariableName, createMasterCSSManifest, createVariableNameResolver };
@@ -0,0 +1,546 @@
1
+ import { addCompilerDiagnosticCount, setCompilerDiagnosticCount, timeCompilerDiagnostic } from "./diagnostics.js";
2
+ import { createCompilerCSS, parseAt, parseSelector } from "@master/css-engine/compiler";
3
+ import UtilityType from "@master/css-schema/utility-type";
4
+ import { flattenMasterCSSManifestVariables, groupMasterCSSManifestVariables } from "@master/css-schema/manifest";
5
+ import { builtinNamespaces } from "@master/css-engine";
6
+ import { isNativeCSSShorthandProperty } from "@master/css-schema/native-css-shorthand";
7
+ //#region src/master-css-manifest.ts
8
+ const NUMERIC_THEME_NAMESPACES = /* @__PURE__ */ new Set([
9
+ "font-size",
10
+ "radius",
11
+ "spacing",
12
+ "breakpoint",
13
+ "container"
14
+ ]);
15
+ const UNITFUL_NUMERIC_TOKEN = /^([+-]?(?:\d+(?:\.\d+)?|\.\d+))(px|rem)?$/;
16
+ function clone(value) {
17
+ if (Array.isArray(value)) return value.map((item) => clone(item));
18
+ if (value && typeof value === "object") {
19
+ const result = {};
20
+ for (const key in value) result[key] = clone(value[key]);
21
+ return result;
22
+ }
23
+ return value;
24
+ }
25
+ function normalizeZero(value) {
26
+ return Object.is(value, -0) ? 0 : value;
27
+ }
28
+ function addVariableNamespace(namespaces, namespace) {
29
+ if (typeof namespace === "string" && namespace) namespaces.add(namespace);
30
+ }
31
+ function addVariableAliasRefNamespace(namespaces, ref) {
32
+ if (typeof ref === "string" && (ref[0] === "=" || ref[0] === "~")) addVariableNamespace(namespaces, ref.slice(1));
33
+ }
34
+ function addImplicitUtilityNamespace(namespaces, value) {
35
+ if (typeof value !== "string" || !value || value[0] === "." || value.includes("()")) return;
36
+ addVariableNamespace(namespaces, value);
37
+ }
38
+ function addUtilityVariableNamespaces(namespaces, utilities) {
39
+ for (const utility of utilities || []) {
40
+ for (const namespace of utility.namespaces || []) addVariableNamespace(namespaces, namespace);
41
+ for (const ref of utility.variableAliasRefs || []) addVariableAliasRefNamespace(namespaces, ref);
42
+ if (utility.implicitNamespace !== false && utility.emit?.type === "property") {
43
+ addImplicitUtilityNamespace(namespaces, utility.id);
44
+ addImplicitUtilityNamespace(namespaces, utility.name);
45
+ }
46
+ }
47
+ }
48
+ function collectVariableNamespaces(input = {}, options = {}) {
49
+ const namespaces = new Set(builtinNamespaces);
50
+ for (const variable of flattenMasterCSSManifestVariables(options.baseManifest?.variables)) addVariableNamespace(namespaces, variable.namespace);
51
+ for (const variable of input.variables || []) addVariableNamespace(namespaces, variable.namespace);
52
+ addUtilityVariableNamespaces(namespaces, options.baseManifest?.utilities);
53
+ addUtilityVariableNamespaces(namespaces, input.utilities);
54
+ return [...namespaces].sort((a, b) => b.length - a.length || a.localeCompare(b));
55
+ }
56
+ function resolveVariableName(variable, namespaces) {
57
+ const explicitName = variable.name?.replace(/^--/, "");
58
+ if (variable.namespace || variable.key !== void 0) {
59
+ const key = variable.key ?? explicitName ?? "";
60
+ return {
61
+ name: variable.namespace ? `${variable.namespace}${key ? "-" + key : ""}` : key,
62
+ key,
63
+ ...variable.namespace ? { namespace: variable.namespace } : {}
64
+ };
65
+ }
66
+ const name = explicitName || "";
67
+ const namespace = namespaces.find((eachNamespace) => name.startsWith(eachNamespace + "-"));
68
+ return namespace ? {
69
+ name,
70
+ namespace,
71
+ key: name.slice(namespace.length + 1)
72
+ } : {
73
+ name,
74
+ key: name
75
+ };
76
+ }
77
+ function createVariableNameResolver(input = {}, options = {}) {
78
+ const namespaces = collectVariableNamespaces(input, options);
79
+ return (variable) => resolveVariableName(variable, namespaces);
80
+ }
81
+ function getVariableType(value) {
82
+ return typeof value === "number" ? "number" : "string";
83
+ }
84
+ function getVariableNumericValue(value, namespace) {
85
+ if (!namespace || !NUMERIC_THEME_NAMESPACES.has(namespace)) return;
86
+ if (typeof value === "number") return { value };
87
+ if (typeof value !== "string") return;
88
+ const match = UNITFUL_NUMERIC_TOKEN.exec(value.trim());
89
+ if (!match) return;
90
+ return {
91
+ value: Number(match[1]),
92
+ ...match[2] ? { unit: match[2] } : {}
93
+ };
94
+ }
95
+ function collectVariableDependencies(value, dependencies = /* @__PURE__ */ new Set()) {
96
+ if (typeof value !== "string") return dependencies;
97
+ for (const match of value.matchAll(/\$(-?[_a-zA-Z0-9-]+)/g)) dependencies.add(match[1]);
98
+ for (const match of value.matchAll(/var\(\s*--(-?[_a-zA-Z0-9-]+)/g)) dependencies.add(match[1]);
99
+ return dependencies;
100
+ }
101
+ function variableSlot(variable) {
102
+ return variable.name || `${variable.namespace || ""}\0${variable.key}`;
103
+ }
104
+ function pushVariable(target, variable) {
105
+ const slot = variableSlot(variable);
106
+ const existing = target.find((eachVariable) => variableSlot(eachVariable) === slot);
107
+ if (existing) Object.assign(existing, variable);
108
+ else target.push(variable);
109
+ }
110
+ function compileVariables(input, resolveVariableName) {
111
+ if (!input?.length) return;
112
+ const variables = [];
113
+ const byName = /* @__PURE__ */ new Map();
114
+ for (const definition of input) {
115
+ const resolved = resolveVariableName(definition);
116
+ if (!resolved.name) continue;
117
+ const value = normalizeZero(definition.value);
118
+ const numeric = getVariableNumericValue(value, resolved.namespace);
119
+ const type = numeric ? "number" : getVariableType(value);
120
+ const dependencies = collectVariableDependencies(value);
121
+ if (definition.mode) {
122
+ let target = byName.get(resolved.name);
123
+ if (!target) {
124
+ target = {
125
+ name: resolved.name,
126
+ key: resolved.key,
127
+ ...resolved.namespace ? { namespace: resolved.namespace } : {},
128
+ type,
129
+ modes: {},
130
+ ...definition.static ? { static: true } : {}
131
+ };
132
+ byName.set(resolved.name, target);
133
+ pushVariable(variables, target);
134
+ }
135
+ if (definition.static) target.static = true;
136
+ target.modes ??= {};
137
+ target.modes[definition.mode] = {
138
+ type,
139
+ value,
140
+ ...numeric ? { numeric } : {}
141
+ };
142
+ if (dependencies.size) {
143
+ const next = /* @__PURE__ */ new Set([...target.dependencies || [], ...dependencies]);
144
+ target.dependencies = [...next];
145
+ }
146
+ continue;
147
+ }
148
+ const variable = {
149
+ name: resolved.name,
150
+ key: resolved.key,
151
+ ...resolved.namespace ? { namespace: resolved.namespace } : {},
152
+ type,
153
+ value,
154
+ ...numeric ? { numeric } : {},
155
+ ...dependencies.size ? { dependencies: [...dependencies] } : {},
156
+ ...definition.inline ? { inline: true } : {},
157
+ ...definition.static ? { static: true } : {}
158
+ };
159
+ byName.set(resolved.name, variable);
160
+ pushVariable(variables, variable);
161
+ }
162
+ return variables.length ? variables : void 0;
163
+ }
164
+ function normalizeNumericAtRuleValue(variable, rootSize) {
165
+ if (variable.key.startsWith("-")) return;
166
+ const numeric = variable.numeric || (typeof variable.value === "number" ? { value: variable.value } : void 0);
167
+ if (!numeric) return;
168
+ switch (numeric.unit) {
169
+ case void 0:
170
+ case "":
171
+ case "px": return numeric.value / rootSize;
172
+ case "rem": return numeric.value;
173
+ default: return;
174
+ }
175
+ }
176
+ function createVariableAtRule(variable, id, rootSize) {
177
+ const value = normalizeNumericAtRuleValue(variable, rootSize);
178
+ if (value === void 0) return;
179
+ return {
180
+ id,
181
+ nodes: [{
182
+ type: "number",
183
+ value,
184
+ unit: "rem"
185
+ }]
186
+ };
187
+ }
188
+ function compileAtRules(variables, rootSize) {
189
+ const atRules = {};
190
+ const breakpointAtRules = {};
191
+ const containerAtRules = {};
192
+ for (const variable of variables || []) if (variable.namespace === "breakpoint") {
193
+ const atRule = createVariableAtRule(variable, "media", rootSize);
194
+ if (atRule) {
195
+ atRules[variable.key] = atRule;
196
+ breakpointAtRules[variable.key] = atRule;
197
+ }
198
+ } else if (variable.namespace === "container") {
199
+ const atRule = createVariableAtRule(variable, "container", rootSize);
200
+ if (atRule) containerAtRules[variable.key] = atRule;
201
+ }
202
+ return {
203
+ atRules: Object.keys(atRules).length ? atRules : void 0,
204
+ breakpointAtRules: Object.keys(breakpointAtRules).length ? breakpointAtRules : void 0,
205
+ containerAtRules: Object.keys(containerAtRules).length ? containerAtRules : void 0
206
+ };
207
+ }
208
+ function cloneAtRuleNode(node) {
209
+ return "children" in node ? {
210
+ ...node,
211
+ children: node.children.map(cloneAtRuleNode)
212
+ } : { ...node };
213
+ }
214
+ function cloneAtRule(atRule) {
215
+ return {
216
+ id: atRule.id,
217
+ nodes: atRule.nodes.map(cloneAtRuleNode)
218
+ };
219
+ }
220
+ function cloneSelectorNode(node) {
221
+ return "children" in node && node.children?.length ? {
222
+ ...node,
223
+ children: node.children.map(cloneSelectorNode)
224
+ } : { ...node };
225
+ }
226
+ function compileVariantBranch(branch, css) {
227
+ const selector = branch.selector?.trim();
228
+ const bodylessSelector = selector ? selector.includes("&") ? selector.replace(/&/g, "") : selector : void 0;
229
+ const selectorNodes = branch.selectorNodes?.length ? branch.selectorNodes.map(cloneSelectorNode) : bodylessSelector ? parseSelector(bodylessSelector, css, false).map((node) => cloneSelectorNode(node)) : void 0;
230
+ const atRuleNodes = branch.atRuleNodes?.length ? branch.atRuleNodes.map(cloneAtRule) : branch.atRules?.length ? branch.atRules.map((atRule) => cloneAtRule(parseAt(atRule, css, false))) : void 0;
231
+ return {
232
+ ...branch,
233
+ ...selectorNodes?.length ? { selectorNodes } : {},
234
+ ...branch.atRules?.length ? { atRules: [...branch.atRules] } : {},
235
+ ...atRuleNodes?.length ? { atRuleNodes } : {}
236
+ };
237
+ }
238
+ function compileVariants(input, baseManifest) {
239
+ if (!input?.length) return {};
240
+ const css = createCompilerCSS(baseManifest);
241
+ const variants = [];
242
+ const selectors = {};
243
+ const atRules = {};
244
+ for (const variant of input) {
245
+ const branches = variant.branches.map((branch) => compileVariantBranch(branch, css));
246
+ variants.push({
247
+ token: variant.token,
248
+ branches
249
+ });
250
+ if (variant.token.startsWith(":")) {
251
+ const firstSelectorNodes = branches.find((branch) => branch.selectorNodes?.length)?.selectorNodes;
252
+ if (firstSelectorNodes?.length) selectors[variant.token] = firstSelectorNodes.map(cloneSelectorNode);
253
+ }
254
+ if (variant.token.startsWith("@")) {
255
+ const firstAtRule = branches.find((branch) => branch.atRuleNodes?.length)?.atRuleNodes?.[0];
256
+ if (firstAtRule) atRules[variant.token.slice(1)] = cloneAtRule(firstAtRule);
257
+ else {
258
+ const firstLayer = branches.find((branch) => branch.layer)?.layer;
259
+ if (firstLayer) atRules[variant.token.slice(1)] = {
260
+ id: "layer",
261
+ nodes: [{
262
+ type: "string",
263
+ value: firstLayer
264
+ }]
265
+ };
266
+ }
267
+ }
268
+ }
269
+ return {
270
+ variants,
271
+ ...Object.keys(selectors).length ? { selectors } : {},
272
+ ...Object.keys(atRules).length ? { atRules } : {}
273
+ };
274
+ }
275
+ function cloneDeclarations(declarations) {
276
+ return Array.isArray(declarations) ? [...declarations] : { ...declarations };
277
+ }
278
+ function assertNoValuePlaceholder(declarations) {
279
+ for (const property in declarations) if (declarations[property].includes("--value")) throw new Error("--value() is only supported inside managed pattern declarations");
280
+ }
281
+ function compileUtilityRule(rule) {
282
+ assertNoValuePlaceholder(rule.declarations);
283
+ return {
284
+ declarations: cloneDeclarations(rule.declarations),
285
+ ...rule.atRules?.length ? { atRules: [...rule.atRules] } : {},
286
+ ...rule.selector && rule.selector !== "&" ? { selector: rule.selector } : {}
287
+ };
288
+ }
289
+ const VALUE_PLACEHOLDER_FUNCTION = "--value()";
290
+ const VALUE_PLACEHOLDER_CALL = /--value\s*\(([^)]*)\)/g;
291
+ function compilePatternDeclarationValue(value) {
292
+ if (!value.includes("--value")) return value;
293
+ const parts = [];
294
+ let lastIndex = 0;
295
+ let matched = false;
296
+ VALUE_PLACEHOLDER_CALL.lastIndex = 0;
297
+ for (const match of value.matchAll(VALUE_PLACEHOLDER_CALL)) {
298
+ matched = true;
299
+ if (match[1].trim()) throw new Error("--value() does not accept arguments");
300
+ if (match.index > lastIndex) parts.push(value.slice(lastIndex, match.index));
301
+ parts.push(null);
302
+ lastIndex = match.index + match[0].length;
303
+ }
304
+ if (!matched || value.slice(lastIndex).includes("--value")) throw new Error("--value() must be called as --value()");
305
+ if (lastIndex < value.length) parts.push(value.slice(lastIndex));
306
+ return value === VALUE_PLACEHOLDER_FUNCTION ? null : parts;
307
+ }
308
+ function compilePatternDeclarations(declarations) {
309
+ const compiled = {};
310
+ for (const property in declarations) compiled[property] = compilePatternDeclarationValue(declarations[property]);
311
+ return compiled;
312
+ }
313
+ function compilePatternUtilityRule(rule) {
314
+ return {
315
+ declarations: compilePatternDeclarations(rule.declarations),
316
+ ...rule.atRules?.length ? { atRules: [...rule.atRules] } : {},
317
+ ...rule.selector && rule.selector !== "&" ? { selector: rule.selector } : {}
318
+ };
319
+ }
320
+ function declarationValueUsesPlaceholder(value) {
321
+ return value === null || Array.isArray(value) && value.includes(null);
322
+ }
323
+ function countValuePlaceholderProperties(declarations) {
324
+ let count = 0;
325
+ const declarationMap = declarations;
326
+ for (const property in declarationMap) if (declarationValueUsesPlaceholder(declarationMap[property])) count++;
327
+ return count;
328
+ }
329
+ function utilityTypeFromRules(rules) {
330
+ return rules.some(({ declarations }) => {
331
+ return Object.keys(declarations).some((property) => isNativeCSSShorthandProperty(property)) || countValuePlaceholderProperties(declarations) > 1;
332
+ }) ? UtilityType.Shorthand : UtilityType.Normal;
333
+ }
334
+ function getPatternMatcher(pattern) {
335
+ return {
336
+ type: "pattern",
337
+ prefix: pattern.prefix,
338
+ values: [...pattern.values]
339
+ };
340
+ }
341
+ function getDynamicMatchers(dynamic) {
342
+ const matchers = [];
343
+ if (dynamic.variableAliasRefs?.length) matchers.push({
344
+ type: "variable",
345
+ keys: [dynamic.key]
346
+ });
347
+ if (dynamic.kind) matchers.push({
348
+ type: "value",
349
+ keys: [dynamic.key]
350
+ });
351
+ if (dynamic.arbitrary) matchers.push({
352
+ type: "key",
353
+ keys: [dynamic.key]
354
+ });
355
+ if (dynamic.values?.length) matchers.push({
356
+ type: "pattern",
357
+ prefix: dynamic.key + ":",
358
+ values: [...dynamic.values]
359
+ });
360
+ if (!matchers.length) throw new Error("Managed dynamic utility definition must include at least one value source");
361
+ return matchers;
362
+ }
363
+ function compileUtility(definition, order) {
364
+ if (definition.type === "pattern") {
365
+ if (!definition.pattern) throw new Error("Managed enum pattern definition is missing a pattern");
366
+ const rules = [];
367
+ if (definition.declarations) rules.push({
368
+ declarations: compilePatternDeclarations(definition.declarations),
369
+ ...definition.atRules?.length ? { atRules: [...definition.atRules] } : {}
370
+ });
371
+ if (definition.rules?.length) rules.push(...definition.rules.map(compilePatternUtilityRule));
372
+ return {
373
+ id: definition.name,
374
+ name: definition.name,
375
+ type: UtilityType.Semantic,
376
+ order,
377
+ layer: definition.layer || "utilities",
378
+ emit: {
379
+ type: "static",
380
+ rules
381
+ },
382
+ matchers: [getPatternMatcher(definition.pattern)]
383
+ };
384
+ }
385
+ if (definition.type === "dynamic") {
386
+ if (!definition.dynamic) throw new Error("Managed dynamic utility definition is missing a dynamic source");
387
+ const rules = [];
388
+ if (definition.declarations) rules.push({
389
+ declarations: compilePatternDeclarations(definition.declarations),
390
+ ...definition.atRules?.length ? { atRules: [...definition.atRules] } : {}
391
+ });
392
+ if (definition.rules?.length) rules.push(...definition.rules.map(compilePatternUtilityRule));
393
+ return {
394
+ id: definition.name,
395
+ name: definition.name,
396
+ type: utilityTypeFromRules(rules),
397
+ order,
398
+ layer: definition.layer || "utilities",
399
+ ...definition.dynamic.kind ? { kind: definition.dynamic.kind } : {},
400
+ ...definition.dynamic.variableAliasRefs?.length ? { variableAliasRefs: [...definition.dynamic.variableAliasRefs] } : {},
401
+ emit: {
402
+ type: "static",
403
+ rules
404
+ },
405
+ matchers: getDynamicMatchers(definition.dynamic)
406
+ };
407
+ }
408
+ const name = definition.name.startsWith(".") ? definition.name.slice(1) : definition.name;
409
+ const rules = [];
410
+ if (definition.declarations) {
411
+ assertNoValuePlaceholder(definition.declarations);
412
+ rules.push({
413
+ declarations: cloneDeclarations(definition.declarations),
414
+ ...definition.atRules?.length ? { atRules: [...definition.atRules] } : {}
415
+ });
416
+ }
417
+ if (definition.rules?.length) rules.push(...definition.rules.map(compileUtilityRule));
418
+ return {
419
+ id: "." + name,
420
+ name,
421
+ type: UtilityType.Semantic,
422
+ order,
423
+ layer: definition.layer || "utilities",
424
+ emit: {
425
+ type: "static",
426
+ rules
427
+ },
428
+ matchers: [{
429
+ type: "static",
430
+ name
431
+ }]
432
+ };
433
+ }
434
+ function compileUtilities(input) {
435
+ if (!input?.length) return;
436
+ return input.map((definition, index) => compileUtility(definition, index));
437
+ }
438
+ function compileAnimations(input) {
439
+ return input ? clone(input) : void 0;
440
+ }
441
+ function compileAnimationOptions(input) {
442
+ return input ? clone(input) : void 0;
443
+ }
444
+ function mergeBy(base, next, getKey) {
445
+ const merged = [...(base || []).map((value) => clone(value))];
446
+ for (const value of next || []) {
447
+ const key = getKey(value);
448
+ const index = key === void 0 ? -1 : merged.findIndex((existing) => getKey(existing) === key);
449
+ if (index === -1) merged.push(clone(value));
450
+ else merged[index] = clone(value);
451
+ }
452
+ return merged.length ? merged : void 0;
453
+ }
454
+ function mergeVariables(base, next) {
455
+ return groupMasterCSSManifestVariables(mergeBy(flattenMasterCSSManifestVariables(base), flattenMasterCSSManifestVariables(next), (variable) => variable.name));
456
+ }
457
+ function mergeRecords(base, next) {
458
+ return Object.keys(base || {}).length || Object.keys(next || {}).length ? {
459
+ ...base ? clone(base) : {},
460
+ ...next ? clone(next) : {}
461
+ } : void 0;
462
+ }
463
+ function mergeAnimationOptions(base, next, animations) {
464
+ const merged = mergeRecords(base, next);
465
+ if (!merged) return;
466
+ for (const animationName of Object.keys(animations || {})) if (!next?.[animationName]) delete merged[animationName];
467
+ return Object.keys(merged).length ? merged : void 0;
468
+ }
469
+ function mergeManifest(baseManifest, fragment) {
470
+ if (!baseManifest) return clone(fragment);
471
+ const manifest = {
472
+ version: 1,
473
+ settings: {
474
+ ...baseManifest.settings || {},
475
+ ...fragment.settings || {}
476
+ },
477
+ variables: mergeVariables(baseManifest.variables, fragment.variables),
478
+ animations: mergeRecords(baseManifest.animations, fragment.animations),
479
+ animationOptions: mergeAnimationOptions(baseManifest.animationOptions, fragment.animationOptions, fragment.animations),
480
+ variants: mergeBy(baseManifest.variants, fragment.variants, (variant) => variant.token),
481
+ atRules: mergeRecords(baseManifest.atRules, fragment.atRules),
482
+ breakpointAtRules: mergeRecords(baseManifest.breakpointAtRules, fragment.breakpointAtRules),
483
+ containerAtRules: mergeRecords(baseManifest.containerAtRules, fragment.containerAtRules),
484
+ selectors: mergeRecords(baseManifest.selectors, fragment.selectors),
485
+ utilities: mergeBy(baseManifest.utilities, fragment.utilities, (utility) => `${utility.id}\0${utility.layer || ""}`),
486
+ debug: mergeRecords(baseManifest.debug, fragment.debug)
487
+ };
488
+ return Object.fromEntries(Object.entries(manifest).filter(([, value]) => value !== void 0 && (!Array.isArray(value) || value.length) && (typeof value !== "object" || Array.isArray(value) || Object.keys(value).length)));
489
+ }
490
+ function createMasterCSSManifest(input = {}, options = {}) {
491
+ const diagnostics = options.diagnostics;
492
+ addCompilerDiagnosticCount(diagnostics, "manifest-create-count");
493
+ setCompilerDiagnosticCount(diagnostics, "manifest-input-variable-count", input.variables?.length || 0);
494
+ setCompilerDiagnosticCount(diagnostics, "manifest-input-utility-count", input.utilities?.length || 0);
495
+ setCompilerDiagnosticCount(diagnostics, "manifest-input-variant-count", input.variants?.length || 0);
496
+ setCompilerDiagnosticCount(diagnostics, "manifest-input-animation-count", Object.keys(input.animations || {}).length);
497
+ const rootSize = input.rootSize ?? options.baseManifest?.settings?.rootSize ?? 16;
498
+ const resolveVariableName = timeCompilerDiagnostic(diagnostics, "manifest-variable-name-resolver-ms", () => createVariableNameResolver(input, options));
499
+ const variableDefinitions = timeCompilerDiagnostic(diagnostics, "manifest-compile-variables-ms", () => compileVariables(input.variables, resolveVariableName));
500
+ const variables = timeCompilerDiagnostic(diagnostics, "manifest-group-variables-ms", () => groupMasterCSSManifestVariables(variableDefinitions));
501
+ const { atRules, breakpointAtRules, containerAtRules } = timeCompilerDiagnostic(diagnostics, "manifest-compile-at-rules-ms", () => compileAtRules(variableDefinitions, rootSize));
502
+ const settings = {
503
+ ...input.rootSize !== void 0 ? { rootSize: input.rootSize } : {},
504
+ ...input.baseUnit !== void 0 ? { baseUnit: input.baseUnit } : {},
505
+ ...input.defaultMode !== void 0 ? { defaultMode: input.defaultMode } : {},
506
+ ...input.scope !== void 0 ? { scope: input.scope } : {},
507
+ ...input.important !== void 0 ? { important: input.important } : {},
508
+ ...input.modeTrigger !== void 0 ? { modeTrigger: input.modeTrigger } : {},
509
+ ...input.modes?.length ? { modes: [...input.modes] } : {}
510
+ };
511
+ const variantBaseManifest = timeCompilerDiagnostic(diagnostics, "manifest-variant-base-merge-ms", () => mergeManifest(options.baseManifest, {
512
+ version: 1,
513
+ ...Object.keys(settings).length ? { settings } : {},
514
+ ...variables ? { variables } : {},
515
+ ...atRules ? { atRules } : {},
516
+ ...breakpointAtRules ? { breakpointAtRules } : {},
517
+ ...containerAtRules ? { containerAtRules } : {}
518
+ }));
519
+ const { variants, selectors, atRules: variantAtRules } = timeCompilerDiagnostic(diagnostics, "manifest-compile-variants-ms", () => compileVariants(input.variants, variantBaseManifest));
520
+ const utilities = timeCompilerDiagnostic(diagnostics, "manifest-compile-utilities-ms", () => compileUtilities(input.utilities));
521
+ const animations = timeCompilerDiagnostic(diagnostics, "manifest-compile-animations-ms", () => compileAnimations(input.animations));
522
+ const animationOptions = timeCompilerDiagnostic(diagnostics, "manifest-compile-animation-options-ms", () => compileAnimationOptions(input.animationOptions));
523
+ const fragment = {
524
+ version: 1,
525
+ ...Object.keys(settings).length ? { settings } : {},
526
+ ...variables ? { variables } : {},
527
+ ...animations ? { animations } : {},
528
+ ...animationOptions ? { animationOptions } : {},
529
+ ...variants?.length ? { variants } : {},
530
+ ...atRules || variantAtRules ? { atRules: mergeRecords(atRules, variantAtRules) } : {},
531
+ ...breakpointAtRules ? { breakpointAtRules } : {},
532
+ ...containerAtRules ? { containerAtRules } : {},
533
+ ...selectors ? { selectors } : {},
534
+ ...utilities?.length ? { utilities } : {}
535
+ };
536
+ const manifest = timeCompilerDiagnostic(diagnostics, "manifest-final-merge-ms", () => mergeManifest(options.baseManifest, fragment));
537
+ setCompilerDiagnosticCount(diagnostics, "manifest-output-variable-count", Object.keys(manifest.variables || {}).length);
538
+ setCompilerDiagnosticCount(diagnostics, "manifest-output-utility-count", manifest.utilities?.length || 0);
539
+ setCompilerDiagnosticCount(diagnostics, "manifest-output-variant-count", manifest.variants?.length || 0);
540
+ setCompilerDiagnosticCount(diagnostics, "manifest-output-selector-count", Object.keys(manifest.selectors || {}).length);
541
+ setCompilerDiagnosticCount(diagnostics, "manifest-output-at-rule-count", Object.keys(manifest.atRules || {}).length);
542
+ setCompilerDiagnosticCount(diagnostics, "manifest-output-animation-count", Object.keys(manifest.animations || {}).length);
543
+ return manifest;
544
+ }
545
+ //#endregion
546
+ export { createMasterCSSManifest, createVariableNameResolver };
@@ -1,2 +1,6 @@
1
- import type { NativeCSSDeclarationMatcher } from '@master/css-engine';
2
- export declare const cssTreeNativeDeclarationMatcher: NativeCSSDeclarationMatcher;
1
+ import { NativeCSSDeclarationMatcher } from "@master/css-engine";
2
+
3
+ //#region src/native-declaration.d.ts
4
+ declare const cssTreeNativeDeclarationMatcher: NativeCSSDeclarationMatcher;
5
+ //#endregion
6
+ export { cssTreeNativeDeclarationMatcher };
@@ -0,0 +1,23 @@
1
+ import { lexer, parse, property } from "css-tree";
2
+ //#region src/native-declaration.ts
3
+ const matches = /* @__PURE__ */ new Map();
4
+ function isTargetError(error) {
5
+ return error && typeof error === "object" && "name" in error && (error.name === "SyntaxError" || error.name === "SyntaxMatchError" || error.name === "SyntaxReferenceError");
6
+ }
7
+ const cssTreeNativeDeclarationMatcher = ({ property: property$1, value }) => {
8
+ if (property(property$1).custom) return true;
9
+ const cacheKey = property$1 + "\0" + value;
10
+ const cached = matches.get(cacheKey);
11
+ if (cached !== void 0) return cached;
12
+ let matched = false;
13
+ try {
14
+ const ast = parse(value, { context: "value" });
15
+ matched = !isTargetError(lexer.checkPropertyName(property$1)) && !isTargetError(lexer.matchProperty(property$1, ast).error);
16
+ } catch {
17
+ matched = false;
18
+ }
19
+ matches.set(cacheKey, matched);
20
+ return matched;
21
+ };
22
+ //#endregion
23
+ export { cssTreeNativeDeclarationMatcher };
@@ -1,3 +1,6 @@
1
- export declare function splitSelectorList(selectorText: string): string[];
2
- export declare function combineSelectorLists(parentSelectors: string[], childSelectors: string[]): string[];
3
- export declare function combineStyleSelectors(parentSelector: string, childSelector: string): string;
1
+ //#region src/utils/selectors.d.ts
2
+ declare function splitSelectorList(selectorText: string): string[];
3
+ declare function combineSelectorLists(parentSelectors: string[], childSelectors: string[]): string[];
4
+ declare function combineStyleSelectors(parentSelector: string, childSelector: string): string;
5
+ //#endregion
6
+ export { combineSelectorLists, combineStyleSelectors, splitSelectorList };
@@ -0,0 +1,49 @@
1
+ //#region src/utils/selectors.ts
2
+ function splitSelectorList(selectorText) {
3
+ const selectors = [];
4
+ let current = "";
5
+ let depth = 0;
6
+ let quote = "";
7
+ for (let index = 0; index < selectorText.length; index++) {
8
+ const char = selectorText[index];
9
+ if (quote) {
10
+ current += char;
11
+ if (char === "\\") current += selectorText[++index] || "";
12
+ else if (char === quote) quote = "";
13
+ continue;
14
+ }
15
+ if (char === "\"" || char === "'") {
16
+ quote = char;
17
+ current += char;
18
+ continue;
19
+ }
20
+ if (char === "(" || char === "[") {
21
+ depth++;
22
+ current += char;
23
+ continue;
24
+ }
25
+ if (char === ")" || char === "]") {
26
+ depth--;
27
+ current += char;
28
+ continue;
29
+ }
30
+ if (char === "," && depth === 0) {
31
+ selectors.push(current.trim());
32
+ current = "";
33
+ continue;
34
+ }
35
+ current += char;
36
+ }
37
+ if (current.trim()) selectors.push(current.trim());
38
+ return selectors;
39
+ }
40
+ function combineSelectorLists(parentSelectors, childSelectors) {
41
+ const selectors = [];
42
+ for (const child of childSelectors) for (const parent of parentSelectors) selectors.push(child.includes("&") ? child.replace(/&/g, parent) : `${parent} ${child}`);
43
+ return selectors;
44
+ }
45
+ function combineStyleSelectors(parentSelector, childSelector) {
46
+ return combineSelectorLists(splitSelectorList(parentSelector), splitSelectorList(childSelector)).join(",");
47
+ }
48
+ //#endregion
49
+ export { combineSelectorLists, combineStyleSelectors, splitSelectorList };
@@ -1 +1,4 @@
1
- export default function unquote(value: string): string;
1
+ //#region src/utils/unquote.d.ts
2
+ declare function unquote(value: string): string;
3
+ //#endregion
4
+ export { unquote as default };