@weapp-tailwindcss/postcss 2.2.1-next.5 → 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,72 @@ 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
267
333
  //#region src/compat/mini-program-prefixes.ts
268
334
  const PRESERVED_WEBKIT_DECLARATION_PROPS = new Set([
269
335
  "-webkit-box-orient",
@@ -276,7 +342,7 @@ const PRESERVED_WEBKIT_DECLARATION_PROPS = new Set([
276
342
  ]);
277
343
  const PRESERVED_WEBKIT_VALUE_DECLARATIONS = new Map([["display", new Set(["-webkit-box"])], ["-webkit-background-clip", new Set(["text"])]]);
278
344
  const TRANSITION_PROPS = new Set(["transition", "transition-property"]);
279
- function splitTopLevelCommaList(value) {
345
+ function splitTopLevelCommaList$1(value) {
280
346
  const parts = [];
281
347
  let start = 0;
282
348
  let depth = 0;
@@ -323,7 +389,7 @@ function isPreservedWebkitDeclaration(decl) {
323
389
  return PRESERVED_WEBKIT_VALUE_DECLARATIONS.get(prop)?.has(decl.value.trim().toLowerCase()) ?? false;
324
390
  }
325
391
  function normalizeTransitionValue(value) {
326
- return splitTopLevelCommaList(value).map((part) => part.trim()).filter((part) => part.length > 0 && !part.toLowerCase().startsWith("-webkit-")).join(", ");
392
+ return splitTopLevelCommaList$1(value).map((part) => part.trim()).filter((part) => part.length > 0 && !part.toLowerCase().startsWith("-webkit-")).join(", ");
327
393
  }
328
394
  function hasUnsupportedWebkitKeywordValue(decl) {
329
395
  const value = decl.value.trim().toLowerCase();
@@ -354,6 +420,502 @@ function removeUnsupportedMiniProgramPrefixedAtRule(atRule) {
354
420
  if (atRule.name.toLowerCase() === "-webkit-keyframes") atRule.remove();
355
421
  }
356
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
357
919
  //#region src/compat/uni-app-x.ts
358
920
  const UNI_APP_X_BASE_CARRIER_SELECTORS = new Set([
359
921
  "*",
@@ -1925,6 +2487,78 @@ function normalizeCalcValue(value) {
1925
2487
  function hasVariableReference(value) {
1926
2488
  return value.includes("var(");
1927
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
+ }
1928
2562
  function dedupeDeclarations(rule) {
1929
2563
  const entries = [];
1930
2564
  for (const node of [...rule.nodes]) {
@@ -1976,6 +2610,7 @@ function dedupeDeclarations(rule) {
1976
2610
  if (literalSeen.get(canonical)) node.remove();
1977
2611
  else literalSeen.set(canonical, node);
1978
2612
  }
2613
+ removeRedundantTransitionPropertyFallbacks(rule);
1979
2614
  }
1980
2615
  //#endregion
1981
2616
  //#region src/plugins/post/specificity-cleaner.ts
@@ -2125,6 +2760,9 @@ const postcssWeappTailwindcssPostPlugin = (options) => {
2125
2760
  root.walkDecls((decl) => {
2126
2761
  normalizeMiniProgramPrefixedDeclaration(decl);
2127
2762
  });
2763
+ root.walkRules((rule) => {
2764
+ removeRedundantTransitionPropertyFallbacks(rule);
2765
+ });
2128
2766
  root.walkAtRules((atRule) => {
2129
2767
  removeUnsupportedMiniProgramPrefixedAtRule(atRule);
2130
2768
  });
@@ -2850,4 +3488,4 @@ function createStyleHandler(options) {
2850
3488
  return handler;
2851
3489
  }
2852
3490
  //#endregion
2853
- export { createFallbackPlaceholderReplacer, createInjectPreflight, createStyleHandler, createStylePipeline, internalCssSelectorReplacer, normalizeMiniProgramPrefixedDeclaration, normalizeModernColorValue, postcssHtmlTransform, protectDynamicColorMixAlpha, removeUnsupportedMiniProgramPrefixedAtRule };
3491
+ export { createFallbackPlaceholderReplacer, createInjectPreflight, createStyleHandler, createStylePipeline, finalizeMiniProgramCss, hoistTailwindPreflightBase, internalCssSelectorReplacer, normalizeMiniProgramPrefixedDeclaration, normalizeModernColorValue, postcssHtmlTransform, protectDynamicColorMixAlpha, pruneMiniProgramGeneratedCss, removeUnsupportedAtSupports, removeUnsupportedCascadeLayers, removeUnsupportedMiniProgramAtRules, removeUnsupportedMiniProgramPrefixedAtRule };
@@ -1,3 +1,4 @@
1
1
  import type { Rule } from 'postcss';
2
+ export declare function removeRedundantTransitionPropertyFallbacks(rule: Rule): void;
2
3
  export declare function reorderVariableDeclarations(rule: Rule): void;
3
4
  export declare function dedupeDeclarations(rule: Rule): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapp-tailwindcss/postcss",
3
- "version": "2.2.1-next.5",
3
+ "version": "3.0.0-next.6",
4
4
  "description": "@weapp-tailwindcss/postcss",
5
5
  "author": "ice breaker <1324318532@qq.com>",
6
6
  "license": "MIT",