mnfst 0.5.311 → 0.5.313

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.
@@ -2450,6 +2450,16 @@ TailwindCompiler.prototype.parseClassName = function (className) {
2450
2450
  // Unrecognised @-form falls through to the warning below.
2451
2451
  }
2452
2452
 
2453
+ // Breakpoint ranges (Tailwind v4): max-md → width < md, min-md → width >= md, plus min-/max-[size]
2454
+ const rangeMatch = variant.match(/^(min|max)-(?:\[(.+)\]|(sm|md|lg|xl|2xl))$/);
2455
+ if (rangeMatch) {
2456
+ const named = rangeMatch[3] && /\(min-width:\s*([^)]+)\)/.exec(this.variants[rangeMatch[3]] || '');
2457
+ const size = rangeMatch[2] ? rangeMatch[2].replace(/_/g, ' ') : named && named[1].trim();
2458
+ if (size && !/[{};]/.test(size)) {
2459
+ return { name: variant, selector: `@media (width ${rangeMatch[1] === 'max' ? '<' : '>='} ${size})`, isArbitrary: false };
2460
+ }
2461
+ }
2462
+
2453
2463
  // If no match found, warn once per unique token and return null.
2454
2464
  // Deduped so unsupported variants (e.g. container queries like
2455
2465
  // `@[10rem]:`) don't flood the console on every compile pass.
@@ -2474,14 +2484,14 @@ TailwindCompiler.prototype.parseClassName = function (className) {
2474
2484
  // Generate utilities from CSS variables
2475
2485
  TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedData) {
2476
2486
  try {
2477
- const utilities = [];
2487
+ const out = [];
2478
2488
  const generatedRules = new Set(); // Track generated rules to prevent duplicates
2479
2489
  const variables = this.extractThemeVariables(cssText);
2480
2490
  const { classes: usedClasses, variableSuffixes } = usedData;
2481
2491
  // Optional: raw rules + their full-regen order keys (incremental apply places deltas by them)
2482
2492
  const rulesOut = usedData.rulesOut, keysOut = usedData.keysOut;
2483
2493
  let slot = null;
2484
- const emit = (rule, sub) => { utilities.push(rule); if (rulesOut) { rulesOut.push(rule); keysOut.push(slot.concat(slot[3] ? 2 : sub)); } }; // opacity rules order by discovery
2494
+ const emit = (rule, sub, order) => { out.push({ rule, key: ruleKey(rule, order, slot.concat(slot[3] ? 2 : sub)) }); }; // opacity rules order by discovery
2485
2495
 
2486
2496
  if (variables.size === 0) {
2487
2497
  return '';
@@ -2541,6 +2551,7 @@ TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedDa
2541
2551
  let selector = `.${escapeClassName(variantClass)}`;
2542
2552
  let hasMediaQuery = false;
2543
2553
  let mediaQueryRule = '';
2554
+ const atRules = [];
2544
2555
 
2545
2556
  for (const variant of parsed.variants) {
2546
2557
  if (variant.isArbitrary) {
@@ -2566,9 +2577,10 @@ TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedDa
2566
2577
  // For pseudo-classes, append to selector
2567
2578
  selector = `${selector}${variant.selector}`;
2568
2579
  } else if (variant.selector.startsWith('@')) {
2569
- // For media queries, wrap the whole rule
2580
+ // At-rules wrap the rule; stacked ones nest outer to inner (md:max-lg:)
2570
2581
  hasMediaQuery = true;
2571
- mediaQueryRule = variant.selector;
2582
+ atRules.push(variant.selector);
2583
+ mediaQueryRule = atRules[0];
2572
2584
  }
2573
2585
  }
2574
2586
 
@@ -2582,12 +2594,10 @@ TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedDa
2582
2594
  rule = `${selector} { ${cssContent} }`;
2583
2595
  }
2584
2596
 
2585
- const finalRule = hasMediaQuery ?
2586
- `${mediaQueryRule} { ${rule} }` :
2587
- rule;
2597
+ const finalRule = hasMediaQuery ? wrapAtRules(atRules, rule) : rule;
2588
2598
 
2589
2599
  if (!generatedRules.has(finalRule)) {
2590
- emit(finalRule, 2);
2600
+ emit(finalRule, 2, mediaOrder(parsed.variants));
2591
2601
  generatedRules.add(finalRule);
2592
2602
  }
2593
2603
  }
@@ -2665,13 +2675,17 @@ TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedDa
2665
2675
  }
2666
2676
  }
2667
2677
 
2668
- return utilities.join('\n');
2678
+ return flushEntries(out, rulesOut, keysOut);
2669
2679
  } catch (error) {
2670
2680
  console.error('Error generating utilities:', error);
2671
2681
  return '';
2672
2682
  }
2673
2683
  };
2674
2684
 
2685
+ function wrapAtRules(atRules, rule) {
2686
+ return atRules.reduceRight((inner, at) => `${at} { ${inner} }`, rule);
2687
+ }
2688
+
2675
2689
  // Responsive display ordering: every breakpoint display class sits at 0,2,0, in breakpoint order,
2676
2690
  // so it beats an unprefixed `hidden` from any sheet and larger breakpoints still win (col md:row lg:hidden)
2677
2691
  const BUILTIN_DISPLAY_CLASSES = new Set(['row', 'row-wrap', 'col', 'col-wrap', 'trailing']);
@@ -2685,10 +2699,71 @@ const TAILWIND_DISPLAY = {
2685
2699
  'table-header-group': 'table-header-group', 'table-row-group': 'table-row-group', 'table-row': 'table-row'
2686
2700
  };
2687
2701
 
2702
+ // Breakpoint name (sm, min-sm, max-sm) → { px, max }; null if not a breakpoint
2703
+ function breakpointOf(name) {
2704
+ const max = name.startsWith('max-');
2705
+ const base = max ? name.slice(4) : name.startsWith('min-') ? name.slice(4) : name;
2706
+ return Object.prototype.hasOwnProperty.call(BREAKPOINT_PX, base) ? { px: BREAKPOINT_PX[base], max } : null;
2707
+ }
2708
+
2688
2709
  // max-* widest first, then min-width ascending (Tailwind's order)
2689
2710
  function breakpointRank(name) {
2690
- if (name.startsWith('max-')) return -BREAKPOINT_PX[name.slice(4)];
2691
- return BREAKPOINT_PX[name];
2711
+ const bp = breakpointOf(name);
2712
+ return bp.max ? -bp.px : bp.px;
2713
+ }
2714
+
2715
+ // ---- @media rule order (Tailwind v4): early media, then breakpoints by rank, then other media ----
2716
+ const NO_RANK = -1e6;
2717
+ const EARLY_MEDIA_VARIANTS = new Set([
2718
+ 'motion-safe', 'motion-reduce', 'contrast-more', 'contrast-less',
2719
+ 'prefers-reduced-motion', 'prefers-reduced-motion-no-preference',
2720
+ 'prefers-contrast', 'prefers-contrast-less', 'prefers-contrast-no-preference'
2721
+ ]);
2722
+
2723
+ // Named or arbitrary range variant → rank (max-* negative, min-width positive); null if not a range
2724
+ function rangeRank(name) {
2725
+ if (!name) return null;
2726
+ if (breakpointOf(name)) return breakpointRank(name);
2727
+ const m = /^(min|max)-\[(.+)\]$/.exec(name);
2728
+ if (!m) return null;
2729
+ const u = /^(-?\d*\.?\d+)(px|rem|em)$/.exec(m[2].trim());
2730
+ const px = u ? parseFloat(u[1]) * (u[2] === 'px' ? 1 : 16) : null;
2731
+ if (m[1] === 'max') return px === null ? -1e5 : -px;
2732
+ return px === null ? 1e5 : px;
2733
+ }
2734
+
2735
+ // [band, r1, r2, extra] for an @media rule built from these variants; the highest-order variant decides,
2736
+ // extra lower-order media variants sort after (md: before motion-safe:md:)
2737
+ function mediaOrder(variants) {
2738
+ let band = -1, extra = 0;
2739
+ const ranks = [];
2740
+ for (const v of variants || []) {
2741
+ if (!v || typeof v.selector !== 'string' || !v.selector.startsWith('@media')) continue;
2742
+ const r = rangeRank(v.name);
2743
+ if (r !== null) { ranks.push(r); band = Math.max(band, 1); }
2744
+ else { band = Math.max(band, EARLY_MEDIA_VARIANTS.has(v.name) ? 0 : 2); extra++; }
2745
+ }
2746
+ ranks.sort((a, b) => b - a);
2747
+ if (band === 1) extra = Math.min(extra, 1);
2748
+ else extra = 0;
2749
+ return [band < 0 ? 2 : band, ranks.length ? ranks[0] : NO_RANK, ranks.length > 1 ? ranks[1] : NO_RANK, extra];
2750
+ }
2751
+
2752
+ // Full order key: [media, band, r1, r2, extra, section, var/entry, pair/array, opacity, sub]
2753
+ function ruleKey(rule, order, slotKey) {
2754
+ const head = isMediaRule(rule) ? [1].concat(order || [2, NO_RANK, NO_RANK, 0]) : [0, 0, 0, 0, 0];
2755
+ return head.concat(slotKey);
2756
+ }
2757
+
2758
+ function sortEntries(entries) {
2759
+ return entries.slice().sort((a, b) => compareKeys(a.key, b.key));
2760
+ }
2761
+
2762
+ // Sorted text; raw rules + keys go to rulesOut/keysOut when the caller asked for them
2763
+ function flushEntries(out, rulesOut, keysOut) {
2764
+ const sorted = sortEntries(out);
2765
+ if (rulesOut) for (const e of sorted) { rulesOut.push(e.rule); keysOut.push(e.key); }
2766
+ return sorted.map(e => e.rule).join('\n');
2692
2767
  }
2693
2768
 
2694
2769
  // Top-level display declarations only (nested blocks ignored)
@@ -2711,7 +2786,7 @@ function withoutTopLevelDisplay(css) {
2711
2786
 
2712
2787
  function responsiveDisplayDecl(baseClass, parsed, css) {
2713
2788
  if (!BUILTIN_DISPLAY_CLASSES.has(baseClass) || parsed.variants.length !== 1) return null;
2714
- if (!Object.prototype.hasOwnProperty.call(BREAKPOINT_PX, parsed.variants[0].name)) return null;
2789
+ if (!breakpointOf(parsed.variants[0].name)) return null;
2715
2790
  return topLevelDisplay(css);
2716
2791
  }
2717
2792
 
@@ -2721,14 +2796,12 @@ function tailwindResponsiveDisplay(usedClasses) {
2721
2796
  for (const cls of usedClasses) {
2722
2797
  const parts = cls.split(':');
2723
2798
  if (parts.length !== 2 || !Object.prototype.hasOwnProperty.call(TAILWIND_DISPLAY, parts[1])) continue;
2724
- const bp = parts[0];
2725
- const max = bp.startsWith('max-');
2726
- const px = BREAKPOINT_PX[max ? bp.slice(4) : bp];
2727
- if (!px) continue;
2799
+ const bp = breakpointOf(parts[0]);
2800
+ if (!bp) continue;
2728
2801
  out.push({
2729
2802
  cls,
2730
- media: max ? `@media (width < ${px}px)` : `@media (min-width: ${px}px)`,
2731
- rank: breakpointRank(bp),
2803
+ media: bp.max ? `@media (width < ${bp.px}px)` : `@media (min-width: ${bp.px}px)`,
2804
+ rank: breakpointRank(parts[0]),
2732
2805
  css: TAILWIND_DISPLAY[parts[1]]
2733
2806
  });
2734
2807
  }
@@ -2738,12 +2811,12 @@ function tailwindResponsiveDisplay(usedClasses) {
2738
2811
  // Generate custom utilities from discovered custom utility classes
2739
2812
  TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
2740
2813
  try {
2741
- const utilities = [];
2814
+ const out = [];
2742
2815
  const generatedRules = new Set();
2743
2816
  const { classes: usedClasses } = usedData;
2744
2817
  const rulesOut = usedData.rulesOut, keysOut = usedData.keysOut;
2745
2818
  let slot = null;
2746
- const emit = (rule, sub) => { utilities.push(rule); if (rulesOut) { rulesOut.push(rule); keysOut.push(slot.concat(sub)); } };
2819
+ const emit = (rule, sub, order) => { out.push({ rule, key: ruleKey(rule, order, slot.concat(sub)) }); };
2747
2820
  const responsiveDisplay = [];
2748
2821
 
2749
2822
  // Helper to clean up [object Object] from CSS strings
@@ -2924,6 +2997,7 @@ TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
2924
2997
  let selector = `.${escapeClassName(variantClass)}`;
2925
2998
  let hasMediaQuery = false;
2926
2999
  let mediaQueryRule = '';
3000
+ const atRules = [];
2927
3001
  let nestedSelector = null;
2928
3002
 
2929
3003
  for (const variant of parsed.variants) {
@@ -2950,9 +3024,10 @@ TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
2950
3024
  // For pseudo-classes, append to selector
2951
3025
  selector = `${selector}${variant.selector}`;
2952
3026
  } else if (variant.selector.startsWith('@')) {
2953
- // For media queries, wrap the whole rule
3027
+ // At-rules wrap the rule; stacked ones nest outer to inner (md:max-lg:)
2954
3028
  hasMediaQuery = true;
2955
- mediaQueryRule = variant.selector;
3029
+ atRules.push(variant.selector);
3030
+ mediaQueryRule = atRules[0];
2956
3031
  }
2957
3032
  }
2958
3033
 
@@ -3078,14 +3153,14 @@ TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
3078
3153
  if (typeof rule === 'string' && rule.trim().startsWith('@media')) {
3079
3154
  finalRule = rule;
3080
3155
  } else {
3081
- finalRule = `${mediaQueryRule} { ${rule} }`;
3156
+ finalRule = wrapAtRules(atRules, rule);
3082
3157
  }
3083
3158
  } else {
3084
3159
  finalRule = rule;
3085
3160
  }
3086
3161
 
3087
3162
  if (!generatedRules.has(finalRule)) {
3088
- emit(finalRule, 2);
3163
+ emit(finalRule, 2, mediaOrder(parsed.variants));
3089
3164
  generatedRules.add(finalRule);
3090
3165
  }
3091
3166
  }
@@ -3178,51 +3253,34 @@ TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
3178
3253
  const rule = `${e.media} { ${own}${own} { display: ${e.css} } }`;
3179
3254
  if (generatedRules.has(rule)) continue;
3180
3255
  generatedRules.add(rule);
3181
- slot = [2, e.rank, 0, 0];
3182
- emit(rule, 2);
3256
+ slot = [2, 0, 0, 0];
3257
+ emit(rule, 2, [1, e.rank, NO_RANK, 0]);
3183
3258
  }
3184
3259
  }
3185
3260
 
3186
- return utilities.join('\n');
3261
+ return flushEntries(out, rulesOut, keysOut);
3187
3262
  } catch (error) {
3188
3263
  console.error('Error generating custom utilities:', error);
3189
3264
  return '';
3190
3265
  }
3191
3266
  };
3192
3267
 
3193
- // Split generated CSS into top-level rules (each starts with . or an at-rule)
3194
- TailwindCompiler.prototype.splitUtilityRules = function (utilitiesText) {
3195
- const rules = [];
3196
- let currentRule = '';
3197
- for (const line of utilitiesText.split('\n')) {
3198
- if (line.trim().match(/^(\.|@media|@layer|@supports)/)) {
3199
- if (currentRule.trim()) rules.push(currentRule.trim());
3200
- currentRule = line;
3201
- } else {
3202
- currentRule += '\n' + line;
3203
- }
3204
- }
3205
- if (currentRule.trim()) rules.push(currentRule.trim());
3206
- return rules;
3268
+ // Rules + order keys for theme and custom utilities, in sheet order (base, then @media by breakpoint).
3269
+ // Full compile, incremental apply, cache text and the node bake all write this order.
3270
+ TailwindCompiler.prototype.generateOrderedUtilities = function (themeCss, usedData) {
3271
+ const data = Object.assign({}, usedData, { rulesOut: [], keysOut: [] });
3272
+ this.generateUtilitiesFromVars(themeCss, data);
3273
+ this.generateCustomUtilities(data);
3274
+ return sortEntries(data.rulesOut.map((rule, i) => ({ rule, key: data.keysOut[i] })));
3207
3275
  };
3208
3276
 
3209
- // Sort utilities so base classes come before variants (@media)
3210
- TailwindCompiler.prototype.sortUtilities = function (utilitiesText) {
3211
- if (!utilitiesText) return '';
3212
- const rules = this.splitUtilityRules(utilitiesText);
3213
- rules.sort((a, b) => {
3214
- const aHasMedia = a.startsWith('@media');
3215
- const bHasMedia = b.startsWith('@media');
3216
- if (!aHasMedia && bHasMedia) return -1;
3217
- if (aHasMedia && !bHasMedia) return 1;
3218
- return 0;
3219
- });
3220
- return rules.join('\n\n');
3277
+ TailwindCompiler.prototype.orderedUtilitiesCss = function (entries) {
3278
+ return entries.map(e => e.rule).join('\n\n');
3221
3279
  };
3222
3280
 
3223
3281
  // ---- Incremental apply: new rules go in through CSSOM, never a whole-sheet rewrite ----
3224
3282
  // Entries mirror the layer's cssRules 1:1, each keyed by where a full regen would emit it
3225
- // ([media, section, var/entry, pair/array, region, sub]); deltas are inserted at that index.
3283
+ // (see ruleKey); deltas are inserted at that index.
3226
3284
 
3227
3285
  const utilitiesCssFrom = (applied) => `@layer utilities {\n${applied.entries.map(e => e.rule).join('\n\n')}\n}`;
3228
3286
  const isMediaRule = (rule) => rule.trim().startsWith('@media');
@@ -3232,16 +3290,9 @@ function compareKeys(a, b) {
3232
3290
  return 0;
3233
3291
  }
3234
3292
 
3235
- // Raw rules + order keys for a class set, as a full regen would emit them (base before @media, stable)
3293
+ // Raw rules + order keys for a class set, as a full regen would emit them
3236
3294
  TailwindCompiler.prototype.generateKeyedUtilities = function (themeCss, classes) {
3237
- const usedData = { classes, variableSuffixes: [], rulesOut: [], keysOut: [] };
3238
- this.generateUtilitiesFromVars(themeCss, usedData);
3239
- this.generateCustomUtilities(usedData);
3240
- const out = usedData.rulesOut.map((rule, i) => {
3241
- const media = isMediaRule(rule) ? 1 : 0;
3242
- return { rule, key: [media].concat(usedData.keysOut[i]) };
3243
- });
3244
- return out.filter(e => !e.key[0]).concat(out.filter(e => e.key[0]));
3295
+ return this.generateOrderedUtilities(themeCss, { classes, variableSuffixes: [] });
3245
3296
  };
3246
3297
 
3247
3298
  TailwindCompiler.prototype.utilitiesLayerRule = function () {
@@ -3300,7 +3351,11 @@ TailwindCompiler.prototype.compileDelta = function (themeCss) {
3300
3351
  const candidates = [];
3301
3352
  for (const cls of this.dynamicClassCache) if (!applied.classes.has(cls)) candidates.push(cls);
3302
3353
  const classes = this.filterStaticallyCoveredClasses(candidates);
3354
+ // Tailwind display classes at a breakpoint join the display block, which only a full regen emits
3355
+ if (tailwindResponsiveDisplay(classes).length) return false;
3303
3356
  const fresh = classes.length ? this.generateKeyedUtilities(themeCss, classes).filter(e => !applied.rules.has(e.rule)) : [];
3357
+ // The display block's contents depend on every used class: a new block rule means a full regen
3358
+ if (fresh.some(e => e.key[5] === 2)) return false;
3304
3359
  const entries = applied.entries;
3305
3360
 
3306
3361
  // Place every rule first; bail before touching the sheet if any is ambiguous
@@ -3308,9 +3363,10 @@ TailwindCompiler.prototype.compileDelta = function (themeCss) {
3308
3363
  for (const e of fresh) {
3309
3364
  let lo = 0, hi = entries.length;
3310
3365
  while (lo < hi) { const mid = (lo + hi) >> 1; if (compareKeys(entries[mid].key, e.key) <= 0) lo = mid + 1; else hi = mid; }
3311
- // Same utility, same group, both variants: full regen orders them by discovery
3312
- if (e.key[5] === 2) {
3313
- const sameSlot = (x) => x && compareKeys(x.key.slice(0, 5), e.key.slice(0, 5)) === 0 && x.key[5] === 2;
3366
+ // Same utility, same media rank, both variants: full regen orders them by discovery
3367
+ const last = e.key.length - 1;
3368
+ if (e.key[last] === 2) {
3369
+ const sameSlot = (x) => x && compareKeys(x.key.slice(0, last), e.key.slice(0, last)) === 0 && x.key[last] === 2;
3314
3370
  if (sameSlot(entries[lo - 1]) || sameSlot(entries[lo]) || plan.some(q => sameSlot(q.e))) return false;
3315
3371
  }
3316
3372
  plan.push({ e, at: lo });
@@ -3514,14 +3570,8 @@ TailwindCompiler.prototype.compile = async function () {
3514
3570
  }
3515
3571
  }
3516
3572
 
3517
- // Generate both variable-based and custom utilities
3518
- const varUtilities = this.generateUtilitiesFromVars(themeCss, staticUsedData);
3519
- const customUtilitiesGenerated = this.generateCustomUtilities(staticUsedData);
3573
+ const allUtilities = this.orderedUtilitiesCss(this.generateOrderedUtilities(themeCss, staticUsedData));
3520
3574
 
3521
- let allUtilities = [varUtilities, customUtilitiesGenerated].filter(Boolean).join('\n\n');
3522
- // Sort utilities so base classes come before variants
3523
- allUtilities = this.sortUtilities(allUtilities);
3524
-
3525
3575
  if (allUtilities) {
3526
3576
  const finalCss = `@layer utilities {\n${allUtilities}\n}`;
3527
3577
 
@@ -3605,16 +3655,9 @@ TailwindCompiler.prototype.compile = async function () {
3605
3655
  // Generate utilities for all classes (static + dynamic) if needed
3606
3656
  if (hasVariableChanges || dynamicClassesHash !== this.lastClassesHash) {
3607
3657
 
3608
- // Generate both variable-based and custom utilities
3609
- const rulesOut = [], keysOut = [];
3610
- const keyedData = Object.assign({}, usedData, { rulesOut, keysOut });
3611
- const varUtilities = this.generateUtilitiesFromVars(themeCss, keyedData);
3612
- const customUtilitiesGenerated = this.generateCustomUtilities(keyedData);
3658
+ const entries = this.generateOrderedUtilities(themeCss, usedData);
3659
+ const allUtilities = this.orderedUtilitiesCss(entries);
3613
3660
 
3614
- let allUtilities = [varUtilities, customUtilitiesGenerated].filter(Boolean).join('\n\n');
3615
- // Sort utilities so base classes come before variants
3616
- allUtilities = this.sortUtilities(allUtilities);
3617
-
3618
3661
  if (allUtilities) {
3619
3662
  const finalCss = `@layer utilities {\n${allUtilities}\n}`;
3620
3663
 
@@ -3635,8 +3678,7 @@ TailwindCompiler.prototype.compile = async function () {
3635
3678
  });
3636
3679
  });
3637
3680
  this.lastClassesHash = dynamicClassesHash;
3638
- const entries = rulesOut.map((rule, i) => ({ rule, key: [isMediaRule(rule) ? 1 : 0].concat(keysOut[i]) }));
3639
- this.recordAppliedUtilities(entries.filter(e => !e.key[0]).concat(entries.filter(e => e.key[0])), themeCss, dynamicClasses);
3681
+ this.recordAppliedUtilities(entries, themeCss, dynamicClasses);
3640
3682
  // Classes that arrived during the await above still need a pass
3641
3683
  if (this.dynamicClassCache.size > dynamicClasses.length) this.compile();
3642
3684