@una-ui/extractor-vue-script 1.0.0-alpha.1 → 1.0.0-alpha.10

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 +121 -16
  2. package/package.json +9 -2
package/dist/index.mjs CHANGED
@@ -1,33 +1,138 @@
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();
5
10
  return values.filter((v) => Boolean(v) && v !== ":").map((v) => `[${prefix}~="${v}"]`);
6
11
  }
7
- function splitCodeWithArbitraryVariants(code, prefixes) {
8
- const result = [];
12
+ function normalizePrefixes(prefixes) {
9
13
  const camelCasePrefixes = prefixes.map((prefix) => prefix.replace(/-([a-z])/g, (_, c) => c.toUpperCase()));
10
- prefixes = [.../* @__PURE__ */ new Set([...prefixes, ...camelCasePrefixes])];
11
- for (const prefix of prefixes) {
12
- const regex = new RegExp(`\\b${prefix}\\s*:\\s*(?:['"]([^'"]*)['"]|{[^}]*\\bdefault\\s*:\\s*['"]([^'"]*)['"]\\s*,?.*?})`, "gms");
13
- let match;
14
- while (true) {
15
- match = regex.exec(code);
16
- if (match === null)
17
- break;
18
- const values = (match[1] ?? match[2]).split(defaultSplitRE);
19
- const selectors = generateSelectors(prefix, values);
20
- result.push(...selectors);
14
+ return [.../* @__PURE__ */ new Set([...prefixes, ...camelCasePrefixes])];
15
+ }
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 result;
32
+ return void 0;
33
+ }
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) {
46
+ const result = [];
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
+ }
78
+ }
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;
24
108
  }
25
109
  function extractorVueScript(options) {
26
110
  return {
27
111
  name: "@una-ui/extractor-vue-script",
28
112
  order: 0,
29
- async extract({ code }) {
30
- return splitCodeWithArbitraryVariants(code, options?.prefixes ?? []);
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
+ )];
31
136
  }
32
137
  };
33
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@una-ui/extractor-vue-script",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.10",
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.2.0",
45
+ "@unocss/core": "^66.5.1",
39
46
  "vitest": "^3.2.4"
40
47
  },
41
48
  "scripts": {