1+ /**
2+ * Returns the updated declaration value for the optimized selector for a prop and its 'value specifier'
3+ * @param {string } prop
4+ * @param {string } valueSpecifier
5+ *
6+ * @returns {string | undefined }
7+ */
8+ function getUpdatedDeclValue ( prop , valueSpecifier ) {
9+ switch ( prop ) {
10+ case 'border-left-width' :
11+ case 'border-inline-start-width' :
12+ return `calc(${ valueSpecifier } * var(--tw-divide-x-reverse))`
13+ case 'border-right-width' :
14+ case 'border-inline-end-width' :
15+ return `calc(${ valueSpecifier } * calc(1 - var(--tw-divide-x-reverse)))`
16+ case 'border-top-width' :
17+ case 'border-block-start-width' :
18+ return `calc(${ valueSpecifier } * var(--tw-divide-y-reverse))`
19+ case 'border-bottom-width' :
20+ case 'border-block-end-width' :
21+ return `calc(${ valueSpecifier } * calc(1 - var(--tw-divide-y-reverse)))`
22+ case 'margin-top' :
23+ case 'margin-block-start' :
24+ return `calc(${ valueSpecifier } * var(--tw-space-y-reverse))`
25+ case 'margin-bottom' :
26+ case 'margin-block-end' :
27+ return `calc(${ valueSpecifier } * calc(1 - var(--tw-space-y-reverse)))`
28+ case 'margin-left' :
29+ case 'margin-inline-start' :
30+ return `calc(${ valueSpecifier } * var(--tw-space-x-reverse))`
31+ case 'margin-right' :
32+ case 'margin-inline-end' :
33+ return `calc(${ valueSpecifier } * calc(1 - var(--tw-space-x-reverse)))`
34+ default :
35+ return undefined
36+ }
37+ }
38+
139/**
240 * @type {import('postcss').PluginCreator }
341 */
@@ -6,25 +44,21 @@ module.exports = (opts = {}) => {
644
745 return {
846 postcssPlugin : 'postcss-tailwind-space-divide-optimizer' ,
9- /*
10- Root (root, postcss) {
11- // Transform CSS AST here
12- }
13- */
14-
15- /*
16- Declaration (decl, postcss) {
17- // The faster way to find Declaration node
18- }
19- */
20-
21- /*
22- Declaration: {
23- color: (decl, postcss) {
24- // The fastest way find Declaration node if you know property name
25- }
47+ Rule ( rule ) {
48+ rule . selectors = rule . selectors . map ( selector => {
49+ return selector . replaceAll ( / : n o t \( \[ h i d d e n \] \) \s ? ~ \s ? : n o t \( \[ h i d d e n \] \) / g, ':not(:last-child)' )
50+ } )
51+ rule . walkDecls ( / (?: m a r g i n - (?: t o p | b o t t o m | r i g h t | l e f t | (?: i n l i n e - | b l o c k - ) (?: e n d | s t a r t ) ) | b o r d e r - (?: t o p | b o t t o m | r i g h t | l e f t | (?: i n l i n e - | b l o c k - ) (?: e n d | s t a r t ) ) - w i d t h ) / , decl => {
52+ // Grab the value specifier from the declaration value (eg. '1rem' or possibly any arbitrary value)
53+ const valueSpecifierMatch = decl . value . trim ( ) . match ( / ^ c a l c \( ( .* ) \* (?: c a l c \( 1 - ) ? v a r \( - - t w / )
54+ if ( ! valueSpecifierMatch ) { return }
55+ const valueSpecifier = valueSpecifierMatch [ 1 ]
56+ const optimizedValue = getUpdatedDeclValue ( decl . prop , valueSpecifier )
57+ if ( optimizedValue ) {
58+ decl . value = optimizedValue
59+ }
60+ } )
2661 }
27- */
2862 }
2963}
3064
0 commit comments