@una-ui/extractor-vue-script 0.63.1 → 0.65.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.mjs +115 -34
- package/package.json +9 -2
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import * as babel from '@babel/types';
|
|
1
2
|
import { defaultSplitRE } from '@unocss/core';
|
|
3
|
+
import { transform, NodeTypes } from '@vue/compiler-core';
|
|
4
|
+
import { parse } from '@vue/compiler-sfc';
|
|
5
|
+
import { parseModule } from 'magicast';
|
|
6
|
+
import { parsePath } from 'ufo';
|
|
2
7
|
|
|
3
8
|
function generateSelectors(prefix, values) {
|
|
4
9
|
prefix = prefix.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
@@ -8,50 +13,126 @@ function normalizePrefixes(prefixes) {
|
|
|
8
13
|
const camelCasePrefixes = prefixes.map((prefix) => prefix.replace(/-([a-z])/g, (_, c) => c.toUpperCase()));
|
|
9
14
|
return [.../* @__PURE__ */ new Set([...prefixes, ...camelCasePrefixes])];
|
|
10
15
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
function getLiteralValue(node) {
|
|
17
|
+
if (node.type === "ConditionalExpression") {
|
|
18
|
+
return [node.consequent, node.alternate].map(getLiteralValue);
|
|
19
|
+
}
|
|
20
|
+
if (node.type === "StringLiteral") {
|
|
21
|
+
return node.value;
|
|
22
|
+
}
|
|
23
|
+
if (node.type === "TemplateLiteral") {
|
|
24
|
+
if (node.expressions.length > 0) {
|
|
25
|
+
return void 0;
|
|
21
26
|
}
|
|
27
|
+
return node.quasis.map((q) => q.value.cooked).join("");
|
|
28
|
+
}
|
|
29
|
+
if (node.type === "ObjectExpression") {
|
|
30
|
+
return getObjectLiteralValue(node);
|
|
22
31
|
}
|
|
23
|
-
return
|
|
32
|
+
return void 0;
|
|
24
33
|
}
|
|
25
|
-
function
|
|
34
|
+
function getObjectLiteralValue(node) {
|
|
35
|
+
return Object.fromEntries(
|
|
36
|
+
node.properties.filter((prop) => prop.type === "ObjectProperty").flatMap((prop) => {
|
|
37
|
+
const key = prop.key.type === "Identifier" ? prop.key.name : prop.key.type === "StringLiteral" ? prop.key.value : null;
|
|
38
|
+
if (!key) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
return [[key, getLiteralValue(prop.value)]];
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
function discoverVariants(node, prefixes) {
|
|
26
46
|
const result = [];
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
babel.traverse(node, {
|
|
48
|
+
enter(node2) {
|
|
49
|
+
if (node2.type === "ObjectExpression") {
|
|
50
|
+
const object = getObjectLiteralValue(node2);
|
|
51
|
+
for (let [key, value] of Object.entries(object)) {
|
|
52
|
+
if (prefixes.includes(key) && value) {
|
|
53
|
+
if (!Array.isArray(value)) {
|
|
54
|
+
value = [value];
|
|
55
|
+
}
|
|
56
|
+
for (let v of value) {
|
|
57
|
+
if (typeof v === "object" && v !== null && "default" in v) {
|
|
58
|
+
v = v.default;
|
|
59
|
+
}
|
|
60
|
+
if (typeof v !== "string") {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
result.push([key, v]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (node2.type === "AssignmentPattern") {
|
|
69
|
+
const { left, right } = node2;
|
|
70
|
+
if (left.type === "Identifier" && right.type === "StringLiteral") {
|
|
71
|
+
const prefix = left.name;
|
|
72
|
+
const value = right.value;
|
|
73
|
+
if (prefixes.includes(prefix)) {
|
|
74
|
+
result.push([prefix, value]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
42
78
|
}
|
|
43
|
-
}
|
|
44
|
-
return result
|
|
79
|
+
});
|
|
80
|
+
return result.flatMap(([key, v]) => {
|
|
81
|
+
const values = v.split(defaultSplitRE);
|
|
82
|
+
return generateSelectors(key, values);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function parseCodeAst(code, id) {
|
|
86
|
+
const { $ast: node } = parseModule(code, {
|
|
87
|
+
sourceFileName: id
|
|
88
|
+
});
|
|
89
|
+
return node;
|
|
90
|
+
}
|
|
91
|
+
function extractTemplateExpressions(node) {
|
|
92
|
+
const expressions = [];
|
|
93
|
+
transform(node, {
|
|
94
|
+
nodeTransforms: [
|
|
95
|
+
// readonly "transformer", doesn't do any changes
|
|
96
|
+
(node2) => {
|
|
97
|
+
if (node2.type === NodeTypes.ELEMENT) {
|
|
98
|
+
node2.props.forEach((prop) => {
|
|
99
|
+
if (prop.type === NodeTypes.DIRECTIVE && prop.exp?.ast) {
|
|
100
|
+
expressions.push(prop.exp.ast);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
});
|
|
107
|
+
return expressions;
|
|
45
108
|
}
|
|
46
109
|
function extractorVueScript(options) {
|
|
47
110
|
return {
|
|
48
111
|
name: "@una-ui/extractor-vue-script",
|
|
49
112
|
order: 0,
|
|
50
|
-
async extract({ code }) {
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
113
|
+
async extract({ code, id }) {
|
|
114
|
+
const astExprs = [];
|
|
115
|
+
const cleanPath = parsePath(id).pathname;
|
|
116
|
+
if (cleanPath.endsWith(".vue")) {
|
|
117
|
+
const sfc = parse(code, {
|
|
118
|
+
filename: cleanPath
|
|
119
|
+
});
|
|
120
|
+
if (sfc.descriptor.script) {
|
|
121
|
+
astExprs.push(parseCodeAst(sfc.descriptor.script.content));
|
|
122
|
+
}
|
|
123
|
+
if (sfc.descriptor.scriptSetup) {
|
|
124
|
+
astExprs.push(parseCodeAst(sfc.descriptor.scriptSetup.content));
|
|
125
|
+
}
|
|
126
|
+
if (sfc.descriptor.template?.ast) {
|
|
127
|
+
astExprs.push(...extractTemplateExpressions(sfc.descriptor.template.ast));
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
astExprs.push(parseCodeAst(code, id));
|
|
131
|
+
}
|
|
132
|
+
const prefixes = normalizePrefixes(options?.prefixes ?? []);
|
|
133
|
+
return [...new Set(
|
|
134
|
+
astExprs.flatMap((node) => discoverVariants(node, prefixes))
|
|
135
|
+
)];
|
|
55
136
|
}
|
|
56
137
|
};
|
|
57
138
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@una-ui/extractor-vue-script",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.65.0",
|
|
4
4
|
"description": "Unocss extractor for Vue script",
|
|
5
5
|
"author": "Phojie Rengel <phojrengel@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,8 +34,15 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@babel/types": "^7.28.4",
|
|
39
|
+
"@vue/compiler-core": "^3.5.22",
|
|
40
|
+
"@vue/compiler-sfc": "^3.5.22",
|
|
41
|
+
"magicast": "^0.3.5",
|
|
42
|
+
"ufo": "^1.6.1"
|
|
43
|
+
},
|
|
37
44
|
"devDependencies": {
|
|
38
|
-
"@unocss/core": "^66.
|
|
45
|
+
"@unocss/core": "^66.5.1",
|
|
39
46
|
"vitest": "^3.2.4"
|
|
40
47
|
},
|
|
41
48
|
"scripts": {
|