@una-ui/extractor-vue-script-edge 0.64.0-29260635.86853d3 → 0.64.0-29262088.1cb0816

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.
Files changed (2) hide show
  1. package/dist/index.mjs +81 -34
  2. package/package.json +10 -1
package/dist/index.mjs CHANGED
@@ -1,5 +1,10 @@
1
+ import traverseDefault from '@babel/traverse';
1
2
  import { defaultSplitRE } from '@unocss/core';
3
+ import { parse } from '@vue/compiler-sfc';
4
+ import { parseModule } from 'magicast';
5
+ import { parsePath } from 'ufo';
2
6
 
7
+ const traverse = "default" in traverseDefault ? traverseDefault.default : traverseDefault;
3
8
  function generateSelectors(prefix, values) {
4
9
  prefix = prefix.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
5
10
  return values.filter((v) => Boolean(v) && v !== ":").map((v) => `[${prefix}~="${v}"]`);
@@ -8,50 +13,92 @@ 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 splitCodeWithArbitraryVariants(code, prefixes) {
12
- const result = [];
13
- const normalizedPrefixes = normalizePrefixes(prefixes);
14
- for (const prefix of normalizedPrefixes) {
15
- const regex = new RegExp(`\\b${prefix}\\s*:\\s*(?:['"]([^'"]*)['"]|{[^}]*\\bdefault\\s*:\\s*['"]([^'"]*)['"]\\s*,?.*?})`, "gms");
16
- let match;
17
- while ((match = regex.exec(code)) !== null) {
18
- const values = (match[1] ?? match[2]).split(defaultSplitRE);
19
- const selectors = generateSelectors(prefix, values);
20
- result.push(...selectors);
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("");
22
28
  }
23
- return result;
29
+ if (node.type === "ObjectExpression") {
30
+ return getObjectLiteralValue(node);
31
+ }
32
+ return void 0;
33
+ }
34
+ function getObjectLiteralValue(node) {
35
+ return Object.fromEntries(
36
+ node.properties.filter((prop) => prop.type === "ObjectProperty").filter((prop) => prop.key.type === "Identifier").map((prop) => {
37
+ const key = prop.key.name;
38
+ return [key, getLiteralValue(prop.value)];
39
+ })
40
+ );
24
41
  }
25
- function extractConditionalStatements(code, prefixes) {
42
+ function discoverVariants(node, prefixes) {
26
43
  const result = [];
27
- const normalizedPrefixes = normalizePrefixes(prefixes);
28
- for (const prefix of normalizedPrefixes) {
29
- const ternaryRegex = new RegExp(
30
- `\\b${prefix}\\s*:\\s*[^?]*?\\?\\s*['"\`]([^'"\`]*?)['"\`]\\s*:\\s*['"\`]([^'"\`]*?)['"\`]`,
31
- "gms"
32
- );
33
- let match;
34
- while ((match = ternaryRegex.exec(code)) !== null) {
35
- const [, trueValue, falseValue] = match;
36
- const allValues = [
37
- ...trueValue.split(defaultSplitRE),
38
- ...falseValue.split(defaultSplitRE)
39
- ];
40
- const selectors = generateSelectors(prefix, allValues);
41
- result.push(...selectors);
44
+ traverse(node, {
45
+ noScope: true,
46
+ // scope doesn't work for some reason.
47
+ enter({ node: node2 }) {
48
+ if (node2.type === "ObjectExpression") {
49
+ const object = getObjectLiteralValue(node2);
50
+ for (let [key, value] of Object.entries(object)) {
51
+ if (prefixes.includes(key) && value) {
52
+ if (!Array.isArray(value)) {
53
+ value = [value];
54
+ }
55
+ for (let v of value) {
56
+ if (typeof v === "object" && v !== null && "default" in v) {
57
+ v = v.default;
58
+ }
59
+ if (typeof v !== "string") {
60
+ continue;
61
+ }
62
+ result.push([key, v]);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ if (node2.type === "AssignmentPattern") {
68
+ const { left, right } = node2;
69
+ if (left.type === "Identifier" && right.type === "StringLiteral") {
70
+ const prefix = left.name;
71
+ const value = right.value;
72
+ if (prefixes.includes(prefix)) {
73
+ result.push([prefix, value]);
74
+ }
75
+ }
76
+ }
42
77
  }
43
- }
44
- return result;
78
+ });
79
+ return result.flatMap(([key, v]) => {
80
+ const values = v.split(defaultSplitRE);
81
+ return generateSelectors(key, values);
82
+ });
45
83
  }
46
84
  function extractorVueScript(options) {
47
85
  return {
48
86
  name: "@una-ui/extractor-vue-script",
49
87
  order: 0,
50
- async extract({ code }) {
51
- const prefixes = options?.prefixes ?? [];
52
- const regularResults = splitCodeWithArbitraryVariants(code, prefixes);
53
- const conditionalResults = extractConditionalStatements(code, prefixes);
54
- return [.../* @__PURE__ */ new Set([...regularResults, ...conditionalResults])];
88
+ async extract({ code, id }) {
89
+ const cleanPath = parsePath(id).pathname;
90
+ if (cleanPath.endsWith(".vue")) {
91
+ const sfc = parse(code, {
92
+ filename: cleanPath
93
+ });
94
+ code = sfc.descriptor.scriptSetup?.content || sfc.descriptor.script?.content || "";
95
+ }
96
+ const { $ast: node } = parseModule(code, {
97
+ sourceFileName: id
98
+ });
99
+ const prefixes = normalizePrefixes(options?.prefixes ?? []);
100
+ const regularResults = discoverVariants(node, prefixes);
101
+ return [...new Set(regularResults)];
55
102
  }
56
103
  };
57
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@una-ui/extractor-vue-script-edge",
3
- "version": "0.64.0-29260635.86853d3",
3
+ "version": "0.64.0-29262088.1cb0816",
4
4
  "description": "Unocss extractor for Vue script",
5
5
  "author": "Phojie Rengel <phojrengel@gmail.com>",
6
6
  "license": "MIT",
@@ -34,7 +34,16 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
+ "dependencies": {
38
+ "@babel/parser": "^7.28.3",
39
+ "@babel/traverse": "^7.28.3",
40
+ "@vue/compiler-sfc": "^3.5.18",
41
+ "magicast": "^0.3.5",
42
+ "ufo": "^1.6.1"
43
+ },
37
44
  "devDependencies": {
45
+ "@babel/types": "^7.28.2",
46
+ "@types/babel__traverse": "^7.28.0",
38
47
  "@unocss/core": "^66.4.2",
39
48
  "vitest": "^3.2.4"
40
49
  },