-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
74 lines (65 loc) · 2.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gridSpace from './functions/grid-space';
import responsiveValue from './functions/responsive-value';
import dvh from './functions/dvh';
import svh from './functions/svh';
import lvh from './functions/lvh';
import vw from './functions/vw';
const helpersFunctionsAliasesMap = [
{
functionIdent: 'grid-space',
function: gridSpace
},
{
functionIdent: 'responsive-value',
function: responsiveValue
},
{
functionIdent: 'dvh',
function: dvh
},
{
functionIdent: 'svh',
function: svh
},
{
functionIdent: 'lvh',
function: lvh
},
{
functionIdent: 'vw',
function: vw
}
];
const postcssHelpersFunctions = (options = {}) => {
return {
postcssPlugin: 'postcss-helpers-functions',
async Root(root) {
root.walkDecls((decl) => {
// Create a concatenated regular expression pattern for all functionIdent values
const functionIdents = helpersFunctionsAliasesMap
.map((alias) => {
return alias.functionIdent;
})
.join('|');
const regex = new RegExp(
`\\b(${functionIdents})\\s*\\(\\s*['"]?([^'")]+)?['"]?\\s*\\)`,
'g'
);
// Replace the function with the selected function result
decl.value = decl.value.replace(regex, (match, ident, value) => {
const targetFunction = helpersFunctionsAliasesMap.find((alias) => {
return alias.functionIdent === ident;
});
if (targetFunction) {
const args = value.split(',').map((arg) => arg.trim());
return targetFunction.function.apply(null, args);
}
// Return the original value if no match is found
return match;
});
});
}
};
};
postcssHelpersFunctions.postcss = true;
export default postcssHelpersFunctions;