@weapp-tailwindcss/postcss 1.3.4 → 2.0.1

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);
490
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;
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, {
@@ -661,11 +937,6 @@ var postcssWeappTailwindcssPostPlugin = (options) => {
661
937
  atRule.nodes?.length === 0 && atRule.remove();
662
938
  };
663
939
  }
664
- if (typeof opts.customRuleCallback === "function") {
665
- p.Rule = (rule) => {
666
- opts.customRuleCallback?.(rule, opts);
667
- };
668
- }
669
940
  return p;
670
941
  };
671
942
  postcssWeappTailwindcssPostPlugin.postcss = true;
@@ -1197,39 +1468,163 @@ var postcssWeappTailwindcssPrePlugin = (options) => {
1197
1468
  };
1198
1469
  postcssWeappTailwindcssPrePlugin.postcss = true;
1199
1470
 
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;
1471
+ // src/pipeline.ts
1472
+ var STAGE_ORDER = ["pre", "normal", "post"];
1473
+ function normalizeUserPlugins(plugins) {
1474
+ if (!plugins) {
1475
+ return [];
1476
+ }
1477
+ if (Array.isArray(plugins)) {
1478
+ return plugins.filter(Boolean);
1479
+ }
1480
+ if (typeof plugins === "object") {
1481
+ return Object.values(plugins).filter(Boolean);
1482
+ }
1483
+ return [];
1484
+ }
1485
+ function createStaticDefinition(id, stage, plugin) {
1486
+ return {
1487
+ id,
1488
+ stage,
1489
+ prepare: () => ({
1490
+ id,
1491
+ stage,
1492
+ createPlugin: () => plugin
1493
+ })
1494
+ };
1495
+ }
1496
+ function createPipelineDefinitions(options) {
1497
+ const stages = {
1498
+ pre: [],
1499
+ normal: [],
1500
+ post: []
1501
+ };
1502
+ const userPlugins = normalizeUserPlugins(options.postcssOptions?.plugins);
1503
+ userPlugins.forEach((plugin, index) => {
1504
+ stages.pre.push(createStaticDefinition(`pre:user-${index}`, "pre", plugin));
1505
+ });
1506
+ stages.pre.push({
1507
+ id: "pre:core",
1508
+ stage: "pre",
1509
+ prepare: () => ({
1510
+ id: "pre:core",
1511
+ stage: "pre",
1512
+ createPlugin: () => postcssWeappTailwindcssPrePlugin(options)
1513
+ })
1514
+ });
1515
+ stages.normal.push({
1516
+ id: "normal:preset-env",
1517
+ stage: "normal",
1518
+ prepare: () => ({
1519
+ id: "normal:preset-env",
1520
+ stage: "normal",
1521
+ createPlugin: () => postcssPresetEnv(options.cssPresetEnv)
1522
+ })
1523
+ });
1524
+ stages.normal.push({
1525
+ id: "normal:px-transform",
1526
+ stage: "normal",
1527
+ prepare: () => {
1528
+ const plugin = getPxTransformPlugin(options);
1529
+ return plugin ? {
1530
+ id: "normal:px-transform",
1531
+ stage: "normal",
1532
+ createPlugin: () => plugin
1533
+ } : void 0;
1534
+ }
1535
+ });
1536
+ stages.normal.push({
1537
+ id: "normal:rem-transform",
1538
+ stage: "normal",
1539
+ prepare: () => {
1540
+ const plugin = getRemTransformPlugin(options);
1541
+ return plugin ? {
1542
+ id: "normal:rem-transform",
1543
+ stage: "normal",
1544
+ createPlugin: () => plugin
1545
+ } : void 0;
1546
+ }
1547
+ });
1548
+ stages.normal.push({
1549
+ id: "normal:calc",
1550
+ stage: "normal",
1551
+ prepare: () => {
1552
+ const plugin = getCalcPlugin(options);
1553
+ return plugin ? {
1554
+ id: "normal:calc",
1555
+ stage: "normal",
1556
+ createPlugin: () => plugin
1557
+ } : void 0;
1558
+ }
1559
+ });
1560
+ stages.normal.push({
1561
+ id: "normal:custom-property-cleaner",
1562
+ stage: "normal",
1563
+ prepare: () => {
1564
+ const plugin = getCustomPropertyCleaner(options);
1565
+ return plugin ? {
1566
+ id: "normal:custom-property-cleaner",
1567
+ stage: "normal",
1568
+ createPlugin: () => plugin
1569
+ } : void 0;
1570
+ }
1571
+ });
1572
+ stages.post.push({
1573
+ id: "post:core",
1574
+ stage: "post",
1575
+ prepare: () => ({
1576
+ id: "post:core",
1577
+ stage: "post",
1578
+ createPlugin: () => postcssWeappTailwindcssPostPlugin(options)
1579
+ })
1580
+ });
1581
+ return STAGE_ORDER.flatMap((stage) => stages[stage]);
1221
1582
  }
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;
1583
+ function createStylePipeline(options) {
1584
+ options.ctx = createContext();
1585
+ const preparedNodes = createPipelineDefinitions(options).map((definition) => definition.prepare(options)).filter(Boolean);
1586
+ if (preparedNodes.length === 0) {
1587
+ return {
1588
+ nodes: [],
1589
+ plugins: []
1590
+ };
1591
+ }
1592
+ const stageSizes = /* @__PURE__ */ new Map();
1593
+ preparedNodes.forEach((node) => {
1594
+ stageSizes.set(node.stage, (stageSizes.get(node.stage) ?? 0) + 1);
1595
+ });
1596
+ const stageIndices = /* @__PURE__ */ new Map();
1597
+ const nodes2 = [];
1598
+ const size = preparedNodes.length;
1599
+ preparedNodes.forEach((node, index) => {
1600
+ const stageIndex = stageIndices.get(node.stage) ?? 0;
1601
+ const context = {
1602
+ stage: node.stage,
1603
+ index,
1604
+ size,
1605
+ stageIndex,
1606
+ stageSize: stageSizes.get(node.stage) ?? 0,
1607
+ previous: index > 0 ? {
1608
+ id: preparedNodes[index - 1].id,
1609
+ stage: preparedNodes[index - 1].stage
1610
+ } : void 0,
1611
+ next: index < size - 1 ? {
1612
+ id: preparedNodes[index + 1].id,
1613
+ stage: preparedNodes[index + 1].stage
1614
+ } : void 0
1615
+ };
1616
+ stageIndices.set(node.stage, stageIndex + 1);
1617
+ nodes2.push({
1618
+ id: node.id,
1619
+ stage: node.stage,
1620
+ plugin: node.createPlugin(context),
1621
+ context
1622
+ });
1623
+ });
1624
+ return {
1625
+ nodes: nodes2,
1626
+ plugins: nodes2.map((node) => node.plugin)
1627
+ };
1233
1628
  }
1234
1629
 
1235
1630
  // src/preflight.ts
@@ -1258,16 +1653,16 @@ function createProcessOptions(options) {
1258
1653
  ...options.postcssOptions?.options ?? {}
1259
1654
  };
1260
1655
  }
1261
- var pluginCache = /* @__PURE__ */ new WeakMap();
1656
+ var pipelineCache = /* @__PURE__ */ new WeakMap();
1262
1657
  var processOptionsCache = /* @__PURE__ */ new WeakMap();
1263
1658
  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);
1659
+ function getCachedPipeline(options) {
1660
+ let pipeline = pipelineCache.get(options);
1661
+ if (!pipeline) {
1662
+ pipeline = createStylePipeline(options);
1663
+ pipelineCache.set(options, pipeline);
1269
1664
  }
1270
- return plugins;
1665
+ return pipeline;
1271
1666
  }
1272
1667
  function getCachedProcessOptions(options) {
1273
1668
  const source = options.postcssOptions?.options;
@@ -1281,7 +1676,7 @@ function getCachedProcessOptions(options) {
1281
1676
  return { ...cached };
1282
1677
  }
1283
1678
  function styleHandler(rawSource, options) {
1284
- const plugins = getCachedPlugins(options);
1679
+ const plugins = getCachedPipeline(options).plugins;
1285
1680
  const processOptions = getCachedProcessOptions(options);
1286
1681
  return postcss(plugins).process(
1287
1682
  rawSource,
@@ -1294,18 +1689,27 @@ function createStyleHandler(options) {
1294
1689
  getDefaultOptions(options)
1295
1690
  );
1296
1691
  cachedOptions.cssInjectPreflight = createInjectPreflight(cachedOptions.cssPreflight);
1297
- getCachedPlugins(cachedOptions);
1692
+ getCachedPipeline(cachedOptions);
1298
1693
  getCachedProcessOptions(cachedOptions);
1299
- return (rawSource, opt) => {
1694
+ const handler = ((rawSource, opt) => {
1300
1695
  const resolvedOptions = defuOverrideArray3(opt, cachedOptions);
1301
1696
  return styleHandler(
1302
1697
  rawSource,
1303
1698
  resolvedOptions
1304
1699
  );
1700
+ });
1701
+ handler.getPipeline = (opt) => {
1702
+ if (!opt) {
1703
+ return getCachedPipeline(cachedOptions);
1704
+ }
1705
+ const resolvedOptions = defuOverrideArray3(opt, cachedOptions);
1706
+ return getCachedPipeline(resolvedOptions);
1305
1707
  };
1708
+ return handler;
1306
1709
  }
1307
1710
  export {
1308
1711
  createInjectPreflight,
1309
1712
  createStyleHandler,
1713
+ createStylePipeline,
1310
1714
  internalCssSelectorReplacer
1311
1715
  };