Skip to content

Commit 9450d93

Browse files
committed
feat: implement plugin
1 parent 6c58e4e commit 9450d93

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

index.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
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(/:not\(\[hidden\]\)\s?~\s?:not\(\[hidden\]\)/g, ':not(:last-child)')
50+
})
51+
rule.walkDecls(/(?:margin-(?:top|bottom|right|left|(?:inline-|block-)(?:end|start))|border-(?:top|bottom|right|left|(?:inline-|block-)(?:end|start))-width)/, decl => {
52+
// Grab the value specifier from the declaration value (eg. '1rem' or possibly any arbitrary value)
53+
const valueSpecifierMatch = decl.value.trim().match(/^calc\((.*) \* (?:calc\(1 - )?var\(--tw/)
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

Comments
 (0)