css-typed-vars 0.4.10 → 0.4.12
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/{chunk-YVFKCMBF.js → chunk-2GVOUCFD.js} +86 -11
- package/dist/{chunk-3YUKUAJQ.js → chunk-DWNF2MKL.js} +1 -1
- package/dist/{chunk-K5FAEWQE.js → chunk-KETRI7BY.js} +4 -3
- package/dist/cli.cjs +91 -14
- package/dist/cli.js +4 -3
- package/dist/esbuild.cjs +86 -11
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +89 -13
- package/dist/index.js +2 -2
- package/dist/rollup.cjs +86 -11
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +86 -11
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +86 -11
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +86 -11
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
|
@@ -6,17 +6,89 @@ var getDirname = () => path.dirname(getFilename());
|
|
|
6
6
|
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
7
|
|
|
8
8
|
// src/parser.ts
|
|
9
|
+
function stripComments(text) {
|
|
10
|
+
let result = "";
|
|
11
|
+
let quote = null;
|
|
12
|
+
let urlDepth = 0;
|
|
13
|
+
for (let i = 0; i < text.length; i++) {
|
|
14
|
+
const char = text[i];
|
|
15
|
+
if (quote) {
|
|
16
|
+
result += char;
|
|
17
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
18
|
+
result += text[++i];
|
|
19
|
+
} else if (char === quote) {
|
|
20
|
+
quote = null;
|
|
21
|
+
}
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (urlDepth > 0) {
|
|
25
|
+
if (char === "(") urlDepth++;
|
|
26
|
+
else if (char === ")") urlDepth--;
|
|
27
|
+
result += char;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (char === '"' || char === "'") {
|
|
31
|
+
quote = char;
|
|
32
|
+
result += char;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
36
|
+
urlDepth = 1;
|
|
37
|
+
result += char;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
41
|
+
const end = text.indexOf("*/", i + 2);
|
|
42
|
+
if (end === -1) {
|
|
43
|
+
result += char;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
i = end + 1;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
50
|
+
const nl = text.indexOf("\n", i);
|
|
51
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
result += char;
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
function extractBlock(text, start) {
|
|
59
|
+
let depth = 1;
|
|
60
|
+
let quote = null;
|
|
61
|
+
let i = start;
|
|
62
|
+
for (; i < text.length && depth > 0; i++) {
|
|
63
|
+
const char = text[i];
|
|
64
|
+
if (quote) {
|
|
65
|
+
if (char === "\\") i++;
|
|
66
|
+
else if (char === quote) quote = null;
|
|
67
|
+
} else if (char === '"' || char === "'") {
|
|
68
|
+
quote = char;
|
|
69
|
+
} else if (char === "{") {
|
|
70
|
+
depth++;
|
|
71
|
+
} else if (char === "}") {
|
|
72
|
+
depth--;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
76
|
+
}
|
|
9
77
|
function parseVarNames(css, selectors) {
|
|
10
|
-
const stripped = css
|
|
78
|
+
const stripped = stripComments(css);
|
|
11
79
|
const names = /* @__PURE__ */ new Set();
|
|
12
80
|
const allSelectors = [":root", ...selectors ?? []];
|
|
13
81
|
for (const sel of allSelectors) {
|
|
14
82
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
83
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
84
|
+
let match;
|
|
85
|
+
while (match = openRegex.exec(stripped)) {
|
|
86
|
+
const start = match.index + match[0].length;
|
|
87
|
+
const block = extractBlock(stripped, start);
|
|
88
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
18
89
|
names.add(prop[0]);
|
|
19
90
|
}
|
|
91
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
20
92
|
}
|
|
21
93
|
}
|
|
22
94
|
return [...names];
|
|
@@ -44,17 +116,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
44
116
|
}
|
|
45
117
|
|
|
46
118
|
// src/generator.ts
|
|
119
|
+
function convertCase(value, naming) {
|
|
120
|
+
if (naming === "kebab") return value;
|
|
121
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
122
|
+
}
|
|
47
123
|
function toKey(cssVarName, naming = "camelCase") {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
51
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
124
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
125
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
52
126
|
}
|
|
53
127
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
54
128
|
if (!prefix) return key;
|
|
55
|
-
|
|
56
|
-
if (naming === "
|
|
57
|
-
|
|
129
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
130
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
131
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
132
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
58
133
|
}
|
|
59
134
|
function formatKey(key, naming = "camelCase") {
|
|
60
135
|
if (naming === "kebab") return `'${key}'`;
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
generateDeclaration,
|
|
5
5
|
generateJs,
|
|
6
6
|
scanVarNames
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-2GVOUCFD.js";
|
|
8
8
|
|
|
9
9
|
// src/index.ts
|
|
10
10
|
import { writeFile, mkdir } from "fs/promises";
|
|
@@ -19,9 +19,10 @@ async function generate(options) {
|
|
|
19
19
|
}
|
|
20
20
|
const outPath = resolve(options.output);
|
|
21
21
|
await mkdir(dirname(outPath), { recursive: true });
|
|
22
|
-
|
|
22
|
+
const jsExtMatch = /\.(m|c)?js$/.exec(options.output);
|
|
23
|
+
if (jsExtMatch) {
|
|
23
24
|
await writeFile(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
24
|
-
const dtsPath = outPath.
|
|
25
|
+
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
25
26
|
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
26
27
|
} else {
|
|
27
28
|
await writeFile(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
package/dist/cli.cjs
CHANGED
|
@@ -42,17 +42,89 @@ var import_promises = require("fs/promises");
|
|
|
42
42
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
43
43
|
|
|
44
44
|
// src/parser.ts
|
|
45
|
+
function stripComments(text) {
|
|
46
|
+
let result = "";
|
|
47
|
+
let quote = null;
|
|
48
|
+
let urlDepth = 0;
|
|
49
|
+
for (let i = 0; i < text.length; i++) {
|
|
50
|
+
const char = text[i];
|
|
51
|
+
if (quote) {
|
|
52
|
+
result += char;
|
|
53
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
54
|
+
result += text[++i];
|
|
55
|
+
} else if (char === quote) {
|
|
56
|
+
quote = null;
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (urlDepth > 0) {
|
|
61
|
+
if (char === "(") urlDepth++;
|
|
62
|
+
else if (char === ")") urlDepth--;
|
|
63
|
+
result += char;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (char === '"' || char === "'") {
|
|
67
|
+
quote = char;
|
|
68
|
+
result += char;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
72
|
+
urlDepth = 1;
|
|
73
|
+
result += char;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
77
|
+
const end = text.indexOf("*/", i + 2);
|
|
78
|
+
if (end === -1) {
|
|
79
|
+
result += char;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
i = end + 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
86
|
+
const nl = text.indexOf("\n", i);
|
|
87
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
result += char;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
function extractBlock(text, start) {
|
|
95
|
+
let depth = 1;
|
|
96
|
+
let quote = null;
|
|
97
|
+
let i = start;
|
|
98
|
+
for (; i < text.length && depth > 0; i++) {
|
|
99
|
+
const char = text[i];
|
|
100
|
+
if (quote) {
|
|
101
|
+
if (char === "\\") i++;
|
|
102
|
+
else if (char === quote) quote = null;
|
|
103
|
+
} else if (char === '"' || char === "'") {
|
|
104
|
+
quote = char;
|
|
105
|
+
} else if (char === "{") {
|
|
106
|
+
depth++;
|
|
107
|
+
} else if (char === "}") {
|
|
108
|
+
depth--;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
112
|
+
}
|
|
45
113
|
function parseVarNames(css, selectors) {
|
|
46
|
-
const stripped = css
|
|
114
|
+
const stripped = stripComments(css);
|
|
47
115
|
const names = /* @__PURE__ */ new Set();
|
|
48
116
|
const allSelectors = [":root", ...selectors ?? []];
|
|
49
117
|
for (const sel of allSelectors) {
|
|
50
118
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
119
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
120
|
+
let match;
|
|
121
|
+
while (match = openRegex.exec(stripped)) {
|
|
122
|
+
const start = match.index + match[0].length;
|
|
123
|
+
const block = extractBlock(stripped, start);
|
|
124
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
54
125
|
names.add(prop[0]);
|
|
55
126
|
}
|
|
127
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
56
128
|
}
|
|
57
129
|
}
|
|
58
130
|
return [...names];
|
|
@@ -78,17 +150,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
78
150
|
}
|
|
79
151
|
|
|
80
152
|
// src/generator.ts
|
|
153
|
+
function convertCase(value, naming) {
|
|
154
|
+
if (naming === "kebab") return value;
|
|
155
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
156
|
+
}
|
|
81
157
|
function toKey(cssVarName, naming = "camelCase") {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
85
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
158
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
159
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
86
160
|
}
|
|
87
161
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
88
162
|
if (!prefix) return key;
|
|
89
|
-
|
|
90
|
-
if (naming === "
|
|
91
|
-
|
|
163
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
164
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
165
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
166
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
92
167
|
}
|
|
93
168
|
function formatKey(key, naming = "camelCase") {
|
|
94
169
|
if (naming === "kebab") return `'${key}'`;
|
|
@@ -160,9 +235,10 @@ async function generate(options) {
|
|
|
160
235
|
}
|
|
161
236
|
const outPath = (0, import_node_path.resolve)(options.output);
|
|
162
237
|
await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
|
|
163
|
-
|
|
238
|
+
const jsExtMatch = /\.(m|c)?js$/.exec(options.output);
|
|
239
|
+
if (jsExtMatch) {
|
|
164
240
|
await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
165
|
-
const dtsPath = outPath.
|
|
241
|
+
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
166
242
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
167
243
|
} else {
|
|
168
244
|
await (0, import_promises2.writeFile)(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
|
@@ -247,7 +323,8 @@ async function main() {
|
|
|
247
323
|
console.log(`${label}: ${file}`);
|
|
248
324
|
run(input, output, exclude, prefix, naming, selectors).catch(console.error);
|
|
249
325
|
};
|
|
250
|
-
|
|
326
|
+
const ignored = exclude ? Array.isArray(exclude) ? exclude : [exclude] : void 0;
|
|
327
|
+
(0, import_chokidar.watch)(patterns, ignored ? { ignored } : void 0).on("change", makeHandler("Changed")).on("add", makeHandler("Added")).on("unlink", makeHandler("Removed"));
|
|
251
328
|
console.log("Watching for changes...");
|
|
252
329
|
}
|
|
253
330
|
}
|
package/dist/cli.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
generate
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import "./chunk-
|
|
4
|
+
} from "./chunk-KETRI7BY.js";
|
|
5
|
+
import "./chunk-2GVOUCFD.js";
|
|
6
6
|
|
|
7
7
|
// src/cli.ts
|
|
8
8
|
import { readFile } from "fs/promises";
|
|
@@ -86,7 +86,8 @@ async function main() {
|
|
|
86
86
|
console.log(`${label}: ${file}`);
|
|
87
87
|
run(input, output, exclude, prefix, naming, selectors).catch(console.error);
|
|
88
88
|
};
|
|
89
|
-
|
|
89
|
+
const ignored = exclude ? Array.isArray(exclude) ? exclude : [exclude] : void 0;
|
|
90
|
+
watch(patterns, ignored ? { ignored } : void 0).on("change", makeHandler("Changed")).on("add", makeHandler("Added")).on("unlink", makeHandler("Removed"));
|
|
90
91
|
console.log("Watching for changes...");
|
|
91
92
|
}
|
|
92
93
|
}
|
package/dist/esbuild.cjs
CHANGED
|
@@ -44,17 +44,89 @@ var import_promises = require("fs/promises");
|
|
|
44
44
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
45
|
|
|
46
46
|
// src/parser.ts
|
|
47
|
+
function stripComments(text) {
|
|
48
|
+
let result = "";
|
|
49
|
+
let quote = null;
|
|
50
|
+
let urlDepth = 0;
|
|
51
|
+
for (let i = 0; i < text.length; i++) {
|
|
52
|
+
const char = text[i];
|
|
53
|
+
if (quote) {
|
|
54
|
+
result += char;
|
|
55
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
56
|
+
result += text[++i];
|
|
57
|
+
} else if (char === quote) {
|
|
58
|
+
quote = null;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (urlDepth > 0) {
|
|
63
|
+
if (char === "(") urlDepth++;
|
|
64
|
+
else if (char === ")") urlDepth--;
|
|
65
|
+
result += char;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (char === '"' || char === "'") {
|
|
69
|
+
quote = char;
|
|
70
|
+
result += char;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
74
|
+
urlDepth = 1;
|
|
75
|
+
result += char;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
79
|
+
const end = text.indexOf("*/", i + 2);
|
|
80
|
+
if (end === -1) {
|
|
81
|
+
result += char;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
i = end + 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
88
|
+
const nl = text.indexOf("\n", i);
|
|
89
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
result += char;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function extractBlock(text, start) {
|
|
97
|
+
let depth = 1;
|
|
98
|
+
let quote = null;
|
|
99
|
+
let i = start;
|
|
100
|
+
for (; i < text.length && depth > 0; i++) {
|
|
101
|
+
const char = text[i];
|
|
102
|
+
if (quote) {
|
|
103
|
+
if (char === "\\") i++;
|
|
104
|
+
else if (char === quote) quote = null;
|
|
105
|
+
} else if (char === '"' || char === "'") {
|
|
106
|
+
quote = char;
|
|
107
|
+
} else if (char === "{") {
|
|
108
|
+
depth++;
|
|
109
|
+
} else if (char === "}") {
|
|
110
|
+
depth--;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
114
|
+
}
|
|
47
115
|
function parseVarNames(css, selectors) {
|
|
48
|
-
const stripped = css
|
|
116
|
+
const stripped = stripComments(css);
|
|
49
117
|
const names = /* @__PURE__ */ new Set();
|
|
50
118
|
const allSelectors = [":root", ...selectors ?? []];
|
|
51
119
|
for (const sel of allSelectors) {
|
|
52
120
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
121
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
122
|
+
let match;
|
|
123
|
+
while (match = openRegex.exec(stripped)) {
|
|
124
|
+
const start = match.index + match[0].length;
|
|
125
|
+
const block = extractBlock(stripped, start);
|
|
126
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
56
127
|
names.add(prop[0]);
|
|
57
128
|
}
|
|
129
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
58
130
|
}
|
|
59
131
|
}
|
|
60
132
|
return [...names];
|
|
@@ -80,17 +152,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
80
152
|
}
|
|
81
153
|
|
|
82
154
|
// src/generator.ts
|
|
155
|
+
function convertCase(value, naming) {
|
|
156
|
+
if (naming === "kebab") return value;
|
|
157
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
158
|
+
}
|
|
83
159
|
function toKey(cssVarName, naming = "camelCase") {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
87
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
160
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
161
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
88
162
|
}
|
|
89
163
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
90
164
|
if (!prefix) return key;
|
|
91
|
-
|
|
92
|
-
if (naming === "
|
|
93
|
-
|
|
165
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
166
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
167
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
168
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
94
169
|
}
|
|
95
170
|
function formatKey(key, naming = "camelCase") {
|
|
96
171
|
if (naming === "kebab") return `'${key}'`;
|
package/dist/esbuild.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -46,17 +46,89 @@ var import_promises = require("fs/promises");
|
|
|
46
46
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
47
47
|
|
|
48
48
|
// src/parser.ts
|
|
49
|
+
function stripComments(text) {
|
|
50
|
+
let result = "";
|
|
51
|
+
let quote = null;
|
|
52
|
+
let urlDepth = 0;
|
|
53
|
+
for (let i = 0; i < text.length; i++) {
|
|
54
|
+
const char = text[i];
|
|
55
|
+
if (quote) {
|
|
56
|
+
result += char;
|
|
57
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
58
|
+
result += text[++i];
|
|
59
|
+
} else if (char === quote) {
|
|
60
|
+
quote = null;
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (urlDepth > 0) {
|
|
65
|
+
if (char === "(") urlDepth++;
|
|
66
|
+
else if (char === ")") urlDepth--;
|
|
67
|
+
result += char;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (char === '"' || char === "'") {
|
|
71
|
+
quote = char;
|
|
72
|
+
result += char;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
76
|
+
urlDepth = 1;
|
|
77
|
+
result += char;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
81
|
+
const end = text.indexOf("*/", i + 2);
|
|
82
|
+
if (end === -1) {
|
|
83
|
+
result += char;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
i = end + 1;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
90
|
+
const nl = text.indexOf("\n", i);
|
|
91
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
result += char;
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
function extractBlock(text, start) {
|
|
99
|
+
let depth = 1;
|
|
100
|
+
let quote = null;
|
|
101
|
+
let i = start;
|
|
102
|
+
for (; i < text.length && depth > 0; i++) {
|
|
103
|
+
const char = text[i];
|
|
104
|
+
if (quote) {
|
|
105
|
+
if (char === "\\") i++;
|
|
106
|
+
else if (char === quote) quote = null;
|
|
107
|
+
} else if (char === '"' || char === "'") {
|
|
108
|
+
quote = char;
|
|
109
|
+
} else if (char === "{") {
|
|
110
|
+
depth++;
|
|
111
|
+
} else if (char === "}") {
|
|
112
|
+
depth--;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
116
|
+
}
|
|
49
117
|
function parseVarNames(css, selectors) {
|
|
50
|
-
const stripped = css
|
|
118
|
+
const stripped = stripComments(css);
|
|
51
119
|
const names = /* @__PURE__ */ new Set();
|
|
52
120
|
const allSelectors = [":root", ...selectors ?? []];
|
|
53
121
|
for (const sel of allSelectors) {
|
|
54
122
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
123
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
124
|
+
let match;
|
|
125
|
+
while (match = openRegex.exec(stripped)) {
|
|
126
|
+
const start = match.index + match[0].length;
|
|
127
|
+
const block = extractBlock(stripped, start);
|
|
128
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
58
129
|
names.add(prop[0]);
|
|
59
130
|
}
|
|
131
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
60
132
|
}
|
|
61
133
|
}
|
|
62
134
|
return [...names];
|
|
@@ -82,17 +154,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
82
154
|
}
|
|
83
155
|
|
|
84
156
|
// src/generator.ts
|
|
157
|
+
function convertCase(value, naming) {
|
|
158
|
+
if (naming === "kebab") return value;
|
|
159
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
160
|
+
}
|
|
85
161
|
function toKey(cssVarName, naming = "camelCase") {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
89
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
162
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
163
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
90
164
|
}
|
|
91
165
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
92
166
|
if (!prefix) return key;
|
|
93
|
-
|
|
94
|
-
if (naming === "
|
|
95
|
-
|
|
167
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
168
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
169
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
170
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
96
171
|
}
|
|
97
172
|
function formatKey(key, naming = "camelCase") {
|
|
98
173
|
if (naming === "kebab") return `'${key}'`;
|
|
@@ -164,9 +239,10 @@ async function generate(options) {
|
|
|
164
239
|
}
|
|
165
240
|
const outPath = (0, import_node_path.resolve)(options.output);
|
|
166
241
|
await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
|
|
167
|
-
|
|
242
|
+
const jsExtMatch = /\.(m|c)?js$/.exec(options.output);
|
|
243
|
+
if (jsExtMatch) {
|
|
168
244
|
await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
169
|
-
const dtsPath = outPath.
|
|
245
|
+
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
170
246
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
171
247
|
} else {
|
|
172
248
|
await (0, import_promises2.writeFile)(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
generate
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-KETRI7BY.js";
|
|
4
4
|
import {
|
|
5
5
|
generateCode,
|
|
6
6
|
generateDeclaration,
|
|
7
7
|
generateJs,
|
|
8
8
|
parseVarNames,
|
|
9
9
|
scanVarNames
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-2GVOUCFD.js";
|
|
11
11
|
export {
|
|
12
12
|
generate,
|
|
13
13
|
generateCode,
|
package/dist/rollup.cjs
CHANGED
|
@@ -44,17 +44,89 @@ var import_promises = require("fs/promises");
|
|
|
44
44
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
45
|
|
|
46
46
|
// src/parser.ts
|
|
47
|
+
function stripComments(text) {
|
|
48
|
+
let result = "";
|
|
49
|
+
let quote = null;
|
|
50
|
+
let urlDepth = 0;
|
|
51
|
+
for (let i = 0; i < text.length; i++) {
|
|
52
|
+
const char = text[i];
|
|
53
|
+
if (quote) {
|
|
54
|
+
result += char;
|
|
55
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
56
|
+
result += text[++i];
|
|
57
|
+
} else if (char === quote) {
|
|
58
|
+
quote = null;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (urlDepth > 0) {
|
|
63
|
+
if (char === "(") urlDepth++;
|
|
64
|
+
else if (char === ")") urlDepth--;
|
|
65
|
+
result += char;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (char === '"' || char === "'") {
|
|
69
|
+
quote = char;
|
|
70
|
+
result += char;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
74
|
+
urlDepth = 1;
|
|
75
|
+
result += char;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
79
|
+
const end = text.indexOf("*/", i + 2);
|
|
80
|
+
if (end === -1) {
|
|
81
|
+
result += char;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
i = end + 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
88
|
+
const nl = text.indexOf("\n", i);
|
|
89
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
result += char;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function extractBlock(text, start) {
|
|
97
|
+
let depth = 1;
|
|
98
|
+
let quote = null;
|
|
99
|
+
let i = start;
|
|
100
|
+
for (; i < text.length && depth > 0; i++) {
|
|
101
|
+
const char = text[i];
|
|
102
|
+
if (quote) {
|
|
103
|
+
if (char === "\\") i++;
|
|
104
|
+
else if (char === quote) quote = null;
|
|
105
|
+
} else if (char === '"' || char === "'") {
|
|
106
|
+
quote = char;
|
|
107
|
+
} else if (char === "{") {
|
|
108
|
+
depth++;
|
|
109
|
+
} else if (char === "}") {
|
|
110
|
+
depth--;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
114
|
+
}
|
|
47
115
|
function parseVarNames(css, selectors) {
|
|
48
|
-
const stripped = css
|
|
116
|
+
const stripped = stripComments(css);
|
|
49
117
|
const names = /* @__PURE__ */ new Set();
|
|
50
118
|
const allSelectors = [":root", ...selectors ?? []];
|
|
51
119
|
for (const sel of allSelectors) {
|
|
52
120
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
121
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
122
|
+
let match;
|
|
123
|
+
while (match = openRegex.exec(stripped)) {
|
|
124
|
+
const start = match.index + match[0].length;
|
|
125
|
+
const block = extractBlock(stripped, start);
|
|
126
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
56
127
|
names.add(prop[0]);
|
|
57
128
|
}
|
|
129
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
58
130
|
}
|
|
59
131
|
}
|
|
60
132
|
return [...names];
|
|
@@ -80,17 +152,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
80
152
|
}
|
|
81
153
|
|
|
82
154
|
// src/generator.ts
|
|
155
|
+
function convertCase(value, naming) {
|
|
156
|
+
if (naming === "kebab") return value;
|
|
157
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
158
|
+
}
|
|
83
159
|
function toKey(cssVarName, naming = "camelCase") {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
87
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
160
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
161
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
88
162
|
}
|
|
89
163
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
90
164
|
if (!prefix) return key;
|
|
91
|
-
|
|
92
|
-
if (naming === "
|
|
93
|
-
|
|
165
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
166
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
167
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
168
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
94
169
|
}
|
|
95
170
|
function formatKey(key, naming = "camelCase") {
|
|
96
171
|
if (naming === "kebab") return `'${key}'`;
|
package/dist/rollup.js
CHANGED
package/dist/unplugin.cjs
CHANGED
|
@@ -42,17 +42,89 @@ var import_promises = require("fs/promises");
|
|
|
42
42
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
43
43
|
|
|
44
44
|
// src/parser.ts
|
|
45
|
+
function stripComments(text) {
|
|
46
|
+
let result = "";
|
|
47
|
+
let quote = null;
|
|
48
|
+
let urlDepth = 0;
|
|
49
|
+
for (let i = 0; i < text.length; i++) {
|
|
50
|
+
const char = text[i];
|
|
51
|
+
if (quote) {
|
|
52
|
+
result += char;
|
|
53
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
54
|
+
result += text[++i];
|
|
55
|
+
} else if (char === quote) {
|
|
56
|
+
quote = null;
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (urlDepth > 0) {
|
|
61
|
+
if (char === "(") urlDepth++;
|
|
62
|
+
else if (char === ")") urlDepth--;
|
|
63
|
+
result += char;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (char === '"' || char === "'") {
|
|
67
|
+
quote = char;
|
|
68
|
+
result += char;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
72
|
+
urlDepth = 1;
|
|
73
|
+
result += char;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
77
|
+
const end = text.indexOf("*/", i + 2);
|
|
78
|
+
if (end === -1) {
|
|
79
|
+
result += char;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
i = end + 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
86
|
+
const nl = text.indexOf("\n", i);
|
|
87
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
result += char;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
function extractBlock(text, start) {
|
|
95
|
+
let depth = 1;
|
|
96
|
+
let quote = null;
|
|
97
|
+
let i = start;
|
|
98
|
+
for (; i < text.length && depth > 0; i++) {
|
|
99
|
+
const char = text[i];
|
|
100
|
+
if (quote) {
|
|
101
|
+
if (char === "\\") i++;
|
|
102
|
+
else if (char === quote) quote = null;
|
|
103
|
+
} else if (char === '"' || char === "'") {
|
|
104
|
+
quote = char;
|
|
105
|
+
} else if (char === "{") {
|
|
106
|
+
depth++;
|
|
107
|
+
} else if (char === "}") {
|
|
108
|
+
depth--;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
112
|
+
}
|
|
45
113
|
function parseVarNames(css, selectors) {
|
|
46
|
-
const stripped = css
|
|
114
|
+
const stripped = stripComments(css);
|
|
47
115
|
const names = /* @__PURE__ */ new Set();
|
|
48
116
|
const allSelectors = [":root", ...selectors ?? []];
|
|
49
117
|
for (const sel of allSelectors) {
|
|
50
118
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
119
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
120
|
+
let match;
|
|
121
|
+
while (match = openRegex.exec(stripped)) {
|
|
122
|
+
const start = match.index + match[0].length;
|
|
123
|
+
const block = extractBlock(stripped, start);
|
|
124
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
54
125
|
names.add(prop[0]);
|
|
55
126
|
}
|
|
127
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
56
128
|
}
|
|
57
129
|
}
|
|
58
130
|
return [...names];
|
|
@@ -78,17 +150,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
78
150
|
}
|
|
79
151
|
|
|
80
152
|
// src/generator.ts
|
|
153
|
+
function convertCase(value, naming) {
|
|
154
|
+
if (naming === "kebab") return value;
|
|
155
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
156
|
+
}
|
|
81
157
|
function toKey(cssVarName, naming = "camelCase") {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
85
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
158
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
159
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
86
160
|
}
|
|
87
161
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
88
162
|
if (!prefix) return key;
|
|
89
|
-
|
|
90
|
-
if (naming === "
|
|
91
|
-
|
|
163
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
164
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
165
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
166
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
92
167
|
}
|
|
93
168
|
function formatKey(key, naming = "camelCase") {
|
|
94
169
|
if (naming === "kebab") return `'${key}'`;
|
package/dist/unplugin.js
CHANGED
package/dist/vite.cjs
CHANGED
|
@@ -44,17 +44,89 @@ var import_promises = require("fs/promises");
|
|
|
44
44
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
45
|
|
|
46
46
|
// src/parser.ts
|
|
47
|
+
function stripComments(text) {
|
|
48
|
+
let result = "";
|
|
49
|
+
let quote = null;
|
|
50
|
+
let urlDepth = 0;
|
|
51
|
+
for (let i = 0; i < text.length; i++) {
|
|
52
|
+
const char = text[i];
|
|
53
|
+
if (quote) {
|
|
54
|
+
result += char;
|
|
55
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
56
|
+
result += text[++i];
|
|
57
|
+
} else if (char === quote) {
|
|
58
|
+
quote = null;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (urlDepth > 0) {
|
|
63
|
+
if (char === "(") urlDepth++;
|
|
64
|
+
else if (char === ")") urlDepth--;
|
|
65
|
+
result += char;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (char === '"' || char === "'") {
|
|
69
|
+
quote = char;
|
|
70
|
+
result += char;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
74
|
+
urlDepth = 1;
|
|
75
|
+
result += char;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
79
|
+
const end = text.indexOf("*/", i + 2);
|
|
80
|
+
if (end === -1) {
|
|
81
|
+
result += char;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
i = end + 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
88
|
+
const nl = text.indexOf("\n", i);
|
|
89
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
result += char;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function extractBlock(text, start) {
|
|
97
|
+
let depth = 1;
|
|
98
|
+
let quote = null;
|
|
99
|
+
let i = start;
|
|
100
|
+
for (; i < text.length && depth > 0; i++) {
|
|
101
|
+
const char = text[i];
|
|
102
|
+
if (quote) {
|
|
103
|
+
if (char === "\\") i++;
|
|
104
|
+
else if (char === quote) quote = null;
|
|
105
|
+
} else if (char === '"' || char === "'") {
|
|
106
|
+
quote = char;
|
|
107
|
+
} else if (char === "{") {
|
|
108
|
+
depth++;
|
|
109
|
+
} else if (char === "}") {
|
|
110
|
+
depth--;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
114
|
+
}
|
|
47
115
|
function parseVarNames(css, selectors) {
|
|
48
|
-
const stripped = css
|
|
116
|
+
const stripped = stripComments(css);
|
|
49
117
|
const names = /* @__PURE__ */ new Set();
|
|
50
118
|
const allSelectors = [":root", ...selectors ?? []];
|
|
51
119
|
for (const sel of allSelectors) {
|
|
52
120
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
121
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
122
|
+
let match;
|
|
123
|
+
while (match = openRegex.exec(stripped)) {
|
|
124
|
+
const start = match.index + match[0].length;
|
|
125
|
+
const block = extractBlock(stripped, start);
|
|
126
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
56
127
|
names.add(prop[0]);
|
|
57
128
|
}
|
|
129
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
58
130
|
}
|
|
59
131
|
}
|
|
60
132
|
return [...names];
|
|
@@ -80,17 +152,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
80
152
|
}
|
|
81
153
|
|
|
82
154
|
// src/generator.ts
|
|
155
|
+
function convertCase(value, naming) {
|
|
156
|
+
if (naming === "kebab") return value;
|
|
157
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
158
|
+
}
|
|
83
159
|
function toKey(cssVarName, naming = "camelCase") {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
87
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
160
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
161
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
88
162
|
}
|
|
89
163
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
90
164
|
if (!prefix) return key;
|
|
91
|
-
|
|
92
|
-
if (naming === "
|
|
93
|
-
|
|
165
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
166
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
167
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
168
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
94
169
|
}
|
|
95
170
|
function formatKey(key, naming = "camelCase") {
|
|
96
171
|
if (naming === "kebab") return `'${key}'`;
|
package/dist/vite.js
CHANGED
package/dist/webpack.cjs
CHANGED
|
@@ -44,17 +44,89 @@ var import_promises = require("fs/promises");
|
|
|
44
44
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
45
|
|
|
46
46
|
// src/parser.ts
|
|
47
|
+
function stripComments(text) {
|
|
48
|
+
let result = "";
|
|
49
|
+
let quote = null;
|
|
50
|
+
let urlDepth = 0;
|
|
51
|
+
for (let i = 0; i < text.length; i++) {
|
|
52
|
+
const char = text[i];
|
|
53
|
+
if (quote) {
|
|
54
|
+
result += char;
|
|
55
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
56
|
+
result += text[++i];
|
|
57
|
+
} else if (char === quote) {
|
|
58
|
+
quote = null;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (urlDepth > 0) {
|
|
63
|
+
if (char === "(") urlDepth++;
|
|
64
|
+
else if (char === ")") urlDepth--;
|
|
65
|
+
result += char;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (char === '"' || char === "'") {
|
|
69
|
+
quote = char;
|
|
70
|
+
result += char;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
74
|
+
urlDepth = 1;
|
|
75
|
+
result += char;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
79
|
+
const end = text.indexOf("*/", i + 2);
|
|
80
|
+
if (end === -1) {
|
|
81
|
+
result += char;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
i = end + 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
88
|
+
const nl = text.indexOf("\n", i);
|
|
89
|
+
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
result += char;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function extractBlock(text, start) {
|
|
97
|
+
let depth = 1;
|
|
98
|
+
let quote = null;
|
|
99
|
+
let i = start;
|
|
100
|
+
for (; i < text.length && depth > 0; i++) {
|
|
101
|
+
const char = text[i];
|
|
102
|
+
if (quote) {
|
|
103
|
+
if (char === "\\") i++;
|
|
104
|
+
else if (char === quote) quote = null;
|
|
105
|
+
} else if (char === '"' || char === "'") {
|
|
106
|
+
quote = char;
|
|
107
|
+
} else if (char === "{") {
|
|
108
|
+
depth++;
|
|
109
|
+
} else if (char === "}") {
|
|
110
|
+
depth--;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
114
|
+
}
|
|
47
115
|
function parseVarNames(css, selectors) {
|
|
48
|
-
const stripped = css
|
|
116
|
+
const stripped = stripComments(css);
|
|
49
117
|
const names = /* @__PURE__ */ new Set();
|
|
50
118
|
const allSelectors = [":root", ...selectors ?? []];
|
|
51
119
|
for (const sel of allSelectors) {
|
|
52
120
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
121
|
+
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
122
|
+
let match;
|
|
123
|
+
while (match = openRegex.exec(stripped)) {
|
|
124
|
+
const start = match.index + match[0].length;
|
|
125
|
+
const block = extractBlock(stripped, start);
|
|
126
|
+
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
56
127
|
names.add(prop[0]);
|
|
57
128
|
}
|
|
129
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
58
130
|
}
|
|
59
131
|
}
|
|
60
132
|
return [...names];
|
|
@@ -80,17 +152,20 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
80
152
|
}
|
|
81
153
|
|
|
82
154
|
// src/generator.ts
|
|
155
|
+
function convertCase(value, naming) {
|
|
156
|
+
if (naming === "kebab") return value;
|
|
157
|
+
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
158
|
+
}
|
|
83
159
|
function toKey(cssVarName, naming = "camelCase") {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const key = naming === "snake" ? stripped.replace(/-+/g, "_") : stripped.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
87
|
-
return /^\d/.test(key) ? `_${key}` : key;
|
|
160
|
+
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
161
|
+
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
88
162
|
}
|
|
89
163
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
90
164
|
if (!prefix) return key;
|
|
91
|
-
|
|
92
|
-
if (naming === "
|
|
93
|
-
|
|
165
|
+
const normalizedPrefix = convertCase(prefix, naming);
|
|
166
|
+
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
167
|
+
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
168
|
+
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
94
169
|
}
|
|
95
170
|
function formatKey(key, naming = "camelCase") {
|
|
96
171
|
if (naming === "kebab") return `'${key}'`;
|
package/dist/webpack.js
CHANGED