mnfst 0.5.312 → 0.5.314

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.
@@ -335,9 +335,6 @@ function createVariants() {
335
335
  'xl': '@media (min-width: 1280px)',
336
336
  '2xl': '@media (min-width: 1536px)',
337
337
 
338
- // Supports queries
339
- 'supports': '@supports',
340
-
341
338
  // Starting style
342
339
  'starting': '@starting-style',
343
340
 
@@ -879,7 +876,7 @@ TailwindCompiler.prototype.addCriticalBlockingStylesSync = function () {
879
876
  };
880
877
  } else {
881
878
  // Fall back to the cache (only generate what was used before)
882
- const cached = localStorage.getItem('tailwind-cache');
879
+ const cached = localStorage.getItem(UTILITIES_CACHE_KEY);
883
880
  let cachedClasses = new Set();
884
881
 
885
882
  if (cached) {
@@ -1114,11 +1111,20 @@ TailwindCompiler.prototype.generateSynchronousUtilities = function () {
1114
1111
  // Cache management
1115
1112
  // Methods for loading, saving, and managing cached utilities
1116
1113
 
1114
+ // Bump when generated rule order changes: older sheets would replay in the old order
1115
+ const UTILITIES_CACHE_KEY = 'tailwind-cache:v2';
1116
+ const LEGACY_CACHE_KEYS = ['tailwind-cache'];
1117
+
1118
+ function dropLegacyUtilitiesCache() {
1119
+ for (const k of LEGACY_CACHE_KEYS) { try { localStorage.removeItem(k); } catch (_) { } }
1120
+ }
1121
+
1117
1122
  // Load and apply cached utilities
1118
1123
  TailwindCompiler.prototype.loadAndApplyCache = function () {
1119
1124
  const cacheStart = performance.now();
1125
+ dropLegacyUtilitiesCache();
1120
1126
  try {
1121
- const cached = localStorage.getItem('tailwind-cache');
1127
+ const cached = localStorage.getItem(UTILITIES_CACHE_KEY);
1122
1128
  if (cached) {
1123
1129
  const parsed = JSON.parse(cached);
1124
1130
  this.cache = new Map(Object.entries(parsed));
@@ -1239,7 +1245,7 @@ TailwindCompiler.prototype.savePersistentCache = function () {
1239
1245
  while (this.cache.size > 0 && attempts < 4) {
1240
1246
  try {
1241
1247
  const serialized = JSON.stringify(Object.fromEntries(this.cache));
1242
- localStorage.setItem('tailwind-cache', serialized);
1248
+ localStorage.setItem(UTILITIES_CACHE_KEY, serialized);
1243
1249
  return;
1244
1250
  } catch (error) {
1245
1251
  // QuotaExceededError (name varies by browser): drop the oldest
@@ -1262,13 +1268,14 @@ TailwindCompiler.prototype.savePersistentCache = function () {
1262
1268
  // Last resort: cache is unusable in this origin right now, clear the slot
1263
1269
  // so the next session starts clean. This is a perf optimization, not
1264
1270
  // correctness — drop quietly.
1265
- try { localStorage.removeItem('tailwind-cache'); } catch (_) {}
1271
+ try { localStorage.removeItem(UTILITIES_CACHE_KEY); } catch (_) {}
1266
1272
  };
1267
1273
 
1268
1274
  // Load cache from localStorage
1269
1275
  TailwindCompiler.prototype.loadPersistentCache = function () {
1276
+ dropLegacyUtilitiesCache();
1270
1277
  try {
1271
- const cached = localStorage.getItem('tailwind-cache');
1278
+ const cached = localStorage.getItem(UTILITIES_CACHE_KEY);
1272
1279
  if (cached) {
1273
1280
  const parsed = JSON.parse(cached);
1274
1281
  this.cache = new Map(Object.entries(parsed));
@@ -2484,14 +2491,14 @@ TailwindCompiler.prototype.parseClassName = function (className) {
2484
2491
  // Generate utilities from CSS variables
2485
2492
  TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedData) {
2486
2493
  try {
2487
- const utilities = [];
2494
+ const out = [];
2488
2495
  const generatedRules = new Set(); // Track generated rules to prevent duplicates
2489
2496
  const variables = this.extractThemeVariables(cssText);
2490
2497
  const { classes: usedClasses, variableSuffixes } = usedData;
2491
2498
  // Optional: raw rules + their full-regen order keys (incremental apply places deltas by them)
2492
2499
  const rulesOut = usedData.rulesOut, keysOut = usedData.keysOut;
2493
2500
  let slot = null;
2494
- 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
2501
+ const emit = (rule, sub, order) => { out.push({ rule, key: ruleKey(rule, order, slot.concat(slot[3] ? 2 : sub)) }); }; // opacity rules order by discovery
2495
2502
 
2496
2503
  if (variables.size === 0) {
2497
2504
  return '';
@@ -2597,7 +2604,7 @@ TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedDa
2597
2604
  const finalRule = hasMediaQuery ? wrapAtRules(atRules, rule) : rule;
2598
2605
 
2599
2606
  if (!generatedRules.has(finalRule)) {
2600
- emit(finalRule, 2);
2607
+ emit(finalRule, 2, atRuleOrder(parsed.variants));
2601
2608
  generatedRules.add(finalRule);
2602
2609
  }
2603
2610
  }
@@ -2675,7 +2682,7 @@ TailwindCompiler.prototype.generateUtilitiesFromVars = function (cssText, usedDa
2675
2682
  }
2676
2683
  }
2677
2684
 
2678
- return utilities.join('\n');
2685
+ return flushEntries(out, rulesOut, keysOut);
2679
2686
  } catch (error) {
2680
2687
  console.error('Error generating utilities:', error);
2681
2688
  return '';
@@ -2712,6 +2719,76 @@ function breakpointRank(name) {
2712
2719
  return bp.max ? -bp.px : bp.px;
2713
2720
  }
2714
2721
 
2722
+ // ---- At-rule order (Tailwind v4 variant order; the highest-order variant decides, then the next) ----
2723
+ const NO_RANK = -1e9;
2724
+ const ORDER_STEP = 1e7;
2725
+ const BREAKPOINT_ORDER = 10;
2726
+ const MEDIA_ORDER = [
2727
+ [/prefers-reduced-motion:\s*no-preference/, 1], [/prefers-reduced-motion:\s*reduce/, 2],
2728
+ [/prefers-contrast:\s*more/, 3], [/prefers-contrast:\s*less/, 4],
2729
+ [/orientation:\s*portrait/, 21], [/orientation:\s*landscape/, 22], [/prefers-color-scheme/, 23],
2730
+ [/^@media print/, 25], [/forced-colors/, 26], [/inverted-colors/, 27],
2731
+ [/\(pointer:\s*none/, 28], [/\(pointer:\s*coarse/, 29], [/\(pointer:\s*fine/, 30],
2732
+ [/any-pointer:\s*none/, 31], [/any-pointer:\s*coarse/, 32], [/any-pointer:\s*fine/, 33],
2733
+ [/scripting:\s*none/, 34]
2734
+ ];
2735
+
2736
+ function toPx(size) {
2737
+ const u = /^(-?\d*\.?\d+)(px|rem|em)$/.exec(size.trim());
2738
+ return u ? parseFloat(u[1]) * (u[2] === 'px' ? 1 : 16) : null;
2739
+ }
2740
+
2741
+ // Width condition → rank (max widest first, then min ascending); null if none
2742
+ function widthRank(selector) {
2743
+ let m = /\((?:min-width:\s*|width\s*>=\s*)([^)]+)\)/.exec(selector);
2744
+ if (m) { const px = toPx(m[1]); return px === null ? 1e5 : px; }
2745
+ m = /\((?:max-width:\s*|width\s*<\s*)([^)]+)\)/.exec(selector);
2746
+ if (m) { const px = toPx(m[1]); return px === null ? -1e5 : -px; }
2747
+ return null;
2748
+ }
2749
+
2750
+ // One at-rule variant's place in Tailwind's order; null for selector variants
2751
+ function variantOrder(selector) {
2752
+ if (typeof selector !== 'string' || !selector.startsWith('@')) return null;
2753
+ if (selector.startsWith('@supports')) return 0;
2754
+ if (selector.startsWith('@container')) return 20 * ORDER_STEP + (widthRank(selector) || 0);
2755
+ if (selector.startsWith('@starting-style')) return 24 * ORDER_STEP;
2756
+ if (!selector.startsWith('@media')) return 40 * ORDER_STEP;
2757
+ const r = widthRank(selector);
2758
+ if (r !== null) return BREAKPOINT_ORDER * ORDER_STEP + r;
2759
+ for (const [re, n] of MEDIA_ORDER) if (re.test(selector)) return n * ORDER_STEP;
2760
+ return 40 * ORDER_STEP;
2761
+ }
2762
+
2763
+ // Top three at-rule variant orders, highest first (md: before motion-safe:md: before lg:)
2764
+ function atRuleOrder(variants) {
2765
+ const orders = [];
2766
+ for (const v of variants || []) {
2767
+ const o = v && variantOrder(v.selector);
2768
+ if (o !== null && o !== undefined) orders.push(o);
2769
+ }
2770
+ orders.sort((a, b) => b - a);
2771
+ return [0, 1, 2].map(i => i < orders.length ? orders[i] : NO_RANK);
2772
+ }
2773
+
2774
+ // Full order key: [at-rule, o1, o2, o3, section, var/entry, pair/array, opacity, sub]
2775
+ const KEY_SECTION = 4;
2776
+ function ruleKey(rule, order, slotKey) {
2777
+ const head = isAtRule(rule) ? [1].concat(order || [40 * ORDER_STEP, NO_RANK, NO_RANK]) : [0, 0, 0, 0];
2778
+ return head.concat(slotKey);
2779
+ }
2780
+
2781
+ function sortEntries(entries) {
2782
+ return entries.slice().sort((a, b) => compareKeys(a.key, b.key));
2783
+ }
2784
+
2785
+ // Sorted text; raw rules + keys go to rulesOut/keysOut when the caller asked for them
2786
+ function flushEntries(out, rulesOut, keysOut) {
2787
+ const sorted = sortEntries(out);
2788
+ if (rulesOut) for (const e of sorted) { rulesOut.push(e.rule); keysOut.push(e.key); }
2789
+ return sorted.map(e => e.rule).join('\n');
2790
+ }
2791
+
2715
2792
  // Top-level display declarations only (nested blocks ignored)
2716
2793
  function topLevelDisplay(css) {
2717
2794
  let flat = '', depth = 0;
@@ -2757,12 +2834,12 @@ function tailwindResponsiveDisplay(usedClasses) {
2757
2834
  // Generate custom utilities from discovered custom utility classes
2758
2835
  TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
2759
2836
  try {
2760
- const utilities = [];
2837
+ const out = [];
2761
2838
  const generatedRules = new Set();
2762
2839
  const { classes: usedClasses } = usedData;
2763
2840
  const rulesOut = usedData.rulesOut, keysOut = usedData.keysOut;
2764
2841
  let slot = null;
2765
- const emit = (rule, sub) => { utilities.push(rule); if (rulesOut) { rulesOut.push(rule); keysOut.push(slot.concat(sub)); } };
2842
+ const emit = (rule, sub, order) => { out.push({ rule, key: ruleKey(rule, order, slot.concat(sub)) }); };
2766
2843
  const responsiveDisplay = [];
2767
2844
 
2768
2845
  // Helper to clean up [object Object] from CSS strings
@@ -3106,7 +3183,7 @@ TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
3106
3183
  }
3107
3184
 
3108
3185
  if (!generatedRules.has(finalRule)) {
3109
- emit(finalRule, 2);
3186
+ emit(finalRule, 2, atRuleOrder(parsed.variants));
3110
3187
  generatedRules.add(finalRule);
3111
3188
  }
3112
3189
  }
@@ -3199,70 +3276,46 @@ TailwindCompiler.prototype.generateCustomUtilities = function (usedData) {
3199
3276
  const rule = `${e.media} { ${own}${own} { display: ${e.css} } }`;
3200
3277
  if (generatedRules.has(rule)) continue;
3201
3278
  generatedRules.add(rule);
3202
- slot = [2, e.rank, 0, 0];
3203
- emit(rule, 2);
3279
+ slot = [2, 0, 0, 0];
3280
+ emit(rule, 2, [BREAKPOINT_ORDER * ORDER_STEP + e.rank, NO_RANK, NO_RANK]);
3204
3281
  }
3205
3282
  }
3206
3283
 
3207
- return utilities.join('\n');
3284
+ return flushEntries(out, rulesOut, keysOut);
3208
3285
  } catch (error) {
3209
3286
  console.error('Error generating custom utilities:', error);
3210
3287
  return '';
3211
3288
  }
3212
3289
  };
3213
3290
 
3214
- // Split generated CSS into top-level rules (each starts with . or an at-rule)
3215
- TailwindCompiler.prototype.splitUtilityRules = function (utilitiesText) {
3216
- const rules = [];
3217
- let currentRule = '';
3218
- for (const line of utilitiesText.split('\n')) {
3219
- if (line.trim().match(/^(\.|@media|@layer|@supports)/)) {
3220
- if (currentRule.trim()) rules.push(currentRule.trim());
3221
- currentRule = line;
3222
- } else {
3223
- currentRule += '\n' + line;
3224
- }
3225
- }
3226
- if (currentRule.trim()) rules.push(currentRule.trim());
3227
- return rules;
3291
+ // Rules + order keys for theme and custom utilities, in sheet order (base, then at-rules in Tailwind variant order).
3292
+ // Full compile, incremental apply, cache text and the node bake all write this order.
3293
+ TailwindCompiler.prototype.generateOrderedUtilities = function (themeCss, usedData) {
3294
+ const data = Object.assign({}, usedData, { rulesOut: [], keysOut: [] });
3295
+ this.generateUtilitiesFromVars(themeCss, data);
3296
+ this.generateCustomUtilities(data);
3297
+ return sortEntries(data.rulesOut.map((rule, i) => ({ rule, key: data.keysOut[i] })));
3228
3298
  };
3229
3299
 
3230
- // Sort utilities so base classes come before variants (@media)
3231
- TailwindCompiler.prototype.sortUtilities = function (utilitiesText) {
3232
- if (!utilitiesText) return '';
3233
- const rules = this.splitUtilityRules(utilitiesText);
3234
- rules.sort((a, b) => {
3235
- const aHasMedia = a.startsWith('@media');
3236
- const bHasMedia = b.startsWith('@media');
3237
- if (!aHasMedia && bHasMedia) return -1;
3238
- if (aHasMedia && !bHasMedia) return 1;
3239
- return 0;
3240
- });
3241
- return rules.join('\n\n');
3300
+ TailwindCompiler.prototype.orderedUtilitiesCss = function (entries) {
3301
+ return entries.map(e => e.rule).join('\n\n');
3242
3302
  };
3243
3303
 
3244
3304
  // ---- Incremental apply: new rules go in through CSSOM, never a whole-sheet rewrite ----
3245
3305
  // Entries mirror the layer's cssRules 1:1, each keyed by where a full regen would emit it
3246
- // ([media, section, var/entry, pair/array, region, sub]); deltas are inserted at that index.
3306
+ // (see ruleKey); deltas are inserted at that index.
3247
3307
 
3248
3308
  const utilitiesCssFrom = (applied) => `@layer utilities {\n${applied.entries.map(e => e.rule).join('\n\n')}\n}`;
3249
- const isMediaRule = (rule) => rule.trim().startsWith('@media');
3309
+ const isAtRule = (rule) => rule.trim().startsWith('@');
3250
3310
 
3251
3311
  function compareKeys(a, b) {
3252
3312
  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return a[i] - b[i];
3253
3313
  return 0;
3254
3314
  }
3255
3315
 
3256
- // Raw rules + order keys for a class set, as a full regen would emit them (base before @media, stable)
3316
+ // Raw rules + order keys for a class set, as a full regen would emit them
3257
3317
  TailwindCompiler.prototype.generateKeyedUtilities = function (themeCss, classes) {
3258
- const usedData = { classes, variableSuffixes: [], rulesOut: [], keysOut: [] };
3259
- this.generateUtilitiesFromVars(themeCss, usedData);
3260
- this.generateCustomUtilities(usedData);
3261
- const out = usedData.rulesOut.map((rule, i) => {
3262
- const media = isMediaRule(rule) ? 1 : 0;
3263
- return { rule, key: [media].concat(usedData.keysOut[i]) };
3264
- });
3265
- return out.filter(e => !e.key[0]).concat(out.filter(e => e.key[0]));
3318
+ return this.generateOrderedUtilities(themeCss, { classes, variableSuffixes: [] });
3266
3319
  };
3267
3320
 
3268
3321
  TailwindCompiler.prototype.utilitiesLayerRule = function () {
@@ -3281,7 +3334,7 @@ TailwindCompiler.prototype.entriesMatchSheet = function (entries) {
3281
3334
  const layer = this.utilitiesLayerRule();
3282
3335
  if (!layer || layer.cssRules.length !== entries.length) return false;
3283
3336
  for (let i = 0; i < entries.length; i++) {
3284
- if ((layer.cssRules[i] instanceof CSSMediaRule) !== !!entries[i].key[0]) return false;
3337
+ if (!(layer.cssRules[i] instanceof CSSStyleRule) !== !!entries[i].key[0]) return false;
3285
3338
  }
3286
3339
  return true;
3287
3340
  };
@@ -3321,7 +3374,11 @@ TailwindCompiler.prototype.compileDelta = function (themeCss) {
3321
3374
  const candidates = [];
3322
3375
  for (const cls of this.dynamicClassCache) if (!applied.classes.has(cls)) candidates.push(cls);
3323
3376
  const classes = this.filterStaticallyCoveredClasses(candidates);
3377
+ // Tailwind display classes at a breakpoint join the display block, which only a full regen emits
3378
+ if (tailwindResponsiveDisplay(classes).length) return false;
3324
3379
  const fresh = classes.length ? this.generateKeyedUtilities(themeCss, classes).filter(e => !applied.rules.has(e.rule)) : [];
3380
+ // The display block's contents depend on every used class: a new block rule means a full regen
3381
+ if (fresh.some(e => e.key[KEY_SECTION] === 2)) return false;
3325
3382
  const entries = applied.entries;
3326
3383
 
3327
3384
  // Place every rule first; bail before touching the sheet if any is ambiguous
@@ -3329,9 +3386,10 @@ TailwindCompiler.prototype.compileDelta = function (themeCss) {
3329
3386
  for (const e of fresh) {
3330
3387
  let lo = 0, hi = entries.length;
3331
3388
  while (lo < hi) { const mid = (lo + hi) >> 1; if (compareKeys(entries[mid].key, e.key) <= 0) lo = mid + 1; else hi = mid; }
3332
- // Same utility, same group, both variants: full regen orders them by discovery
3333
- if (e.key[5] === 2) {
3334
- const sameSlot = (x) => x && compareKeys(x.key.slice(0, 5), e.key.slice(0, 5)) === 0 && x.key[5] === 2;
3389
+ // Same utility, same media rank, both variants: full regen orders them by discovery
3390
+ const last = e.key.length - 1;
3391
+ if (e.key[last] === 2) {
3392
+ const sameSlot = (x) => x && compareKeys(x.key.slice(0, last), e.key.slice(0, last)) === 0 && x.key[last] === 2;
3335
3393
  if (sameSlot(entries[lo - 1]) || sameSlot(entries[lo]) || plan.some(q => sameSlot(q.e))) return false;
3336
3394
  }
3337
3395
  plan.push({ e, at: lo });
@@ -3535,14 +3593,8 @@ TailwindCompiler.prototype.compile = async function () {
3535
3593
  }
3536
3594
  }
3537
3595
 
3538
- // Generate both variable-based and custom utilities
3539
- const varUtilities = this.generateUtilitiesFromVars(themeCss, staticUsedData);
3540
- const customUtilitiesGenerated = this.generateCustomUtilities(staticUsedData);
3596
+ const allUtilities = this.orderedUtilitiesCss(this.generateOrderedUtilities(themeCss, staticUsedData));
3541
3597
 
3542
- let allUtilities = [varUtilities, customUtilitiesGenerated].filter(Boolean).join('\n\n');
3543
- // Sort utilities so base classes come before variants
3544
- allUtilities = this.sortUtilities(allUtilities);
3545
-
3546
3598
  if (allUtilities) {
3547
3599
  const finalCss = `@layer utilities {\n${allUtilities}\n}`;
3548
3600
 
@@ -3626,16 +3678,9 @@ TailwindCompiler.prototype.compile = async function () {
3626
3678
  // Generate utilities for all classes (static + dynamic) if needed
3627
3679
  if (hasVariableChanges || dynamicClassesHash !== this.lastClassesHash) {
3628
3680
 
3629
- // Generate both variable-based and custom utilities
3630
- const rulesOut = [], keysOut = [];
3631
- const keyedData = Object.assign({}, usedData, { rulesOut, keysOut });
3632
- const varUtilities = this.generateUtilitiesFromVars(themeCss, keyedData);
3633
- const customUtilitiesGenerated = this.generateCustomUtilities(keyedData);
3681
+ const entries = this.generateOrderedUtilities(themeCss, usedData);
3682
+ const allUtilities = this.orderedUtilitiesCss(entries);
3634
3683
 
3635
- let allUtilities = [varUtilities, customUtilitiesGenerated].filter(Boolean).join('\n\n');
3636
- // Sort utilities so base classes come before variants
3637
- allUtilities = this.sortUtilities(allUtilities);
3638
-
3639
3684
  if (allUtilities) {
3640
3685
  const finalCss = `@layer utilities {\n${allUtilities}\n}`;
3641
3686
 
@@ -3656,8 +3701,7 @@ TailwindCompiler.prototype.compile = async function () {
3656
3701
  });
3657
3702
  });
3658
3703
  this.lastClassesHash = dynamicClassesHash;
3659
- const entries = rulesOut.map((rule, i) => ({ rule, key: [isMediaRule(rule) ? 1 : 0].concat(keysOut[i]) }));
3660
- this.recordAppliedUtilities(entries.filter(e => !e.key[0]).concat(entries.filter(e => e.key[0])), themeCss, dynamicClasses);
3704
+ this.recordAppliedUtilities(entries, themeCss, dynamicClasses);
3661
3705
  // Classes that arrived during the await above still need a pass
3662
3706
  if (this.dynamicClassCache.size > dynamicClasses.length) this.compile();
3663
3707