css-typed-vars 0.4.17 → 0.6.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/README.md +74 -6
- package/dist/chunk-LH53WYFF.js +487 -0
- package/dist/{chunk-DOOYTSTY.js → chunk-TD7SYMDA.js} +38 -14
- package/dist/chunk-VZ2BHHXD.js +86 -0
- package/dist/cli.cjs +352 -47
- package/dist/cli.js +59 -8
- package/dist/esbuild.cjs +266 -42
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/generator-C2AppeJL.d.cts +15 -0
- package/dist/generator-C2AppeJL.d.ts +15 -0
- package/dist/index.cjs +319 -40
- package/dist/index.d.cts +18 -5
- package/dist/index.d.ts +18 -5
- package/dist/index.js +16 -2
- package/dist/rollup.cjs +266 -42
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +266 -42
- package/dist/unplugin.d.cts +7 -1
- package/dist/unplugin.d.ts +7 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +266 -42
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +266 -42
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +6 -2
- package/dist/chunk-EHRNYU34.js +0 -32
- package/dist/chunk-WYLYZXQJ.js +0 -265
- package/dist/generator-Ba8XeISD.d.cts +0 -6
- package/dist/generator-Ba8XeISD.d.ts +0 -6
package/dist/chunk-WYLYZXQJ.js
DELETED
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
// node_modules/tsup/assets/esm_shims.js
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
var getFilename = () => fileURLToPath(import.meta.url);
|
|
5
|
-
var getDirname = () => path.dirname(getFilename());
|
|
6
|
-
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
|
-
|
|
8
|
-
// src/parser.ts
|
|
9
|
-
function stepQuote(text, i, quote) {
|
|
10
|
-
const char = text[i];
|
|
11
|
-
if (char === "\\" && i + 1 < text.length) return { consumed: 2, closed: false };
|
|
12
|
-
if (char === quote) return { consumed: 1, closed: true };
|
|
13
|
-
return { consumed: 1, closed: false };
|
|
14
|
-
}
|
|
15
|
-
function stripComments(text) {
|
|
16
|
-
let result = "";
|
|
17
|
-
let quote = null;
|
|
18
|
-
let urlDepth = 0;
|
|
19
|
-
for (let i = 0; i < text.length; i++) {
|
|
20
|
-
const char = text[i];
|
|
21
|
-
if (quote) {
|
|
22
|
-
const step = stepQuote(text, i, quote);
|
|
23
|
-
result += text.slice(i, i + step.consumed);
|
|
24
|
-
if (step.closed) quote = null;
|
|
25
|
-
i += step.consumed - 1;
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
if (char === '"' || char === "'") {
|
|
29
|
-
quote = char;
|
|
30
|
-
result += char;
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
if (urlDepth > 0) {
|
|
34
|
-
if (char === "(") urlDepth++;
|
|
35
|
-
else if (char === ")") urlDepth--;
|
|
36
|
-
result += char;
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
40
|
-
urlDepth = 1;
|
|
41
|
-
result += char;
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
if (char === "/" && text[i + 1] === "*") {
|
|
45
|
-
const end = text.indexOf("*/", i + 2);
|
|
46
|
-
if (end === -1) {
|
|
47
|
-
result += char;
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
i = end + 1;
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
54
|
-
const nl = text.indexOf("\n", i);
|
|
55
|
-
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
result += char;
|
|
59
|
-
}
|
|
60
|
-
return result;
|
|
61
|
-
}
|
|
62
|
-
function extractBlock(text, start) {
|
|
63
|
-
let depth = 1;
|
|
64
|
-
let quote = null;
|
|
65
|
-
let i = start;
|
|
66
|
-
for (; i < text.length && depth > 0; i++) {
|
|
67
|
-
const char = text[i];
|
|
68
|
-
if (quote) {
|
|
69
|
-
const step = stepQuote(text, i, quote);
|
|
70
|
-
if (step.closed) quote = null;
|
|
71
|
-
i += step.consumed - 1;
|
|
72
|
-
} else if (char === '"' || char === "'") {
|
|
73
|
-
quote = char;
|
|
74
|
-
} else if (char === "{") {
|
|
75
|
-
depth++;
|
|
76
|
-
} else if (char === "}") {
|
|
77
|
-
depth--;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
81
|
-
}
|
|
82
|
-
function maskQuotedContent(text) {
|
|
83
|
-
let result = "";
|
|
84
|
-
let quote = null;
|
|
85
|
-
for (let i = 0; i < text.length; i++) {
|
|
86
|
-
const char = text[i];
|
|
87
|
-
if (quote) {
|
|
88
|
-
const step = stepQuote(text, i, quote);
|
|
89
|
-
if (step.closed) {
|
|
90
|
-
result += char;
|
|
91
|
-
quote = null;
|
|
92
|
-
} else {
|
|
93
|
-
result += " ".repeat(step.consumed);
|
|
94
|
-
}
|
|
95
|
-
i += step.consumed - 1;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (char === '"' || char === "'") {
|
|
99
|
-
quote = char;
|
|
100
|
-
}
|
|
101
|
-
result += char;
|
|
102
|
-
}
|
|
103
|
-
return result;
|
|
104
|
-
}
|
|
105
|
-
function parseVarNames(css, selectors) {
|
|
106
|
-
const stripped = stripComments(css);
|
|
107
|
-
const names = /* @__PURE__ */ new Set();
|
|
108
|
-
const allSelectors = [":root", ...selectors ?? []];
|
|
109
|
-
for (const sel of allSelectors) {
|
|
110
|
-
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
111
|
-
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
112
|
-
let match;
|
|
113
|
-
while (match = openRegex.exec(stripped)) {
|
|
114
|
-
const start = match.index + match[0].length;
|
|
115
|
-
const block = extractBlock(stripped, start);
|
|
116
|
-
for (const prop of maskQuotedContent(block).matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
117
|
-
names.add(prop[0]);
|
|
118
|
-
}
|
|
119
|
-
openRegex.lastIndex = start + block.length + 1;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return [...names];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// src/scanner.ts
|
|
126
|
-
import { readFile } from "fs/promises";
|
|
127
|
-
import fg from "fast-glob";
|
|
128
|
-
async function scanVarNames(patterns, exclude, selectors) {
|
|
129
|
-
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
130
|
-
(p) => p.replace(/\\/g, "/")
|
|
131
|
-
);
|
|
132
|
-
const normalizedExclude = exclude ? (Array.isArray(exclude) ? exclude : [exclude]).map((p) => p.replace(/\\/g, "/")) : [];
|
|
133
|
-
const files = await fg(normalized, { absolute: true, ignore: normalizedExclude });
|
|
134
|
-
const all = /* @__PURE__ */ new Set();
|
|
135
|
-
await Promise.all(
|
|
136
|
-
files.map(async (file) => {
|
|
137
|
-
let css;
|
|
138
|
-
try {
|
|
139
|
-
css = await readFile(file, "utf8");
|
|
140
|
-
} catch (err) {
|
|
141
|
-
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
for (const name of parseVarNames(css, selectors)) {
|
|
145
|
-
all.add(name);
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
);
|
|
149
|
-
return [...all].sort();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// src/generator.ts
|
|
153
|
-
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
154
|
-
function convertCase(value, naming) {
|
|
155
|
-
if (!VALID_NAMINGS.includes(naming)) {
|
|
156
|
-
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
157
|
-
}
|
|
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
|
-
}
|
|
161
|
-
function toKey(cssVarName, naming = "camelCase") {
|
|
162
|
-
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
163
|
-
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
164
|
-
}
|
|
165
|
-
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
166
|
-
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
167
|
-
var SINGLE_QUOTE_ESCAPES = {};
|
|
168
|
-
SINGLE_QUOTE_ESCAPES["\\"] = "\\\\";
|
|
169
|
-
SINGLE_QUOTE_ESCAPES["'"] = "\\'";
|
|
170
|
-
SINGLE_QUOTE_ESCAPES["\n"] = "\\n";
|
|
171
|
-
SINGLE_QUOTE_ESCAPES["\r"] = "\\r";
|
|
172
|
-
SINGLE_QUOTE_ESCAPES[LINE_SEPARATOR_CHAR] = "\\u2028";
|
|
173
|
-
SINGLE_QUOTE_ESCAPES[PARAGRAPH_SEPARATOR_CHAR] = "\\u2029";
|
|
174
|
-
function escapeSingleQuoted(value) {
|
|
175
|
-
let result = "";
|
|
176
|
-
for (const char of value) {
|
|
177
|
-
result += SINGLE_QUOTE_ESCAPES[char] ?? char;
|
|
178
|
-
}
|
|
179
|
-
return result;
|
|
180
|
-
}
|
|
181
|
-
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
182
|
-
if (!prefix) return key;
|
|
183
|
-
let normalizedPrefix = convertCase(prefix, naming);
|
|
184
|
-
if (naming === "kebab") {
|
|
185
|
-
normalizedPrefix = escapeSingleQuoted(normalizedPrefix);
|
|
186
|
-
return `${normalizedPrefix}-${key}`;
|
|
187
|
-
}
|
|
188
|
-
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
189
|
-
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
190
|
-
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
191
|
-
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
192
|
-
}
|
|
193
|
-
function formatKey(key, naming = "camelCase") {
|
|
194
|
-
if (naming === "kebab") return `'${key}'`;
|
|
195
|
-
return key;
|
|
196
|
-
}
|
|
197
|
-
function findKeyCollisions(varNames, prefix, naming) {
|
|
198
|
-
const byKey = /* @__PURE__ */ new Map();
|
|
199
|
-
for (const name of varNames) {
|
|
200
|
-
const key = applyPrefix(toKey(name, naming), prefix, naming);
|
|
201
|
-
const existing = byKey.get(key);
|
|
202
|
-
if (existing) existing.push(name);
|
|
203
|
-
else byKey.set(key, [name]);
|
|
204
|
-
}
|
|
205
|
-
for (const [key, names] of byKey) {
|
|
206
|
-
if (names.length < 2) byKey.delete(key);
|
|
207
|
-
}
|
|
208
|
-
return byKey;
|
|
209
|
-
}
|
|
210
|
-
function warnOnCollisions(varNames, prefix, naming) {
|
|
211
|
-
for (const [key, vars] of findKeyCollisions(varNames, prefix, naming)) {
|
|
212
|
-
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
function buildEntries(varNames, prefix, naming) {
|
|
216
|
-
const byKey = /* @__PURE__ */ new Map();
|
|
217
|
-
for (const name of varNames) {
|
|
218
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
219
|
-
byKey.set(key, name);
|
|
220
|
-
}
|
|
221
|
-
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
222
|
-
}
|
|
223
|
-
function generateCode(varNames, prefix, naming) {
|
|
224
|
-
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
225
|
-
return [
|
|
226
|
-
"// generated \u2014 do not edit",
|
|
227
|
-
"export const cssVars = {",
|
|
228
|
-
...entries,
|
|
229
|
-
"} as const;",
|
|
230
|
-
"",
|
|
231
|
-
"export type CssVarName = keyof typeof cssVars;",
|
|
232
|
-
""
|
|
233
|
-
].join("\n");
|
|
234
|
-
}
|
|
235
|
-
function generateJs(varNames, prefix, naming) {
|
|
236
|
-
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
237
|
-
return [
|
|
238
|
-
"// generated \u2014 do not edit",
|
|
239
|
-
"export const cssVars = {",
|
|
240
|
-
...entries,
|
|
241
|
-
"};",
|
|
242
|
-
""
|
|
243
|
-
].join("\n");
|
|
244
|
-
}
|
|
245
|
-
function generateDeclaration(varNames, prefix, naming) {
|
|
246
|
-
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
247
|
-
return [
|
|
248
|
-
"export declare const cssVars: {",
|
|
249
|
-
...entries,
|
|
250
|
-
"};",
|
|
251
|
-
"export type CssVarName = keyof typeof cssVars;",
|
|
252
|
-
""
|
|
253
|
-
].join("\n");
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export {
|
|
257
|
-
__dirname,
|
|
258
|
-
parseVarNames,
|
|
259
|
-
scanVarNames,
|
|
260
|
-
VALID_NAMINGS,
|
|
261
|
-
warnOnCollisions,
|
|
262
|
-
generateCode,
|
|
263
|
-
generateJs,
|
|
264
|
-
generateDeclaration
|
|
265
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type NamingConvention = 'camelCase' | 'snake' | 'kebab';
|
|
2
|
-
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
3
|
-
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
4
|
-
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
5
|
-
|
|
6
|
-
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type NamingConvention = 'camelCase' | 'snake' | 'kebab';
|
|
2
|
-
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
3
|
-
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
4
|
-
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
5
|
-
|
|
6
|
-
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|