@weapp-tailwindcss/postcss 1.3.4 → 2.0.0

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
@@ -34,7 +34,7 @@ function getDefaultOptions(options) {
34
34
  };
35
35
  }
36
36
 
37
- // src/plugins/index.ts
37
+ // src/pipeline.ts
38
38
  import postcssPresetEnv from "postcss-preset-env";
39
39
 
40
40
  // src/plugins/ctx.ts
@@ -80,32 +80,41 @@ import { regExpTest } from "@weapp-tailwindcss/shared";
80
80
  import valueParser from "postcss-value-parser";
81
81
  function getCustomPropertyCleaner(options) {
82
82
  const includeCustomProperties = Array.isArray(options.cssCalc) ? options.cssCalc : typeof options.cssCalc === "object" ? options.cssCalc.includeCustomProperties : [];
83
- if (!includeCustomProperties || includeCustomProperties.length === 0) {
83
+ const shouldMatchCustomProperties = Array.isArray(includeCustomProperties) && includeCustomProperties.length > 0;
84
+ if (!shouldMatchCustomProperties) {
84
85
  return null;
85
86
  }
86
87
  return {
87
88
  postcssPlugin: "postcss-remove-include-custom-properties",
88
89
  OnceExit(root) {
89
- root.walkDecls((decl, idx) => {
90
- if (idx === 0 || !/--/.test(decl.value)) {
90
+ root.walkDecls((decl) => {
91
+ const prevNode = decl.prev();
92
+ if (!prevNode || prevNode.type !== "decl" || prevNode.prop !== decl.prop) {
91
93
  return;
92
94
  }
93
- const prevNode = decl.parent?.nodes[idx - 1];
94
- if (!prevNode || prevNode.prop !== decl.prop) {
95
+ if (prevNode.value === decl.value) {
96
+ decl.remove();
97
+ return;
98
+ }
99
+ if (!shouldMatchCustomProperties || !/--/.test(decl.value)) {
95
100
  return;
96
101
  }
97
102
  const parsed = valueParser(decl.value);
103
+ let containsIncludedCustomProperty = false;
98
104
  parsed.walk((node) => {
99
- if (node.type !== "function" || node.value !== "var") {
105
+ if (node.type !== "function" || node.value !== "var" || containsIncludedCustomProperty) {
100
106
  return;
101
107
  }
102
108
  const match = node.nodes.find((x) => {
103
109
  return x.type === "word" && regExpTest(includeCustomProperties, x.value);
104
110
  });
105
111
  if (match) {
106
- decl.remove();
112
+ containsIncludedCustomProperty = true;
107
113
  }
108
114
  });
115
+ if (containsIncludedCustomProperty) {
116
+ decl.remove();
117
+ }
109
118
  });
110
119
  }
111
120
  };
@@ -374,128 +383,306 @@ function composeIsPseudo(strs) {
374
383
 
375
384
  // src/selectorParser/rule-transformer.ts
376
385
  var ruleTransformCache = /* @__PURE__ */ new WeakMap();
377
- function createRuleTransformer(options) {
378
- let currentRule;
379
- const { escapeMap, cssSelectorReplacement, cssRemoveHoverPseudoClass, uniAppX } = options;
380
- const transform = (selectors) => {
381
- const rule = currentRule;
382
- if (!rule) {
383
- return;
386
+ var MIRROR_PROP_PAIRS = [
387
+ ["margin-top", "margin-bottom"],
388
+ ["margin-left", "margin-right"],
389
+ ["margin-inline-start", "margin-inline-end"],
390
+ ["margin-block-start", "margin-block-end"],
391
+ ["border-top-width", "border-bottom-width"],
392
+ ["border-left-width", "border-right-width"],
393
+ ["border-inline-start-width", "border-inline-end-width"],
394
+ ["border-block-start-width", "border-block-end-width"]
395
+ ];
396
+ var MIRROR_PROP_MAP = /* @__PURE__ */ new Map();
397
+ var SPACING_PROP_SET = /* @__PURE__ */ new Set();
398
+ for (const [a, b] of MIRROR_PROP_PAIRS) {
399
+ MIRROR_PROP_MAP.set(a, b);
400
+ MIRROR_PROP_MAP.set(b, a);
401
+ SPACING_PROP_SET.add(a);
402
+ SPACING_PROP_SET.add(b);
403
+ }
404
+ var LEGACY_WEBKIT_SPACING_PROPS = /* @__PURE__ */ new Set([
405
+ "-webkit-margin-start",
406
+ "-webkit-margin-end",
407
+ "-webkit-margin-before",
408
+ "-webkit-margin-after"
409
+ ]);
410
+ var VAR_REFERENCE_PATTERN = /var\(/i;
411
+ function dedupeSpacingProps(rule) {
412
+ const grouped = /* @__PURE__ */ new Map();
413
+ for (const node of rule.nodes) {
414
+ if (node.type !== "decl") {
415
+ continue;
384
416
  }
385
- selectors.walk((selector, index) => {
386
- if (selector.type === "class") {
387
- selector.value = internalCssSelectorReplacer(selector.value, {
388
- escapeMap
389
- });
390
- } else if (selector.type === "universal") {
391
- if (cssSelectorReplacement?.universal) {
392
- selector.value = composeIsPseudo(cssSelectorReplacement.universal);
393
- }
394
- } else if (selector.type === "selector") {
395
- if (cssRemoveHoverPseudoClass) {
396
- const node = selector.nodes.find((x) => x.type === "pseudo" && x.value === ":hover");
397
- if (node) {
398
- selector.remove();
399
- }
400
- }
401
- } else if (selector.type === "pseudo") {
402
- if (selector.value === ":root" && cssSelectorReplacement?.root) {
403
- selector.value = composeIsPseudo(cssSelectorReplacement.root);
404
- } else if (selector.value === ":where") {
405
- if (uniAppX) {
406
- selector.value = ":is";
407
- }
408
- if (index === 0 && selector.length === 1) {
409
- selector.walk((node, idx) => {
410
- if (idx === 0 && node.type === "class") {
411
- const nodes2 = node.parent?.nodes;
412
- if (nodes2) {
413
- const first = nodes2[idx + 1];
414
- if (first && first.type === "combinator" && first.value === ">") {
415
- const second = nodes2[idx + 2];
416
- if (second && second.type === "pseudo" && second.value === ":not" && second.first.first.type === "pseudo" && second.first.first.value === ":last-child") {
417
- const ast = getCombinatorSelectorAst(options);
418
- second.replaceWith(
419
- ...ast
420
- );
421
- }
422
- }
423
- }
424
- }
425
- });
426
- selector.replaceWith(...selector.nodes);
427
- for (const node of rule.nodes) {
428
- if (node.type === "decl") {
429
- if (node.prop === "margin-block-start") {
430
- node.prop = "margin-block-end";
431
- } else if (node.prop === "margin-block-end") {
432
- node.prop = "margin-block-start";
433
- } else if (node.prop === "margin-inline-start") {
434
- node.prop = "margin-inline-end";
435
- } else if (node.prop === "margin-inline-end") {
436
- node.prop = "margin-inline-start";
437
- } else if (node.prop === "margin-top") {
438
- node.prop = "margin-bottom";
439
- } else if (node.prop === "margin-bottom") {
440
- node.prop = "margin-top";
441
- } else if (node.prop === "margin-left") {
442
- node.prop = "margin-right";
443
- } else if (node.prop === "margin-right") {
444
- node.prop = "margin-left";
445
- } else if (node.prop === "-webkit-margin-start" || node.prop === "-webkit-margin-end" || node.prop === "-webkit-margin-before" || node.prop === "-webkit-margin-after") {
446
- node.remove();
447
- }
448
- }
449
- }
450
- }
451
- }
452
- } else if (selector.type === "combinator") {
453
- if (selector.value === ">") {
454
- const nodes2 = selector.parent?.nodes;
455
- if (nodes2) {
456
- const first = nodes2[index + 1];
457
- if (first && first.type === "pseudo" && first.value === ":not" && (first.first.first.type === "attribute" && first.first.first.attribute === "hidden" || first.first.first.type === "tag" && first.first.first.value === "template")) {
458
- const second = nodes2[index + 2];
459
- if (second && second.type === "combinator" && (second.value === "~" || second.value === "+")) {
460
- const third = nodes2[index + 3];
461
- if (third && third.type === "pseudo" && third.value === ":not" && (third.first.first.type === "attribute" && third.first.first.attribute === "hidden" || third.first.first.type === "tag" && third.first.first.value === "template")) {
462
- const ast = getCombinatorSelectorAst(options);
463
- selector.parent?.nodes.splice(
464
- index + 1,
465
- 3,
466
- ...ast
467
- );
468
- }
469
- }
470
- }
471
- }
472
- }
473
- } else if (selector.type === "tag") {
474
- if (uniAppX) {
475
- selector.remove();
476
- }
477
- } else if (selector.type === "attribute") {
478
- if (uniAppX) {
479
- selector.remove();
480
- }
417
+ if (!SPACING_PROP_SET.has(node.prop)) {
418
+ continue;
419
+ }
420
+ const list = grouped.get(node.prop);
421
+ if (list) {
422
+ list.push(node);
423
+ } else {
424
+ grouped.set(node.prop, [node]);
425
+ }
426
+ }
427
+ for (const [, declarations] of grouped) {
428
+ if (declarations.length <= 1) {
429
+ continue;
430
+ }
431
+ const unique = [];
432
+ const seenValues = /* @__PURE__ */ new Set();
433
+ for (const decl of declarations) {
434
+ if (decl.parent !== rule) {
435
+ continue;
481
436
  }
482
- });
483
- selectors.walk((selector) => {
484
- if (selector.type === "selector") {
485
- selector.length === 0 && selector.remove();
437
+ const key = `${decl.important ? "!important@@" : ""}${decl.value}`;
438
+ if (seenValues.has(key)) {
439
+ decl.remove();
440
+ continue;
486
441
  }
487
- });
488
- if (selectors.length === 0) {
489
- rule.remove();
442
+ seenValues.add(key);
443
+ unique.push(decl);
444
+ }
445
+ if (unique.length <= 1) {
446
+ continue;
447
+ }
448
+ const literals = unique.filter((decl) => !VAR_REFERENCE_PATTERN.test(decl.value));
449
+ const variables = unique.filter((decl) => VAR_REFERENCE_PATTERN.test(decl.value));
450
+ if (variables.length === 0 || literals.length === 0) {
451
+ continue;
452
+ }
453
+ const ordered = [...literals, ...variables];
454
+ const alreadyOrdered = ordered.every((decl, index) => decl === unique[index]);
455
+ if (alreadyOrdered) {
456
+ continue;
457
+ }
458
+ const anchor = unique[unique.length - 1]?.next() ?? void 0;
459
+ for (const decl of unique) {
460
+ decl.remove();
461
+ }
462
+ for (const decl of ordered) {
463
+ if (anchor) {
464
+ rule.insertBefore(anchor, decl);
465
+ } else {
466
+ rule.append(decl);
467
+ }
468
+ }
469
+ }
470
+ }
471
+ function isNotLastChildPseudo(node) {
472
+ if (!node || node.type !== "pseudo" || node.value !== ":not") {
473
+ return false;
474
+ }
475
+ const selectors = node.nodes;
476
+ if (!selectors || selectors.length !== 1) {
477
+ return false;
478
+ }
479
+ const firstSelector = selectors[0];
480
+ if (firstSelector.type !== "selector") {
481
+ return false;
482
+ }
483
+ const target = firstSelector.nodes?.[0];
484
+ return Boolean(target && target.type === "pseudo" && target.value === ":last-child");
485
+ }
486
+ function transformSpacingSelector(nodes2, options) {
487
+ if (!nodes2 || nodes2.length === 0) {
488
+ return false;
489
+ }
490
+ for (let idx = 0; idx < nodes2.length; idx++) {
491
+ const current = nodes2[idx];
492
+ if (current.type !== "class" && current.type !== "nesting") {
493
+ continue;
494
+ }
495
+ const combinator = nodes2[idx + 1];
496
+ if (!combinator || combinator.type !== "combinator" || combinator.value !== ">") {
497
+ continue;
498
+ }
499
+ const candidate = nodes2[idx + 2];
500
+ if (!isNotLastChildPseudo(candidate)) {
501
+ continue;
502
+ }
503
+ const ast = getCombinatorSelectorAst(options);
504
+ candidate.replaceWith(...ast);
505
+ return true;
506
+ }
507
+ return false;
508
+ }
509
+ function normalizeSpacingDeclarations(rule) {
510
+ for (const node of [...rule.nodes]) {
511
+ if (node.type !== "decl") {
512
+ continue;
513
+ }
514
+ if (LEGACY_WEBKIT_SPACING_PROPS.has(node.prop)) {
515
+ node.remove();
516
+ continue;
517
+ }
518
+ const mirror = MIRROR_PROP_MAP.get(node.prop);
519
+ if (mirror) {
520
+ node.prop = mirror;
521
+ }
522
+ }
523
+ dedupeSpacingProps(rule);
524
+ }
525
+ function flattenWherePseudo(node, context, index, parent) {
526
+ if (context.options.uniAppX) {
527
+ node.value = ":is";
528
+ }
529
+ if (index === 0 && node.length === 1) {
530
+ const targetSelector = node.nodes?.[0];
531
+ if (targetSelector && targetSelector.type === "selector" && transformSpacingSelector(targetSelector.nodes, context.options)) {
532
+ context.requiresSpacingNormalization = true;
490
533
  }
534
+ node.replaceWith(...node.nodes);
535
+ if (parent && parent.type === "selector" && parent.length === 0) {
536
+ parent.remove();
537
+ }
538
+ }
539
+ }
540
+ function handleClassNode(node, context) {
541
+ if (node.type !== "class") {
542
+ return;
543
+ }
544
+ node.value = internalCssSelectorReplacer(node.value, { escapeMap: context.options.escapeMap });
545
+ }
546
+ function handleUniversalNode(node, context) {
547
+ if (node.type !== "universal") {
548
+ return;
549
+ }
550
+ const replacement = context.options.cssSelectorReplacement?.universal;
551
+ if (replacement) {
552
+ node.value = composeIsPseudo(replacement);
553
+ }
554
+ }
555
+ function shouldRemoveHoverSelector(selector, options) {
556
+ if (!options.cssRemoveHoverPseudoClass) {
557
+ return false;
558
+ }
559
+ return selector.nodes.some((node) => node.type === "pseudo" && node.value === ":hover");
560
+ }
561
+ function isHiddenOrTemplateNotPseudo(node) {
562
+ if (!node || node.type !== "pseudo" || node.value !== ":not") {
563
+ return false;
564
+ }
565
+ const selector = node.first;
566
+ if (!selector || selector.type !== "selector") {
567
+ return false;
568
+ }
569
+ const first = selector.first;
570
+ if (!first) {
571
+ return false;
572
+ }
573
+ if (first.type === "attribute") {
574
+ return first.attribute === "hidden";
575
+ }
576
+ if (first.type === "tag") {
577
+ return first.value === "template";
578
+ }
579
+ return false;
580
+ }
581
+ function handleCombinatorNode(node, index, context) {
582
+ if (node.type !== "combinator" || node.value !== ">") {
583
+ return;
584
+ }
585
+ const nodes2 = node.parent?.nodes;
586
+ if (!nodes2) {
587
+ return;
588
+ }
589
+ const first = nodes2[index + 1];
590
+ const second = nodes2[index + 2];
591
+ const third = nodes2[index + 3];
592
+ if (isHiddenOrTemplateNotPseudo(first) && second && second.type === "combinator" && (second.value === "~" || second.value === "+") && isHiddenOrTemplateNotPseudo(third)) {
593
+ const ast = getCombinatorSelectorAst(context.options);
594
+ nodes2.splice(index + 1, 3, ...ast);
595
+ }
596
+ }
597
+ function handlePseudoNode(node, index, context, parent) {
598
+ if (node.type !== "pseudo") {
599
+ return;
600
+ }
601
+ if (node.value === ":root" && context.options.cssSelectorReplacement?.root) {
602
+ node.value = composeIsPseudo(context.options.cssSelectorReplacement.root);
603
+ return;
604
+ }
605
+ if (node.value === ":where") {
606
+ flattenWherePseudo(node, context, index, parent);
607
+ }
608
+ }
609
+ function handleTagOrAttribute(node, context) {
610
+ if (!context.options.uniAppX) {
611
+ return;
612
+ }
613
+ if (node.type === "tag" || node.type === "attribute") {
614
+ node.remove();
615
+ }
616
+ }
617
+ function handleSelectorNode(selector, context) {
618
+ if (shouldRemoveHoverSelector(selector, context.options)) {
619
+ selector.remove();
620
+ return;
621
+ }
622
+ if (transformSpacingSelector(selector.nodes, context.options)) {
623
+ context.requiresSpacingNormalization = true;
624
+ }
625
+ }
626
+ function transformSelectors(selectors, context) {
627
+ selectors.walk((node, index) => {
628
+ const parent = node.parent?.type === "selector" ? node.parent : void 0;
629
+ switch (node.type) {
630
+ case "class":
631
+ handleClassNode(node, context);
632
+ break;
633
+ case "universal":
634
+ handleUniversalNode(node, context);
635
+ break;
636
+ case "selector":
637
+ handleSelectorNode(node, context);
638
+ break;
639
+ case "pseudo":
640
+ if (!isNotLastChildPseudo(node)) {
641
+ handlePseudoNode(node, index, context, parent);
642
+ }
643
+ break;
644
+ case "combinator":
645
+ handleCombinatorNode(node, index, context);
646
+ break;
647
+ case "tag":
648
+ case "attribute":
649
+ handleTagOrAttribute(node, context);
650
+ break;
651
+ default:
652
+ break;
653
+ }
654
+ });
655
+ if (context.requiresSpacingNormalization) {
656
+ normalizeSpacingDeclarations(context.rule);
657
+ }
658
+ selectors.walk((node) => {
659
+ if (node.type === "selector" && node.length === 0) {
660
+ node.remove();
661
+ }
662
+ });
663
+ if (selectors.length === 0) {
664
+ context.rule.remove();
665
+ }
666
+ }
667
+ function createRuleTransformer(options) {
668
+ let context;
669
+ const transform = (selectors) => {
670
+ if (!context) {
671
+ return;
672
+ }
673
+ transformSelectors(selectors, context);
491
674
  };
492
675
  const parser = psp4(transform);
493
676
  return (rule) => {
494
- currentRule = rule;
677
+ context = {
678
+ options,
679
+ requiresSpacingNormalization: false,
680
+ rule
681
+ };
495
682
  try {
496
683
  parser.transformSync(rule, normalizeTransformOptions());
497
684
  } finally {
498
- currentRule = void 0;
685
+ context = void 0;
499
686
  }
500
687
  };
501
688
  }
@@ -559,14 +746,33 @@ function createRootSpecificityCleaner(options) {
559
746
  }
560
747
  var OklabSuffix = "in oklab";
561
748
  var logicalPropMap = /* @__PURE__ */ new Map([
749
+ // margin
562
750
  ["margin-inline-start", "margin-left"],
563
751
  ["margin-inline-end", "margin-right"],
564
752
  ["margin-block-start", "margin-top"],
565
753
  ["margin-block-end", "margin-bottom"],
754
+ // padding
566
755
  ["padding-inline-start", "padding-left"],
567
756
  ["padding-inline-end", "padding-right"],
568
757
  ["padding-block-start", "padding-top"],
569
- ["padding-block-end", "padding-bottom"]
758
+ ["padding-block-end", "padding-bottom"],
759
+ // border
760
+ ["border-inline-start", "border-left"],
761
+ ["border-inline-end", "border-right"],
762
+ ["border-block-start", "border-top"],
763
+ ["border-block-end", "border-bottom"],
764
+ ["border-inline-start-width", "border-left-width"],
765
+ ["border-inline-end-width", "border-right-width"]
766
+ ]);
767
+ var variablePriorityProps = /* @__PURE__ */ new Set([
768
+ "margin-left",
769
+ "margin-right",
770
+ "margin-top",
771
+ "margin-bottom",
772
+ "border-left-width",
773
+ "border-right-width",
774
+ "border-top-width",
775
+ "border-bottom-width"
570
776
  ]);
571
777
  function getCanonicalProp(prop) {
572
778
  return logicalPropMap.get(prop) ?? prop;
@@ -583,6 +789,9 @@ function normalizeCalcValue(value) {
583
789
  } while (next !== prev);
584
790
  return next.replace(/calc\(\s*(1\s*-\s*var\([^()]+\))\s*\)/gi, "($1)");
585
791
  }
792
+ function hasVariableReference(value) {
793
+ return value.includes("var(");
794
+ }
586
795
  function dedupeDeclarations(rule) {
587
796
  const entries = [];
588
797
  for (const node of [...rule.nodes]) {
@@ -618,6 +827,73 @@ function dedupeDeclarations(rule) {
618
827
  entry.decl.remove();
619
828
  }
620
829
  }
830
+ const reorderGroups = /* @__PURE__ */ new Map();
831
+ for (const node of rule.nodes) {
832
+ if (node.type !== "decl") {
833
+ continue;
834
+ }
835
+ const canonical = getCanonicalProp(node.prop);
836
+ if (!variablePriorityProps.has(canonical)) {
837
+ continue;
838
+ }
839
+ const existing = reorderGroups.get(canonical);
840
+ if (existing) {
841
+ existing.push(node);
842
+ } else {
843
+ reorderGroups.set(canonical, [node]);
844
+ }
845
+ }
846
+ for (const declarations of reorderGroups.values()) {
847
+ if (declarations.length <= 1) {
848
+ continue;
849
+ }
850
+ const literals = declarations.filter((decl) => !hasVariableReference(decl.value));
851
+ const variables = declarations.filter((decl) => hasVariableReference(decl.value));
852
+ if (literals.length === 0 || variables.length === 0) {
853
+ continue;
854
+ }
855
+ const ordered = [...literals, ...variables];
856
+ let needReorder = false;
857
+ for (let index = 0; index < ordered.length; index++) {
858
+ if (ordered[index] !== declarations[index]) {
859
+ needReorder = true;
860
+ break;
861
+ }
862
+ }
863
+ if (!needReorder) {
864
+ continue;
865
+ }
866
+ const anchor = declarations[declarations.length - 1]?.next() ?? void 0;
867
+ for (const decl of declarations) {
868
+ decl.remove();
869
+ }
870
+ for (const decl of ordered) {
871
+ if (anchor) {
872
+ rule.insertBefore(anchor, decl);
873
+ } else {
874
+ rule.append(decl);
875
+ }
876
+ }
877
+ }
878
+ const literalSeen = /* @__PURE__ */ new Map();
879
+ for (const node of [...rule.nodes]) {
880
+ if (node.type !== "decl") {
881
+ continue;
882
+ }
883
+ const canonical = getCanonicalProp(node.prop);
884
+ if (!variablePriorityProps.has(canonical)) {
885
+ continue;
886
+ }
887
+ if (hasVariableReference(node.value)) {
888
+ continue;
889
+ }
890
+ const existing = literalSeen.get(canonical);
891
+ if (existing) {
892
+ node.remove();
893
+ } else {
894
+ literalSeen.set(canonical, node);
895
+ }
896
+ }
621
897
  }
622
898
  var postcssWeappTailwindcssPostPlugin = (options) => {
623
899
  const opts = defu(options, {
@@ -1197,39 +1473,163 @@ var postcssWeappTailwindcssPrePlugin = (options) => {
1197
1473
  };
1198
1474
  postcssWeappTailwindcssPrePlugin.postcss = true;
1199
1475
 
1200
- // src/plugins/index.ts
1201
- import { default as default2 } from "postcss-rem-to-responsive-pixel";
1202
- function normalizePlugins(options) {
1203
- const plugins = [];
1204
- const pxPlugin = getPxTransformPlugin(options);
1205
- if (pxPlugin) {
1206
- plugins.push(pxPlugin);
1207
- }
1208
- const remPlugin = getRemTransformPlugin(options);
1209
- if (remPlugin) {
1210
- plugins.push(remPlugin);
1211
- }
1212
- const calcPlugin = getCalcPlugin(options);
1213
- if (calcPlugin) {
1214
- plugins.push(calcPlugin);
1215
- }
1216
- const customPropertyCleaner = getCustomPropertyCleaner(options);
1217
- if (customPropertyCleaner) {
1218
- plugins.push(customPropertyCleaner);
1219
- }
1220
- return plugins;
1476
+ // src/pipeline.ts
1477
+ var STAGE_ORDER = ["pre", "normal", "post"];
1478
+ function normalizeUserPlugins(plugins) {
1479
+ if (!plugins) {
1480
+ return [];
1481
+ }
1482
+ if (Array.isArray(plugins)) {
1483
+ return plugins.filter(Boolean);
1484
+ }
1485
+ if (typeof plugins === "object") {
1486
+ return Object.values(plugins).filter(Boolean);
1487
+ }
1488
+ return [];
1221
1489
  }
1222
- function getPlugins(options) {
1223
- const ctx = createContext();
1224
- options.ctx = ctx;
1225
- const plugins = [
1226
- ...options.postcssOptions?.plugins ?? [],
1227
- postcssWeappTailwindcssPrePlugin(options),
1228
- postcssPresetEnv(options.cssPresetEnv),
1229
- ...normalizePlugins(options),
1230
- postcssWeappTailwindcssPostPlugin(options)
1231
- ].filter(Boolean);
1232
- return plugins;
1490
+ function createStaticDefinition(id, stage, plugin) {
1491
+ return {
1492
+ id,
1493
+ stage,
1494
+ prepare: () => ({
1495
+ id,
1496
+ stage,
1497
+ createPlugin: () => plugin
1498
+ })
1499
+ };
1500
+ }
1501
+ function createPipelineDefinitions(options) {
1502
+ const stages = {
1503
+ pre: [],
1504
+ normal: [],
1505
+ post: []
1506
+ };
1507
+ const userPlugins = normalizeUserPlugins(options.postcssOptions?.plugins);
1508
+ userPlugins.forEach((plugin, index) => {
1509
+ stages.pre.push(createStaticDefinition(`pre:user-${index}`, "pre", plugin));
1510
+ });
1511
+ stages.pre.push({
1512
+ id: "pre:core",
1513
+ stage: "pre",
1514
+ prepare: () => ({
1515
+ id: "pre:core",
1516
+ stage: "pre",
1517
+ createPlugin: () => postcssWeappTailwindcssPrePlugin(options)
1518
+ })
1519
+ });
1520
+ stages.normal.push({
1521
+ id: "normal:preset-env",
1522
+ stage: "normal",
1523
+ prepare: () => ({
1524
+ id: "normal:preset-env",
1525
+ stage: "normal",
1526
+ createPlugin: () => postcssPresetEnv(options.cssPresetEnv)
1527
+ })
1528
+ });
1529
+ stages.normal.push({
1530
+ id: "normal:px-transform",
1531
+ stage: "normal",
1532
+ prepare: () => {
1533
+ const plugin = getPxTransformPlugin(options);
1534
+ return plugin ? {
1535
+ id: "normal:px-transform",
1536
+ stage: "normal",
1537
+ createPlugin: () => plugin
1538
+ } : void 0;
1539
+ }
1540
+ });
1541
+ stages.normal.push({
1542
+ id: "normal:rem-transform",
1543
+ stage: "normal",
1544
+ prepare: () => {
1545
+ const plugin = getRemTransformPlugin(options);
1546
+ return plugin ? {
1547
+ id: "normal:rem-transform",
1548
+ stage: "normal",
1549
+ createPlugin: () => plugin
1550
+ } : void 0;
1551
+ }
1552
+ });
1553
+ stages.normal.push({
1554
+ id: "normal:calc",
1555
+ stage: "normal",
1556
+ prepare: () => {
1557
+ const plugin = getCalcPlugin(options);
1558
+ return plugin ? {
1559
+ id: "normal:calc",
1560
+ stage: "normal",
1561
+ createPlugin: () => plugin
1562
+ } : void 0;
1563
+ }
1564
+ });
1565
+ stages.normal.push({
1566
+ id: "normal:custom-property-cleaner",
1567
+ stage: "normal",
1568
+ prepare: () => {
1569
+ const plugin = getCustomPropertyCleaner(options);
1570
+ return plugin ? {
1571
+ id: "normal:custom-property-cleaner",
1572
+ stage: "normal",
1573
+ createPlugin: () => plugin
1574
+ } : void 0;
1575
+ }
1576
+ });
1577
+ stages.post.push({
1578
+ id: "post:core",
1579
+ stage: "post",
1580
+ prepare: () => ({
1581
+ id: "post:core",
1582
+ stage: "post",
1583
+ createPlugin: () => postcssWeappTailwindcssPostPlugin(options)
1584
+ })
1585
+ });
1586
+ return STAGE_ORDER.flatMap((stage) => stages[stage]);
1587
+ }
1588
+ function createStylePipeline(options) {
1589
+ options.ctx = createContext();
1590
+ const preparedNodes = createPipelineDefinitions(options).map((definition) => definition.prepare(options)).filter(Boolean);
1591
+ if (preparedNodes.length === 0) {
1592
+ return {
1593
+ nodes: [],
1594
+ plugins: []
1595
+ };
1596
+ }
1597
+ const stageSizes = /* @__PURE__ */ new Map();
1598
+ preparedNodes.forEach((node) => {
1599
+ stageSizes.set(node.stage, (stageSizes.get(node.stage) ?? 0) + 1);
1600
+ });
1601
+ const stageIndices = /* @__PURE__ */ new Map();
1602
+ const nodes2 = [];
1603
+ const size = preparedNodes.length;
1604
+ preparedNodes.forEach((node, index) => {
1605
+ const stageIndex = stageIndices.get(node.stage) ?? 0;
1606
+ const context = {
1607
+ stage: node.stage,
1608
+ index,
1609
+ size,
1610
+ stageIndex,
1611
+ stageSize: stageSizes.get(node.stage) ?? 0,
1612
+ previous: index > 0 ? {
1613
+ id: preparedNodes[index - 1].id,
1614
+ stage: preparedNodes[index - 1].stage
1615
+ } : void 0,
1616
+ next: index < size - 1 ? {
1617
+ id: preparedNodes[index + 1].id,
1618
+ stage: preparedNodes[index + 1].stage
1619
+ } : void 0
1620
+ };
1621
+ stageIndices.set(node.stage, stageIndex + 1);
1622
+ nodes2.push({
1623
+ id: node.id,
1624
+ stage: node.stage,
1625
+ plugin: node.createPlugin(context),
1626
+ context
1627
+ });
1628
+ });
1629
+ return {
1630
+ nodes: nodes2,
1631
+ plugins: nodes2.map((node) => node.plugin)
1632
+ };
1233
1633
  }
1234
1634
 
1235
1635
  // src/preflight.ts
@@ -1258,16 +1658,16 @@ function createProcessOptions(options) {
1258
1658
  ...options.postcssOptions?.options ?? {}
1259
1659
  };
1260
1660
  }
1261
- var pluginCache = /* @__PURE__ */ new WeakMap();
1661
+ var pipelineCache = /* @__PURE__ */ new WeakMap();
1262
1662
  var processOptionsCache = /* @__PURE__ */ new WeakMap();
1263
1663
  var processOptionsSourceCache = /* @__PURE__ */ new WeakMap();
1264
- function getCachedPlugins(options) {
1265
- let plugins = pluginCache.get(options);
1266
- if (!plugins) {
1267
- plugins = getPlugins(options);
1268
- pluginCache.set(options, plugins);
1664
+ function getCachedPipeline(options) {
1665
+ let pipeline = pipelineCache.get(options);
1666
+ if (!pipeline) {
1667
+ pipeline = createStylePipeline(options);
1668
+ pipelineCache.set(options, pipeline);
1269
1669
  }
1270
- return plugins;
1670
+ return pipeline;
1271
1671
  }
1272
1672
  function getCachedProcessOptions(options) {
1273
1673
  const source = options.postcssOptions?.options;
@@ -1281,7 +1681,7 @@ function getCachedProcessOptions(options) {
1281
1681
  return { ...cached };
1282
1682
  }
1283
1683
  function styleHandler(rawSource, options) {
1284
- const plugins = getCachedPlugins(options);
1684
+ const plugins = getCachedPipeline(options).plugins;
1285
1685
  const processOptions = getCachedProcessOptions(options);
1286
1686
  return postcss(plugins).process(
1287
1687
  rawSource,
@@ -1294,18 +1694,27 @@ function createStyleHandler(options) {
1294
1694
  getDefaultOptions(options)
1295
1695
  );
1296
1696
  cachedOptions.cssInjectPreflight = createInjectPreflight(cachedOptions.cssPreflight);
1297
- getCachedPlugins(cachedOptions);
1697
+ getCachedPipeline(cachedOptions);
1298
1698
  getCachedProcessOptions(cachedOptions);
1299
- return (rawSource, opt) => {
1699
+ const handler = ((rawSource, opt) => {
1300
1700
  const resolvedOptions = defuOverrideArray3(opt, cachedOptions);
1301
1701
  return styleHandler(
1302
1702
  rawSource,
1303
1703
  resolvedOptions
1304
1704
  );
1705
+ });
1706
+ handler.getPipeline = (opt) => {
1707
+ if (!opt) {
1708
+ return getCachedPipeline(cachedOptions);
1709
+ }
1710
+ const resolvedOptions = defuOverrideArray3(opt, cachedOptions);
1711
+ return getCachedPipeline(resolvedOptions);
1305
1712
  };
1713
+ return handler;
1306
1714
  }
1307
1715
  export {
1308
1716
  createInjectPreflight,
1309
1717
  createStyleHandler,
1718
+ createStylePipeline,
1310
1719
  internalCssSelectorReplacer
1311
1720
  };