-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
47 lines (40 loc) · 1.27 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
import plugin from 'tailwindcss/plugin.js';
type Options = Record<string, any>;
export default plugin.withOptions(function (options: Options = {}) {
return function ({ matchUtilities, theme, config }) {
const context = {
theme,
config,
...(options ?? {}),
};
matchUtilities({
js: (value) => {
const escape = (str: string) => {
return str.replace(/_/g, '\\_').replace(/ /g, '_');
};
const unescape = (str: string) => {
return str.replace(/(?<!\\)_/g, ' ').replace(/\\_/g, '_');
};
const parseString = (str: string) => {
return str.split(/(#{.*?})/g).map((el, i) => (i % 2 === 1 ? el.slice(2, -1) : el));
};
const parts = parseString(escape(value.slice(1, -1)));
const utility = parts
.map((part, i) => {
if (i % 2 === 0) {
return part;
} else {
const args = Object.keys(context);
const values = Object.values(context);
const func = new Function(...args, `return ${unescape(part)};`);
return escape(`${func(...values)}`);
}
})
.join('');
return {
[`@apply ${utility}`]: {},
};
},
});
};
});