@una-ui/extractor-vue-script-edge 0.64.0-29305804.1d98fb4 → 0.64.0-29313954.f2c5220
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 +49 -15
- package/package.json +4 -6
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as babel from '@babel/types';
|
|
2
2
|
import { defaultSplitRE } from '@unocss/core';
|
|
3
|
+
import { transform, NodeTypes } from '@vue/compiler-core';
|
|
3
4
|
import { parse } from '@vue/compiler-sfc';
|
|
4
5
|
import { parseModule } from 'magicast';
|
|
5
6
|
import { parsePath } from 'ufo';
|
|
6
7
|
|
|
7
|
-
const traverse = "default" in traverseDefault ? traverseDefault.default : traverseDefault;
|
|
8
8
|
function generateSelectors(prefix, values) {
|
|
9
9
|
prefix = prefix.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
10
10
|
return values.filter((v) => Boolean(v) && v !== ":").map((v) => `[${prefix}~="${v}"]`);
|
|
@@ -33,18 +33,19 @@ function getLiteralValue(node) {
|
|
|
33
33
|
}
|
|
34
34
|
function getObjectLiteralValue(node) {
|
|
35
35
|
return Object.fromEntries(
|
|
36
|
-
node.properties.filter((prop) => prop.type === "ObjectProperty").
|
|
37
|
-
const key = prop.key.name;
|
|
38
|
-
|
|
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)]];
|
|
39
42
|
})
|
|
40
43
|
);
|
|
41
44
|
}
|
|
42
45
|
function discoverVariants(node, prefixes) {
|
|
43
46
|
const result = [];
|
|
44
|
-
traverse(node, {
|
|
45
|
-
|
|
46
|
-
// scope doesn't work for some reason.
|
|
47
|
-
enter({ node: node2 }) {
|
|
47
|
+
babel.traverse(node, {
|
|
48
|
+
enter(node2) {
|
|
48
49
|
if (node2.type === "ObjectExpression") {
|
|
49
50
|
const object = getObjectLiteralValue(node2);
|
|
50
51
|
for (let [key, value] of Object.entries(object)) {
|
|
@@ -81,24 +82,57 @@ function discoverVariants(node, prefixes) {
|
|
|
81
82
|
return generateSelectors(key, values);
|
|
82
83
|
});
|
|
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;
|
|
108
|
+
}
|
|
84
109
|
function extractorVueScript(options) {
|
|
85
110
|
return {
|
|
86
111
|
name: "@una-ui/extractor-vue-script",
|
|
87
112
|
order: 0,
|
|
88
113
|
async extract({ code, id }) {
|
|
114
|
+
const astExprs = [];
|
|
89
115
|
const cleanPath = parsePath(id).pathname;
|
|
90
116
|
if (cleanPath.endsWith(".vue")) {
|
|
91
117
|
const sfc = parse(code, {
|
|
92
118
|
filename: cleanPath
|
|
93
119
|
});
|
|
94
|
-
|
|
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));
|
|
95
131
|
}
|
|
96
|
-
const { $ast: node } = parseModule(code, {
|
|
97
|
-
sourceFileName: id
|
|
98
|
-
});
|
|
99
132
|
const prefixes = normalizePrefixes(options?.prefixes ?? []);
|
|
100
|
-
|
|
101
|
-
|
|
133
|
+
return [...new Set(
|
|
134
|
+
astExprs.flatMap((node) => discoverVariants(node, prefixes))
|
|
135
|
+
)];
|
|
102
136
|
}
|
|
103
137
|
};
|
|
104
138
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@una-ui/extractor-vue-script-edge",
|
|
3
|
-
"version": "0.64.0-
|
|
3
|
+
"version": "0.64.0-29313954.f2c5220",
|
|
4
4
|
"description": "Unocss extractor for Vue script",
|
|
5
5
|
"author": "Phojie Rengel <phojrengel@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,15 +35,13 @@
|
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@babel/
|
|
39
|
-
"@
|
|
40
|
-
"@vue/compiler-sfc": "^3.5.
|
|
38
|
+
"@babel/types": "^7.28.2",
|
|
39
|
+
"@vue/compiler-core": "^3.5.22",
|
|
40
|
+
"@vue/compiler-sfc": "^3.5.22",
|
|
41
41
|
"magicast": "^0.3.5",
|
|
42
42
|
"ufo": "^1.6.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@babel/types": "^7.28.2",
|
|
46
|
-
"@types/babel__traverse": "^7.28.0",
|
|
47
45
|
"@unocss/core": "^66.5.1",
|
|
48
46
|
"vitest": "^3.2.4"
|
|
49
47
|
},
|