@weapp-tailwindcss/postcss 2.2.1-next.4 → 3.0.0-next.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.
package/dist/index.mjs CHANGED
@@ -264,6 +264,658 @@ function protectDynamicColorMixAlpha(css, options = {}) {
264
264
  };
265
265
  }
266
266
  //#endregion
267
+ //#region src/compat/mini-program-css/at-rules.ts
268
+ const MINI_PROGRAM_UNSUPPORTED_AT_RULES = new Set(["property", "supports"]);
269
+ function removeAtRulesByScan(css, names) {
270
+ let index = 0;
271
+ let result = "";
272
+ const atRulePattern = new RegExp(`@(?:${[...names].join("|")})\\b`, "i");
273
+ while (index < css.length) {
274
+ const match = atRulePattern.exec(css.slice(index));
275
+ if (!match || match.index === void 0) {
276
+ result += css.slice(index);
277
+ break;
278
+ }
279
+ const start = index + match.index;
280
+ result += css.slice(index, start);
281
+ const blockStart = css.indexOf("{", start);
282
+ if (blockStart === -1) {
283
+ result += css.slice(start);
284
+ break;
285
+ }
286
+ let depth = 0;
287
+ let cursor = blockStart;
288
+ for (; cursor < css.length; cursor++) {
289
+ const char = css[cursor];
290
+ if (char === "{") depth++;
291
+ else if (char === "}") {
292
+ depth--;
293
+ if (depth === 0) {
294
+ cursor++;
295
+ break;
296
+ }
297
+ }
298
+ }
299
+ index = cursor;
300
+ }
301
+ return result;
302
+ }
303
+ function removeUnsupportedMiniProgramAtRules(css) {
304
+ try {
305
+ const root = postcss.parse(css);
306
+ root.walkAtRules((atRule) => {
307
+ if (MINI_PROGRAM_UNSUPPORTED_AT_RULES.has(atRule.name)) atRule.remove();
308
+ });
309
+ root.walkAtRules((atRule) => {
310
+ if (atRule.nodes && atRule.nodes.length === 0) atRule.remove();
311
+ });
312
+ return root.toString();
313
+ } catch {
314
+ return removeAtRulesByScan(css, MINI_PROGRAM_UNSUPPORTED_AT_RULES);
315
+ }
316
+ }
317
+ function removeUnsupportedAtSupports(css) {
318
+ return removeUnsupportedMiniProgramAtRules(css);
319
+ }
320
+ /**
321
+ * 移除小程序不支持的 cascade layer 语法,同时保留 layer 内的实际规则。
322
+ */
323
+ function removeUnsupportedCascadeLayers(root) {
324
+ root.walkAtRules("layer", (atRule) => {
325
+ if (!atRule.nodes || atRule.nodes.length === 0) {
326
+ atRule.remove();
327
+ return;
328
+ }
329
+ atRule.replaceWith(...atRule.nodes);
330
+ });
331
+ }
332
+ //#endregion
333
+ //#region src/compat/mini-program-prefixes.ts
334
+ const PRESERVED_WEBKIT_DECLARATION_PROPS = new Set([
335
+ "-webkit-box-orient",
336
+ "-webkit-line-clamp",
337
+ "-webkit-overflow-scrolling",
338
+ "-webkit-text-fill-color",
339
+ "-webkit-text-stroke",
340
+ "-webkit-text-stroke-color",
341
+ "-webkit-text-stroke-width"
342
+ ]);
343
+ const PRESERVED_WEBKIT_VALUE_DECLARATIONS = new Map([["display", new Set(["-webkit-box"])], ["-webkit-background-clip", new Set(["text"])]]);
344
+ const TRANSITION_PROPS = new Set(["transition", "transition-property"]);
345
+ function splitTopLevelCommaList$1(value) {
346
+ const parts = [];
347
+ let start = 0;
348
+ let depth = 0;
349
+ let quote;
350
+ let escaped = false;
351
+ for (let i = 0; i < value.length; i++) {
352
+ const char = value[i];
353
+ if (escaped) {
354
+ escaped = false;
355
+ continue;
356
+ }
357
+ if (char === "\\") {
358
+ escaped = true;
359
+ continue;
360
+ }
361
+ if (quote) {
362
+ if (char === quote) quote = void 0;
363
+ continue;
364
+ }
365
+ if (char === "\"" || char === "'") {
366
+ quote = char;
367
+ continue;
368
+ }
369
+ if (char === "(") {
370
+ depth++;
371
+ continue;
372
+ }
373
+ if (char === ")") {
374
+ depth = Math.max(0, depth - 1);
375
+ continue;
376
+ }
377
+ if (char === "," && depth === 0) {
378
+ parts.push(value.slice(start, i));
379
+ start = i + 1;
380
+ }
381
+ }
382
+ parts.push(value.slice(start));
383
+ return parts;
384
+ }
385
+ function isPreservedWebkitDeclaration(decl) {
386
+ const prop = decl.prop.toLowerCase();
387
+ if (prop.startsWith("-webkit-mask")) return true;
388
+ if (PRESERVED_WEBKIT_DECLARATION_PROPS.has(prop)) return true;
389
+ return PRESERVED_WEBKIT_VALUE_DECLARATIONS.get(prop)?.has(decl.value.trim().toLowerCase()) ?? false;
390
+ }
391
+ function normalizeTransitionValue(value) {
392
+ return splitTopLevelCommaList$1(value).map((part) => part.trim()).filter((part) => part.length > 0 && !part.toLowerCase().startsWith("-webkit-")).join(", ");
393
+ }
394
+ function hasUnsupportedWebkitKeywordValue(decl) {
395
+ const value = decl.value.trim().toLowerCase();
396
+ if (!value.startsWith("-webkit-")) return false;
397
+ if (PRESERVED_WEBKIT_VALUE_DECLARATIONS.get(decl.prop.toLowerCase())?.has(value)) return false;
398
+ return /^-webkit-[\w-]+$/.test(value);
399
+ }
400
+ /**
401
+ * 收敛小程序 CSS 中的 WebKit 前缀,只保留 WXSS 里有实际价值的兼容写法。
402
+ */
403
+ function normalizeMiniProgramPrefixedDeclaration(decl) {
404
+ const prop = decl.prop.toLowerCase();
405
+ if (TRANSITION_PROPS.has(prop) && decl.value.toLowerCase().includes("-webkit-")) {
406
+ const value = normalizeTransitionValue(decl.value);
407
+ if (value.length === 0) {
408
+ decl.remove();
409
+ return;
410
+ }
411
+ decl.value = value;
412
+ }
413
+ if (prop.startsWith("-webkit-") && !isPreservedWebkitDeclaration(decl)) {
414
+ decl.remove();
415
+ return;
416
+ }
417
+ if (hasUnsupportedWebkitKeywordValue(decl)) decl.remove();
418
+ }
419
+ function removeUnsupportedMiniProgramPrefixedAtRule(atRule) {
420
+ if (atRule.name.toLowerCase() === "-webkit-keyframes") atRule.remove();
421
+ }
422
+ //#endregion
423
+ //#region src/compat/mini-program-css/color-gamut.ts
424
+ const DISPLAY_P3_VALUE_RE$1 = /color\(\s*display-p3\b/i;
425
+ const COLOR_GAMUT_P3_RE$1 = /\(\s*color-gamut\s*:\s*p3\s*\)/i;
426
+ function isDisplayP3MediaRule(atRule) {
427
+ return atRule.name === "media" && COLOR_GAMUT_P3_RE$1.test(atRule.params);
428
+ }
429
+ function isDisplayP3Declaration(decl) {
430
+ return DISPLAY_P3_VALUE_RE$1.test(decl.value);
431
+ }
432
+ //#endregion
433
+ //#region src/compat/mini-program-css/selectors.ts
434
+ const MINI_PROGRAM_THEME_SCOPE_SELECTOR = ":host,page,.tw-root,wx-root-portal-content";
435
+ const MINI_PROGRAM_PREFLIGHT_SELECTORS = new Set([
436
+ "*",
437
+ "view",
438
+ "text",
439
+ ":before",
440
+ ":after",
441
+ "::before",
442
+ "::after"
443
+ ]);
444
+ const MINI_PROGRAM_THEME_SCOPE_SELECTORS = new Set([
445
+ ":host",
446
+ ":root",
447
+ "page",
448
+ ".tw-root",
449
+ "wx-root-portal-content"
450
+ ]);
451
+ const SPECIFICITY_PLACEHOLDER_SUFFIXES = [":not(#n)", ":not(#\\#)"];
452
+ const MINI_PROGRAM_UNSUPPORTED_BROWSER_SELECTORS = new Set([
453
+ ":-moz-focusring",
454
+ ":-moz-ui-invalid",
455
+ "::-webkit-calendar-picker-indicator",
456
+ "::-webkit-date-and-time-value",
457
+ "::-webkit-datetime-edit",
458
+ "::-webkit-datetime-edit-day-field",
459
+ "::-webkit-datetime-edit-fields-wrapper",
460
+ "::-webkit-datetime-edit-hour-field",
461
+ "::-webkit-datetime-edit-meridiem-field",
462
+ "::-webkit-datetime-edit-millisecond-field",
463
+ "::-webkit-datetime-edit-minute-field",
464
+ "::-webkit-datetime-edit-month-field",
465
+ "::-webkit-datetime-edit-second-field",
466
+ "::-webkit-datetime-edit-year-field",
467
+ "::-webkit-inner-spin-button",
468
+ "::-webkit-input-placeholder",
469
+ "::-webkit-outer-spin-button",
470
+ "::-webkit-search-decoration",
471
+ "::placeholder",
472
+ "[hidden]:where(:not([hidden='until-found']))"
473
+ ]);
474
+ const MINI_PROGRAM_UNSUPPORTED_BROWSER_TAG_SELECTORS = new Set([
475
+ "a",
476
+ "abbr:where([title])",
477
+ "audio",
478
+ "b",
479
+ "button",
480
+ "canvas",
481
+ "code",
482
+ "embed",
483
+ "h1",
484
+ "h2",
485
+ "h3",
486
+ "h4",
487
+ "h5",
488
+ "h6",
489
+ "hr",
490
+ "html",
491
+ "iframe",
492
+ "img",
493
+ "input",
494
+ "input:where([type='button'],[type='reset'],[type='submit'])",
495
+ "kbd",
496
+ "menu",
497
+ "object",
498
+ "ol",
499
+ "optgroup",
500
+ "pre",
501
+ "progress",
502
+ "samp",
503
+ "select",
504
+ "select[multiple]optgroup",
505
+ "select[multiple]optgroupoption",
506
+ "select[size]optgroup",
507
+ "select[size]optgroupoption",
508
+ "small",
509
+ "strong",
510
+ "sub",
511
+ "summary",
512
+ "sup",
513
+ "svg",
514
+ "table",
515
+ "textarea",
516
+ "ul",
517
+ "video"
518
+ ]);
519
+ function normalizeSelector$1(selector) {
520
+ return selector.trim().replace(/\s+/g, "");
521
+ }
522
+ function getRuleSelectors(rule) {
523
+ return rule.selector.split(",").map(normalizeSelector$1).filter(Boolean);
524
+ }
525
+ function isUnsupportedBrowserSelector(selector) {
526
+ const normalized = normalizeSelector$1(selector);
527
+ return MINI_PROGRAM_UNSUPPORTED_BROWSER_SELECTORS.has(normalized) || MINI_PROGRAM_UNSUPPORTED_BROWSER_TAG_SELECTORS.has(normalized);
528
+ }
529
+ function isMiniProgramPreflightSelector(selectors) {
530
+ return selectors.length > 0 && selectors.every((selector) => MINI_PROGRAM_PREFLIGHT_SELECTORS.has(selector)) && selectors.some((selector) => selector === "*" || selector === ":before" || selector === ":after" || selector === "::before" || selector === "::after");
531
+ }
532
+ function isMiniProgramThemeScopeSelector(selectors) {
533
+ return selectors.length > 0 && selectors.every((selector) => MINI_PROGRAM_THEME_SCOPE_SELECTORS.has(selector));
534
+ }
535
+ //#endregion
536
+ //#region src/compat/mini-program-css/predicates.ts
537
+ const PREFLIGHT_RESET_PROPS = new Set([
538
+ "box-sizing",
539
+ "border",
540
+ "border-width",
541
+ "border-style",
542
+ "border-color",
543
+ "margin",
544
+ "padding"
545
+ ]);
546
+ const PSEUDO_CONTENT_SELECTOR_RE = /^(?:::before|::after|:before|:after)(?:,(?:::before|::after|:before|:after))*$/;
547
+ const TW_CONTENT_VAR_RE$1 = /var\(\s*--tw-content\b/;
548
+ function hasTailwindPreflightDeclaration(rule) {
549
+ let hasTailwindVar = false;
550
+ let hasResetProp = false;
551
+ rule.walkDecls((decl) => {
552
+ if (decl.prop.startsWith("--tw-")) hasTailwindVar = true;
553
+ if (PREFLIGHT_RESET_PROPS.has(decl.prop)) hasResetProp = true;
554
+ });
555
+ return hasTailwindVar || hasResetProp;
556
+ }
557
+ function hasTwContentDeclaration(rule) {
558
+ let hasContentInit = false;
559
+ rule.walkDecls("--tw-content", () => {
560
+ hasContentInit = true;
561
+ });
562
+ return hasContentInit;
563
+ }
564
+ function isCustomPropertyRule(rule) {
565
+ let hasDeclaration = false;
566
+ let allCustomProperties = true;
567
+ rule.each((node) => {
568
+ if (node.type !== "decl") return;
569
+ hasDeclaration = true;
570
+ if (!node.prop.startsWith("--")) allCustomProperties = false;
571
+ });
572
+ return hasDeclaration && allCustomProperties;
573
+ }
574
+ function isEmptyTwContentDeclaration(decl) {
575
+ return decl.prop === "--tw-content" && (decl.value === "\"\"" || decl.value === "''");
576
+ }
577
+ function isOnlyTwContentDeclarations(rule) {
578
+ let hasDeclaration = false;
579
+ let onlyContentVariable = true;
580
+ rule.walkDecls((decl) => {
581
+ hasDeclaration = true;
582
+ if (decl.prop !== "--tw-content") onlyContentVariable = false;
583
+ });
584
+ return hasDeclaration && onlyContentVariable;
585
+ }
586
+ function isPseudoContentInitRule(rule) {
587
+ const selector = rule.selector.replace(/\s+/g, "");
588
+ return PSEUDO_CONTENT_SELECTOR_RE.test(selector) && isOnlyTwContentDeclarations(rule);
589
+ }
590
+ function usesTwContentVariable(root) {
591
+ let used = false;
592
+ root.walkDecls((decl) => {
593
+ if (TW_CONTENT_VAR_RE$1.test(decl.value)) used = true;
594
+ });
595
+ return used;
596
+ }
597
+ function isMiniProgramPreflightRule(node) {
598
+ if (node.type !== "rule") return false;
599
+ return isMiniProgramPreflightSelector(getRuleSelectors(node)) && hasTailwindPreflightDeclaration(node);
600
+ }
601
+ function isMiniProgramThemeVariableRule(node) {
602
+ if (node.type !== "rule") return false;
603
+ return isMiniProgramThemeScopeSelector(getRuleSelectors(node)) && isCustomPropertyRule(node);
604
+ }
605
+ //#endregion
606
+ //#region src/compat/mini-program-css/root-cleanups.ts
607
+ function removeSpecificityPlaceholders(root) {
608
+ root.walkRules((rule) => {
609
+ if (!rule.selectors || rule.selectors.length === 0) return;
610
+ let changed = false;
611
+ const selectors = rule.selectors.map((selector) => {
612
+ let next = selector;
613
+ for (const suffix of SPECIFICITY_PLACEHOLDER_SUFFIXES) if (next.includes(suffix)) next = next.split(suffix).join("");
614
+ if (next !== selector) changed = true;
615
+ return next;
616
+ });
617
+ if (changed) rule.selectors = selectors;
618
+ });
619
+ }
620
+ function removeEmptyAtRuleAncestors(parent) {
621
+ while (parent?.type === "atrule" && (!parent.nodes || parent.nodes.length === 0)) {
622
+ const nextParent = parent.parent;
623
+ parent.remove();
624
+ parent = nextParent?.type === "atrule" ? nextParent : void 0;
625
+ }
626
+ }
627
+ function removeUnsupportedBrowserSelectors(root) {
628
+ root.walkRules((rule) => {
629
+ if (!rule.selectors || rule.selectors.length === 0) return;
630
+ const selectors = rule.selectors.filter((selector) => !isUnsupportedBrowserSelector(selector));
631
+ if (selectors.length === rule.selectors.length) return;
632
+ if (selectors.length === 0) {
633
+ const parent = rule.parent;
634
+ rule.remove();
635
+ removeEmptyAtRuleAncestors(parent);
636
+ return;
637
+ }
638
+ rule.selectors = selectors;
639
+ });
640
+ }
641
+ function removeDeclarationAndEmptyRule$1(decl) {
642
+ const parent = decl.parent;
643
+ decl.remove();
644
+ if (parent?.type === "rule" && parent.nodes.length === 0) {
645
+ const ruleParent = parent.parent;
646
+ parent.remove();
647
+ removeEmptyAtRuleAncestors(ruleParent);
648
+ }
649
+ }
650
+ function removeDisplayP3Declarations(root) {
651
+ root.walkAtRules((atRule) => {
652
+ if (isDisplayP3MediaRule(atRule)) {
653
+ const parent = atRule.parent;
654
+ atRule.remove();
655
+ removeEmptyAtRuleAncestors(parent);
656
+ }
657
+ });
658
+ }
659
+ function removeUnsupportedModernColorDeclarations(root) {
660
+ const customPropertyValues = /* @__PURE__ */ new Map();
661
+ root.walkDecls((decl) => {
662
+ if (decl.prop.startsWith("--")) customPropertyValues.set(decl.prop, decl.value.trim());
663
+ });
664
+ root.walkDecls((decl) => {
665
+ const normalized = normalizeModernColorValue(decl.value, customPropertyValues);
666
+ if (normalized.changed) {
667
+ decl.value = normalized.value;
668
+ if (decl.prop.startsWith("--")) customPropertyValues.set(decl.prop, decl.value.trim());
669
+ }
670
+ if (normalized.hasUnsupported) removeDeclarationAndEmptyRule$1(decl);
671
+ });
672
+ }
673
+ //#endregion
674
+ //#region src/compat/mini-program-css/finalize.ts
675
+ const HOIST_ANCHOR_COMMENT = "__weapp_tailwindcss_base_anchor__";
676
+ function createPseudoContentInitRule() {
677
+ const rule = postcss.rule({ selector: "::before,\n::after" });
678
+ rule.append({
679
+ prop: "--tw-content",
680
+ value: "''"
681
+ });
682
+ return rule;
683
+ }
684
+ function collectPreflightRules(root, options = {}) {
685
+ const preflightNodes = [];
686
+ let hasContentInit = false;
687
+ for (const node of root.nodes ?? []) if (isMiniProgramPreflightRule(node)) {
688
+ preflightNodes.push(node);
689
+ if (hasTwContentDeclaration(node)) hasContentInit = true;
690
+ }
691
+ if (preflightNodes.length === 0) return [];
692
+ const clonedPreflightRules = preflightNodes.map((node) => node.clone());
693
+ const contentInitRules = options.preservePseudoContentInit ? clonedPreflightRules.filter((rule) => hasTwContentDeclaration(rule)) : [];
694
+ const otherPreflightRules = clonedPreflightRules.filter((rule) => !hasTwContentDeclaration(rule));
695
+ const preflightRules = hasContentInit ? [...contentInitRules, ...otherPreflightRules] : [...options.preservePseudoContentInit ? [createPseudoContentInitRule()] : [], ...otherPreflightRules];
696
+ for (const node of preflightNodes) node.remove();
697
+ return preflightRules;
698
+ }
699
+ function createPreflightResetRule(cssPreflight) {
700
+ if (!cssPreflight || typeof cssPreflight !== "object") return;
701
+ const rule = postcss.rule({ selector: "view,text,:after,:before" });
702
+ for (const [prop, value] of Object.entries(cssPreflight)) {
703
+ if (value === false) continue;
704
+ rule.append({
705
+ prop,
706
+ value: value.toString()
707
+ });
708
+ }
709
+ return rule.nodes?.length ? rule : void 0;
710
+ }
711
+ function collectThemeVariableRule(root, options = {}) {
712
+ const themeRules = [];
713
+ const declarations = /* @__PURE__ */ new Map();
714
+ const shouldPreserveContentInit = options.preservePseudoContentInit || usesTwContentVariable(root);
715
+ for (const node of root.nodes ?? []) {
716
+ if (!isMiniProgramThemeVariableRule(node)) continue;
717
+ themeRules.push(node);
718
+ node.walkDecls((decl) => {
719
+ if (isDisplayP3Declaration(decl)) return;
720
+ if (!shouldPreserveContentInit && isEmptyTwContentDeclaration(decl)) return;
721
+ declarations.set(decl.prop, decl.clone());
722
+ });
723
+ }
724
+ for (const rule of themeRules) rule.remove();
725
+ if (declarations.size === 0) return;
726
+ const rule = postcss.rule({ selector: MINI_PROGRAM_THEME_SCOPE_SELECTOR });
727
+ for (const decl of declarations.values()) rule.append(decl);
728
+ return rule;
729
+ }
730
+ function getTopDirectiveTail(root) {
731
+ let tail;
732
+ for (const node of root.nodes ?? []) {
733
+ if (node.type === "atrule" && (node.name === "charset" || node.name === "import")) {
734
+ tail = node;
735
+ continue;
736
+ }
737
+ break;
738
+ }
739
+ return tail;
740
+ }
741
+ function createHoistInsertionAnchor(root) {
742
+ for (const node of root.nodes ?? []) if (isMiniProgramPreflightRule(node) || isMiniProgramThemeVariableRule(node)) {
743
+ const anchor = postcss.comment({ text: HOIST_ANCHOR_COMMENT });
744
+ node.before(anchor);
745
+ return anchor;
746
+ }
747
+ }
748
+ function insertHoistedRules(root, rules, anchor) {
749
+ if (anchor && !anchor.parent) anchor = void 0;
750
+ if (rules.length === 0) {
751
+ anchor?.remove();
752
+ return;
753
+ }
754
+ const topDirectiveTail = getTopDirectiveTail(root);
755
+ const firstRule = rules[0];
756
+ if (!firstRule) return;
757
+ if (anchor) {
758
+ if (anchor.raws.before === void 0) delete firstRule.raws.before;
759
+ else firstRule.raws.before = anchor.raws.before;
760
+ anchor.replaceWith(rules);
761
+ return;
762
+ }
763
+ firstRule.raws.before = topDirectiveTail ? "\n" : "";
764
+ if (topDirectiveTail) topDirectiveTail.after(rules);
765
+ else root.prepend(rules);
766
+ }
767
+ function unwrapTailwindSourceMedia(root) {
768
+ root.walkAtRules("media", (atRule) => {
769
+ if (atRule.params.startsWith("source(") && atRule.nodes && atRule.nodes.length > 0) atRule.replaceWith(...atRule.nodes);
770
+ });
771
+ }
772
+ function finalizeMiniProgramCssRoot(root, options = {}) {
773
+ removeUnsupportedCascadeLayers(root);
774
+ unwrapTailwindSourceMedia(root);
775
+ root.walkAtRules("property", (atRule) => {
776
+ atRule.remove();
777
+ });
778
+ removeSpecificityPlaceholders(root);
779
+ removeUnsupportedBrowserSelectors(root);
780
+ removeDisplayP3Declarations(root);
781
+ removeUnsupportedModernColorDeclarations(root);
782
+ root.walkDecls((decl) => {
783
+ normalizeMiniProgramPrefixedDeclaration(decl);
784
+ });
785
+ root.walkAtRules((atRule) => {
786
+ removeUnsupportedMiniProgramPrefixedAtRule(atRule);
787
+ });
788
+ const hoistAnchor = createHoistInsertionAnchor(root);
789
+ const preflightRules = collectPreflightRules(root, options);
790
+ if (preflightRules.length === 0) {
791
+ const resetRule = createPreflightResetRule(options.cssPreflight);
792
+ if (resetRule) preflightRules.push(resetRule);
793
+ }
794
+ const themeRule = collectThemeVariableRule(root, options);
795
+ insertHoistedRules(root, themeRule ? [...preflightRules, themeRule] : preflightRules, hoistAnchor);
796
+ }
797
+ function hoistTailwindPreflightBase(css) {
798
+ try {
799
+ const root = postcss.parse(css);
800
+ insertHoistedRules(root, collectPreflightRules(root, { preservePseudoContentInit: true }));
801
+ return root.toString();
802
+ } catch {
803
+ return css;
804
+ }
805
+ }
806
+ function finalizeMiniProgramCss(css, options = {}) {
807
+ const cleanedCss = removeUnsupportedMiniProgramAtRules(css);
808
+ try {
809
+ const root = postcss.parse(cleanedCss);
810
+ finalizeMiniProgramCssRoot(root, options);
811
+ return root.toString();
812
+ } catch {
813
+ return cleanedCss;
814
+ }
815
+ }
816
+ //#endregion
817
+ //#region src/compat/mini-program-css/prune-generated.ts
818
+ const DEFAULT_WEAPP_VARIABLE_SCOPE = "page,.tw-root,wx-root-portal-content,:host";
819
+ const DEFAULT_WEAPP_ELEMENT_VARIABLE_SCOPE = "view,text,:before,:after";
820
+ const CLASS_SELECTOR_RE$1 = /(?:^|[^\w-])\.[_a-z\u00A0-\uFFFF\\-]/i;
821
+ const MINI_PROGRAM_ELEMENT_VARIABLE_SCOPE_SELECTORS = new Set([
822
+ "view",
823
+ "text",
824
+ ":before",
825
+ ":after",
826
+ "::before",
827
+ "::after"
828
+ ]);
829
+ function hasClassSelector$2(selector) {
830
+ return CLASS_SELECTOR_RE$1.test(selector);
831
+ }
832
+ function removeEmptyContentInitDeclarations(rule) {
833
+ rule.walkDecls((decl) => {
834
+ if (isEmptyTwContentDeclaration(decl)) decl.remove();
835
+ });
836
+ }
837
+ function isMiniProgramElementVariableScopeRule(rule) {
838
+ const selectors = getRuleSelectors(rule);
839
+ return selectors.length > 0 && selectors.every((selector) => MINI_PROGRAM_ELEMENT_VARIABLE_SCOPE_SELECTORS.has(selector));
840
+ }
841
+ function isTailwindV4GradientRuntimeDeclaration(decl) {
842
+ return decl.prop.startsWith("--tw-gradient-");
843
+ }
844
+ function moveTailwindV4GradientRuntimeDeclarations(rule) {
845
+ const gradientDeclarations = [];
846
+ rule.walkDecls((decl) => {
847
+ if (isTailwindV4GradientRuntimeDeclaration(decl)) {
848
+ gradientDeclarations.push(decl.clone());
849
+ decl.remove();
850
+ }
851
+ });
852
+ if (gradientDeclarations.length > 0) rule.before(new postcss.Rule({
853
+ selector: DEFAULT_WEAPP_ELEMENT_VARIABLE_SCOPE,
854
+ nodes: gradientDeclarations
855
+ }));
856
+ if (rule.nodes.length === 0) rule.remove();
857
+ }
858
+ function isKeyframesRule(rule) {
859
+ let parent = rule.parent;
860
+ while (parent) {
861
+ if (parent.type === "atrule" && parent.name.endsWith("keyframes")) return true;
862
+ parent = parent.parent;
863
+ }
864
+ return false;
865
+ }
866
+ /**
867
+ * 裁剪 Tailwind 生成 CSS 中面向浏览器的 classless 规则。
868
+ */
869
+ function pruneMiniProgramGeneratedCss(css, options = {}) {
870
+ const root = postcss.parse(css);
871
+ const shouldPreserveContentInit = options.preservePreflight || usesTwContentVariable(root);
872
+ root.walkComments((comment) => {
873
+ comment.remove();
874
+ });
875
+ removeUnsupportedCascadeLayers(root);
876
+ removeUnsupportedModernColorDeclarations(root);
877
+ root.walkAtRules("supports", (atRule) => {
878
+ atRule.remove();
879
+ });
880
+ root.walkAtRules((atRule) => {
881
+ removeUnsupportedMiniProgramPrefixedAtRule(atRule);
882
+ });
883
+ root.walkDecls((decl) => {
884
+ normalizeMiniProgramPrefixedDeclaration(decl);
885
+ });
886
+ root.walkRules((rule) => {
887
+ if (isKeyframesRule(rule)) return;
888
+ if (isCustomPropertyRule(rule) && isMiniProgramElementVariableScopeRule(rule)) {
889
+ rule.selector = DEFAULT_WEAPP_ELEMENT_VARIABLE_SCOPE;
890
+ return;
891
+ }
892
+ if (isMiniProgramThemeVariableRule(rule)) {
893
+ moveTailwindV4GradientRuntimeDeclarations(rule);
894
+ if (!rule.parent) return;
895
+ rule.selector = DEFAULT_WEAPP_VARIABLE_SCOPE;
896
+ return;
897
+ }
898
+ if (hasClassSelector$2(rule.selector)) return;
899
+ if (!shouldPreserveContentInit) removeEmptyContentInitDeclarations(rule);
900
+ if (isPseudoContentInitRule(rule)) {
901
+ if (!shouldPreserveContentInit) rule.remove();
902
+ return;
903
+ }
904
+ if (options.preservePreflight && isMiniProgramPreflightRule(rule)) return;
905
+ if (isCustomPropertyRule(rule)) {
906
+ moveTailwindV4GradientRuntimeDeclarations(rule);
907
+ if (!rule.parent) return;
908
+ rule.selector = DEFAULT_WEAPP_VARIABLE_SCOPE;
909
+ return;
910
+ }
911
+ rule.remove();
912
+ });
913
+ root.walkAtRules((atRule) => {
914
+ if (!atRule.nodes || atRule.nodes.length === 0) atRule.remove();
915
+ });
916
+ return root.toString();
917
+ }
918
+ //#endregion
267
919
  //#region src/compat/uni-app-x.ts
268
920
  const UNI_APP_X_BASE_CARRIER_SELECTORS = new Set([
269
921
  "*",
@@ -681,7 +1333,13 @@ const WEAPP_AUTOPREFIXER_BROWSERS = [
681
1333
  "Android >= 4.4",
682
1334
  "ChromeAndroid >= 37"
683
1335
  ];
684
- const WEAPP_AUTOPREFIXER_DEFAULT_OPTIONS = { flexbox: false };
1336
+ const WEAPP_AUTOPREFIXER_DEFAULT_OPTIONS = {
1337
+ add: true,
1338
+ flexbox: false,
1339
+ grid: false,
1340
+ remove: true,
1341
+ supports: false
1342
+ };
685
1343
  const AUTOPREFIXER_PLUGIN_NAME = "autoprefixer";
686
1344
  function isAutoprefixerPlugin(plugin) {
687
1345
  return plugin?.postcssPlugin === AUTOPREFIXER_PLUGIN_NAME;
@@ -1829,6 +2487,78 @@ function normalizeCalcValue(value) {
1829
2487
  function hasVariableReference(value) {
1830
2488
  return value.includes("var(");
1831
2489
  }
2490
+ function splitTopLevelCommaList(value) {
2491
+ const parts = [];
2492
+ let start = 0;
2493
+ let depth = 0;
2494
+ let quote;
2495
+ let escaped = false;
2496
+ for (let i = 0; i < value.length; i++) {
2497
+ const char = value[i];
2498
+ if (escaped) {
2499
+ escaped = false;
2500
+ continue;
2501
+ }
2502
+ if (char === "\\") {
2503
+ escaped = true;
2504
+ continue;
2505
+ }
2506
+ if (quote) {
2507
+ if (char === quote) quote = void 0;
2508
+ continue;
2509
+ }
2510
+ if (char === "\"" || char === "'") {
2511
+ quote = char;
2512
+ continue;
2513
+ }
2514
+ if (char === "(") {
2515
+ depth++;
2516
+ continue;
2517
+ }
2518
+ if (char === ")") {
2519
+ depth = Math.max(0, depth - 1);
2520
+ continue;
2521
+ }
2522
+ if (char === "," && depth === 0) {
2523
+ parts.push(value.slice(start, i));
2524
+ start = i + 1;
2525
+ }
2526
+ }
2527
+ parts.push(value.slice(start));
2528
+ return parts;
2529
+ }
2530
+ function getTransitionPropertySet(value) {
2531
+ const items = splitTopLevelCommaList(value).map((item) => item.trim().toLowerCase()).filter(Boolean);
2532
+ return items.length > 0 ? new Set(items) : void 0;
2533
+ }
2534
+ function isSubsetOfSet(subset, superset) {
2535
+ for (const item of subset) if (!superset.has(item)) return false;
2536
+ return true;
2537
+ }
2538
+ function removeRedundantTransitionPropertyFallbacks(rule) {
2539
+ const entries = rule.nodes.filter((node) => node.type === "decl" && node.prop.toLowerCase() === "transition-property").map((decl) => ({
2540
+ decl,
2541
+ items: getTransitionPropertySet(decl.value)
2542
+ }));
2543
+ for (let i = 0; i < entries.length; i++) {
2544
+ const entry = entries[i];
2545
+ if (!entry?.items) continue;
2546
+ for (let j = i + 1; j < entries.length; j++) {
2547
+ const next = entries[j];
2548
+ if (!next?.items) continue;
2549
+ if (next.items.size === entry.items.size && isSubsetOfSet(entry.items, next.items)) {
2550
+ next.decl.remove();
2551
+ entries.splice(j, 1);
2552
+ j--;
2553
+ continue;
2554
+ }
2555
+ if (next.items.size > entry.items.size && isSubsetOfSet(entry.items, next.items)) {
2556
+ entry.decl.remove();
2557
+ break;
2558
+ }
2559
+ }
2560
+ }
2561
+ }
1832
2562
  function dedupeDeclarations(rule) {
1833
2563
  const entries = [];
1834
2564
  for (const node of [...rule.nodes]) {
@@ -1880,6 +2610,7 @@ function dedupeDeclarations(rule) {
1880
2610
  if (literalSeen.get(canonical)) node.remove();
1881
2611
  else literalSeen.set(canonical, node);
1882
2612
  }
2613
+ removeRedundantTransitionPropertyFallbacks(rule);
1883
2614
  }
1884
2615
  //#endregion
1885
2616
  //#region src/plugins/post/specificity-cleaner.ts
@@ -2022,17 +2753,32 @@ const postcssWeappTailwindcssPostPlugin = (options) => {
2022
2753
  else normalizeTailwindcssRpxDeclaration(decl, { majorVersion: opts.majorVersion });
2023
2754
  if (enableMainChunkTransforms) normalizeTailwindcssV4Declaration(decl);
2024
2755
  removeLegacyFlexboxPrefix(decl);
2756
+ if (enableMainChunkTransforms) normalizeMiniProgramPrefixedDeclaration(decl);
2025
2757
  };
2026
- if (enableMainChunkTransforms) p.AtRuleExit = (atRule) => {
2027
- /**
2028
- * @description 移除 property
2029
- */
2030
- if (opts.cssRemoveProperty && atRule.name === "property") atRule.remove();
2031
- /**
2032
- * 清除空节点
2033
- */
2034
- atRule.nodes?.length === 0 && atRule.remove();
2035
- };
2758
+ if (enableMainChunkTransforms) {
2759
+ p.OnceExit = (root) => {
2760
+ root.walkDecls((decl) => {
2761
+ normalizeMiniProgramPrefixedDeclaration(decl);
2762
+ });
2763
+ root.walkRules((rule) => {
2764
+ removeRedundantTransitionPropertyFallbacks(rule);
2765
+ });
2766
+ root.walkAtRules((atRule) => {
2767
+ removeUnsupportedMiniProgramPrefixedAtRule(atRule);
2768
+ });
2769
+ };
2770
+ p.AtRuleExit = (atRule) => {
2771
+ removeUnsupportedMiniProgramPrefixedAtRule(atRule);
2772
+ /**
2773
+ * @description 移除 property
2774
+ */
2775
+ if (opts.cssRemoveProperty && atRule.name === "property") atRule.remove();
2776
+ /**
2777
+ * 清除空节点
2778
+ */
2779
+ atRule.nodes?.length === 0 && atRule.remove();
2780
+ };
2781
+ }
2036
2782
  return p;
2037
2783
  };
2038
2784
  postcssWeappTailwindcssPostPlugin.postcss = true;
@@ -2742,4 +3488,4 @@ function createStyleHandler(options) {
2742
3488
  return handler;
2743
3489
  }
2744
3490
  //#endregion
2745
- export { createFallbackPlaceholderReplacer, createInjectPreflight, createStyleHandler, createStylePipeline, internalCssSelectorReplacer, normalizeModernColorValue, postcssHtmlTransform, protectDynamicColorMixAlpha };
3491
+ export { createFallbackPlaceholderReplacer, createInjectPreflight, createStyleHandler, createStylePipeline, finalizeMiniProgramCss, hoistTailwindPreflightBase, internalCssSelectorReplacer, normalizeMiniProgramPrefixedDeclaration, normalizeModernColorValue, postcssHtmlTransform, protectDynamicColorMixAlpha, pruneMiniProgramGeneratedCss, removeUnsupportedAtSupports, removeUnsupportedCascadeLayers, removeUnsupportedMiniProgramAtRules, removeUnsupportedMiniProgramPrefixedAtRule };