@homebound/truss 2.29.4 → 2.29.6

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.
@@ -2428,18 +2428,56 @@ function computeRulePriority(rule) {
2428
2428
  function isVariableRule(rule) {
2429
2429
  return rule.declarations.some((d) => d.cssVarName !== void 0);
2430
2430
  }
2431
+ function ruleSortKey(priority, className, atRulePrelude2) {
2432
+ const widthInterval = atRulePrelude2 === void 0 ? null : parseWidthInterval(atRulePrelude2);
2433
+ return { priority, className, widthInterval };
2434
+ }
2435
+ function compareRuleSortKeys(a, b) {
2436
+ return a.priority - b.priority || compareWidthIntervals(a.widthInterval, b.widthInterval) || compareClassNames(a.className, b.className);
2437
+ }
2431
2438
  function sortRulesByPriority(rules) {
2432
2439
  const decorated = Array.from(rules, (rule) => {
2433
- return { rule, priority: computeRulePriority(rule) };
2434
- });
2435
- decorated.sort((a, b) => {
2436
- return a.priority - b.priority || compareClassNames(a.rule.className, b.rule.className);
2440
+ const priority = computeRulePriority(rule);
2441
+ return { rule, priority, key: ruleSortKey(priority, rule.className, rule.mediaQuery) };
2437
2442
  });
2438
- return decorated;
2443
+ decorated.sort((a, b) => compareRuleSortKeys(a.key, b.key));
2444
+ return decorated.map((d) => ({ rule: d.rule, priority: d.priority }));
2439
2445
  }
2440
2446
  function compareClassNames(a, b) {
2441
2447
  return a < b ? -1 : a > b ? 1 : 0;
2442
2448
  }
2449
+ function parseWidthInterval(prelude) {
2450
+ if (/,|\bnot\b|\bor\b|[<>]/.test(prelude)) return null;
2451
+ const terms = Array.from(prelude.matchAll(/\((min|max)-width:\s*([^)]*)\)/g));
2452
+ if (terms.length === 0) return null;
2453
+ let lo = 0;
2454
+ let hi = Infinity;
2455
+ for (const term of terms) {
2456
+ const px = parsePxLength(term[2]);
2457
+ if (px === null) return null;
2458
+ if (term[1] === "min") {
2459
+ lo = Math.max(lo, px);
2460
+ } else {
2461
+ hi = Math.min(hi, px);
2462
+ }
2463
+ }
2464
+ return { lo, hi };
2465
+ }
2466
+ function parsePxLength(value) {
2467
+ const match = value.trim().match(/^(\d+(?:\.\d+)?)(px)?$/);
2468
+ if (!match) return null;
2469
+ if (match[2] === void 0 && Number(match[1]) !== 0) return null;
2470
+ return Number(match[1]);
2471
+ }
2472
+ function compareWidthIntervals(a, b) {
2473
+ if (a === null || b === null) {
2474
+ return (a === null ? 1 : 0) - (b === null ? 1 : 0);
2475
+ }
2476
+ const widthA = a.hi - a.lo;
2477
+ const widthB = b.hi - b.lo;
2478
+ if (widthA !== widthB) return widthA > widthB ? -1 : 1;
2479
+ return a.lo - b.lo;
2480
+ }
2443
2481
 
2444
2482
  // src/plugin/emit-css.ts
2445
2483
  function collectAtomicRules(chains, mapping) {
@@ -3479,11 +3517,14 @@ function mergeTrussCss(sources) {
3479
3517
  }
3480
3518
  allArbitraryCssBlocks.push(...source.arbitraryCssBlocks);
3481
3519
  }
3482
- allRules.sort((a, b) => a.priority - b.priority || compareClassNames(a.className, b.className));
3520
+ const decorated = allRules.map((rule) => {
3521
+ return { rule, key: ruleSortKey(rule.priority, rule.className, atRulePrelude(rule.cssText)) };
3522
+ });
3523
+ decorated.sort((a, b) => compareRuleSortKeys(a.key, b.key));
3483
3524
  const lines = [];
3484
- for (const rule of allRules) {
3485
- lines.push(`/* @truss p:${rule.priority} c:${rule.className} */`);
3486
- lines.push(rule.cssText);
3525
+ for (const entry of decorated) {
3526
+ lines.push(`/* @truss p:${entry.rule.priority} c:${entry.rule.className} */`);
3527
+ lines.push(entry.rule.cssText);
3487
3528
  }
3488
3529
  for (const prop of allProperties) {
3489
3530
  lines.push(`/* @truss @property */`);
@@ -3494,6 +3535,11 @@ function mergeTrussCss(sources) {
3494
3535
  }
3495
3536
  return lines.join("\n");
3496
3537
  }
3538
+ function atRulePrelude(cssText) {
3539
+ if (!cssText.startsWith("@")) return void 0;
3540
+ const brace = cssText.indexOf("{");
3541
+ return brace === -1 ? void 0 : cssText.slice(0, brace).trim();
3542
+ }
3497
3543
 
3498
3544
  // src/plugin/transform-session.ts
3499
3545
  function createTrussTransformSession(options) {