@unocss/transformer-directives 0.35.4 → 0.36.0
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.cjs +28 -27
- package/dist/index.mjs +28 -27
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -15,13 +15,14 @@ function transformerDirectives(options = {}) {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
+
const themeFnRE = /theme\((.*?)\)/g;
|
|
18
19
|
async function transformDirectives(code, uno, options, filename, originalCode, offset) {
|
|
19
20
|
const {
|
|
20
21
|
varStyle = "--at-",
|
|
21
22
|
throwOnMissing = true
|
|
22
23
|
} = options;
|
|
23
24
|
const isApply = code.original.includes("@apply") || varStyle !== false && code.original.includes(varStyle);
|
|
24
|
-
const hasThemeFn =
|
|
25
|
+
const hasThemeFn = code.original.match(themeFnRE);
|
|
25
26
|
if (!isApply && !hasThemeFn)
|
|
26
27
|
return;
|
|
27
28
|
const ast = cssTree.parse(originalCode || code.original, {
|
|
@@ -85,37 +86,37 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
|
|
|
85
86
|
code.remove(calcOffset(childNode.loc.start.offset), calcOffset(childNode.loc.end.offset));
|
|
86
87
|
};
|
|
87
88
|
const handleThemeFn = (node) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return
|
|
107
|
-
|
|
108
|
-
throw new Error(`theme of "${childNode.value}" did not found`);
|
|
109
|
-
return null;
|
|
89
|
+
const value = node.value;
|
|
90
|
+
const offset2 = value.loc.start.offset;
|
|
91
|
+
const str = code.original.slice(offset2, value.loc.end.offset);
|
|
92
|
+
const matches = Array.from(str.matchAll(themeFnRE));
|
|
93
|
+
if (!matches.length)
|
|
94
|
+
return;
|
|
95
|
+
for (const match of matches) {
|
|
96
|
+
const rawArg = match[1].trim();
|
|
97
|
+
if (!rawArg)
|
|
98
|
+
throw new Error("theme() expect exact one argument, but got 0");
|
|
99
|
+
let value2 = uno.config.theme;
|
|
100
|
+
const keys = rawArg.slice(1, -1).split(".");
|
|
101
|
+
keys.every((key) => {
|
|
102
|
+
if (value2[key] != null)
|
|
103
|
+
value2 = value2[key];
|
|
104
|
+
else if (value2[+key] != null)
|
|
105
|
+
value2 = value2[+key];
|
|
106
|
+
else
|
|
107
|
+
return false;
|
|
108
|
+
return true;
|
|
110
109
|
});
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
|
|
110
|
+
if (typeof value2 === "string") {
|
|
111
|
+
code.overwrite(offset2 + match.index, offset2 + match.index + match[0].length, value2);
|
|
112
|
+
} else if (throwOnMissing) {
|
|
113
|
+
throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`);
|
|
114
|
+
}
|
|
114
115
|
}
|
|
115
116
|
};
|
|
116
117
|
const stack = [];
|
|
117
118
|
const processNode = async (node, _item, _list) => {
|
|
118
|
-
if (hasThemeFn)
|
|
119
|
+
if (hasThemeFn && node.type === "Declaration")
|
|
119
120
|
handleThemeFn(node);
|
|
120
121
|
if (isApply && node.type === "Rule") {
|
|
121
122
|
await Promise.all(node.block.children.map(async (childNode, _childItem) => {
|
package/dist/index.mjs
CHANGED
|
@@ -11,13 +11,14 @@ function transformerDirectives(options = {}) {
|
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
+
const themeFnRE = /theme\((.*?)\)/g;
|
|
14
15
|
async function transformDirectives(code, uno, options, filename, originalCode, offset) {
|
|
15
16
|
const {
|
|
16
17
|
varStyle = "--at-",
|
|
17
18
|
throwOnMissing = true
|
|
18
19
|
} = options;
|
|
19
20
|
const isApply = code.original.includes("@apply") || varStyle !== false && code.original.includes(varStyle);
|
|
20
|
-
const hasThemeFn =
|
|
21
|
+
const hasThemeFn = code.original.match(themeFnRE);
|
|
21
22
|
if (!isApply && !hasThemeFn)
|
|
22
23
|
return;
|
|
23
24
|
const ast = parse(originalCode || code.original, {
|
|
@@ -81,37 +82,37 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
|
|
|
81
82
|
code.remove(calcOffset(childNode.loc.start.offset), calcOffset(childNode.loc.end.offset));
|
|
82
83
|
};
|
|
83
84
|
const handleThemeFn = (node) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
throw new Error(`theme of "${childNode.value}" did not found`);
|
|
105
|
-
return null;
|
|
85
|
+
const value = node.value;
|
|
86
|
+
const offset2 = value.loc.start.offset;
|
|
87
|
+
const str = code.original.slice(offset2, value.loc.end.offset);
|
|
88
|
+
const matches = Array.from(str.matchAll(themeFnRE));
|
|
89
|
+
if (!matches.length)
|
|
90
|
+
return;
|
|
91
|
+
for (const match of matches) {
|
|
92
|
+
const rawArg = match[1].trim();
|
|
93
|
+
if (!rawArg)
|
|
94
|
+
throw new Error("theme() expect exact one argument, but got 0");
|
|
95
|
+
let value2 = uno.config.theme;
|
|
96
|
+
const keys = rawArg.slice(1, -1).split(".");
|
|
97
|
+
keys.every((key) => {
|
|
98
|
+
if (value2[key] != null)
|
|
99
|
+
value2 = value2[key];
|
|
100
|
+
else if (value2[+key] != null)
|
|
101
|
+
value2 = value2[+key];
|
|
102
|
+
else
|
|
103
|
+
return false;
|
|
104
|
+
return true;
|
|
106
105
|
});
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
if (typeof value2 === "string") {
|
|
107
|
+
code.overwrite(offset2 + match.index, offset2 + match.index + match[0].length, value2);
|
|
108
|
+
} else if (throwOnMissing) {
|
|
109
|
+
throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`);
|
|
110
|
+
}
|
|
110
111
|
}
|
|
111
112
|
};
|
|
112
113
|
const stack = [];
|
|
113
114
|
const processNode = async (node, _item, _list) => {
|
|
114
|
-
if (hasThemeFn)
|
|
115
|
+
if (hasThemeFn && node.type === "Declaration")
|
|
115
116
|
handleThemeFn(node);
|
|
116
117
|
if (isApply && node.type === "Rule") {
|
|
117
118
|
await Promise.all(node.block.children.map(async (childNode, _childItem) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/transformer-directives",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"description": "UnoCSS transformer for `@apply` directive",
|
|
5
5
|
"author": "hannoeru <me@hanlee.co>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unocss/core": "0.
|
|
34
|
+
"@unocss/core": "0.36.0",
|
|
35
35
|
"css-tree": "^2.1.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|