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/webpack.cjs
CHANGED
|
@@ -140,10 +140,28 @@ function maskQuotedContent(text) {
|
|
|
140
140
|
}
|
|
141
141
|
return result;
|
|
142
142
|
}
|
|
143
|
-
function
|
|
143
|
+
function extractValue(text, start) {
|
|
144
|
+
let i = start;
|
|
145
|
+
let quote = null;
|
|
146
|
+
for (; i < text.length; i++) {
|
|
147
|
+
const char = text[i];
|
|
148
|
+
if (quote) {
|
|
149
|
+
const step = stepQuote(text, i, quote);
|
|
150
|
+
if (step.closed) quote = null;
|
|
151
|
+
i += step.consumed - 1;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (char === '"' || char === "'") {
|
|
155
|
+
quote = char;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (char === ";" || char === "}") break;
|
|
159
|
+
}
|
|
160
|
+
return text.slice(start, i).trim();
|
|
161
|
+
}
|
|
162
|
+
function scanDeclarations(css, selectors, onProperty) {
|
|
144
163
|
const stripped = stripComments(css);
|
|
145
|
-
const
|
|
146
|
-
const allSelectors = [":root", ...selectors ?? []];
|
|
164
|
+
const allSelectors = [":root", "@theme", ...selectors ?? []];
|
|
147
165
|
for (const sel of allSelectors) {
|
|
148
166
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
149
167
|
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
@@ -151,53 +169,135 @@ function parseVarNames(css, selectors) {
|
|
|
151
169
|
while (match = openRegex.exec(stripped)) {
|
|
152
170
|
const start = match.index + match[0].length;
|
|
153
171
|
const block = extractBlock(stripped, start);
|
|
154
|
-
|
|
155
|
-
|
|
172
|
+
const masked = maskQuotedContent(block);
|
|
173
|
+
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
174
|
+
const name = prop[0];
|
|
175
|
+
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
176
|
+
onProperty(name, extractValue(block, colonIndex + 1));
|
|
156
177
|
}
|
|
157
178
|
openRegex.lastIndex = start + block.length + 1;
|
|
158
179
|
}
|
|
159
180
|
}
|
|
160
|
-
|
|
181
|
+
}
|
|
182
|
+
function parseVarDeclarations(css, selectors) {
|
|
183
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
184
|
+
scanDeclarations(css, selectors, (name, value) => declarations.set(name, value));
|
|
185
|
+
return declarations;
|
|
186
|
+
}
|
|
187
|
+
var PROPERTY_DESCRIPTORS = [
|
|
188
|
+
["syntax", "syntax"],
|
|
189
|
+
["inherits", "inherits"],
|
|
190
|
+
["initial-value", "initialValue"]
|
|
191
|
+
];
|
|
192
|
+
function parsePropertyRules(css) {
|
|
193
|
+
const stripped = stripComments(css);
|
|
194
|
+
const rules = /* @__PURE__ */ new Map();
|
|
195
|
+
const openRegex = /@property\s+(--[\w-]+)\s*\{/g;
|
|
196
|
+
let match;
|
|
197
|
+
while (match = openRegex.exec(stripped)) {
|
|
198
|
+
const name = match[1];
|
|
199
|
+
const start = match.index + match[0].length;
|
|
200
|
+
const block = extractBlock(stripped, start);
|
|
201
|
+
const masked = maskQuotedContent(block);
|
|
202
|
+
const rule = {};
|
|
203
|
+
for (const [descriptor, key] of PROPERTY_DESCRIPTORS) {
|
|
204
|
+
const descRegex = new RegExp(`(?:^|[;{\\s])${descriptor}(?=\\s*:)`);
|
|
205
|
+
const descMatch = descRegex.exec(masked);
|
|
206
|
+
if (!descMatch) continue;
|
|
207
|
+
const nameEnd = descMatch.index + descMatch[0].length;
|
|
208
|
+
const colonIndex = block.indexOf(":", nameEnd);
|
|
209
|
+
rule[key] = extractValue(block, colonIndex + 1);
|
|
210
|
+
}
|
|
211
|
+
if (Object.keys(rule).length > 0) rules.set(name, rule);
|
|
212
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
213
|
+
}
|
|
214
|
+
return rules;
|
|
161
215
|
}
|
|
162
216
|
|
|
163
217
|
// src/scanner.ts
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const
|
|
218
|
+
function normalizePatterns(patterns) {
|
|
219
|
+
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
220
|
+
}
|
|
221
|
+
async function scanCss(patterns, exclude, selectors) {
|
|
222
|
+
const normalized = normalizePatterns(patterns);
|
|
223
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
169
224
|
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
170
|
-
const
|
|
171
|
-
await Promise.all(
|
|
225
|
+
const perFile = await Promise.all(
|
|
172
226
|
files.map(async (file) => {
|
|
173
227
|
let css;
|
|
174
228
|
try {
|
|
175
229
|
css = await (0, import_promises.readFile)(file, "utf8");
|
|
176
230
|
} catch (err) {
|
|
177
231
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
178
|
-
return;
|
|
232
|
+
return { declarations: /* @__PURE__ */ new Map(), properties: /* @__PURE__ */ new Map() };
|
|
179
233
|
}
|
|
180
|
-
|
|
181
|
-
|
|
234
|
+
return { declarations: parseVarDeclarations(css, selectors), properties: parsePropertyRules(css) };
|
|
235
|
+
})
|
|
236
|
+
);
|
|
237
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
238
|
+
const properties = /* @__PURE__ */ new Map();
|
|
239
|
+
for (const result of perFile) {
|
|
240
|
+
for (const [name, value] of result.declarations) declarations.set(name, value);
|
|
241
|
+
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
242
|
+
}
|
|
243
|
+
return { declarations, properties };
|
|
244
|
+
}
|
|
245
|
+
var USAGE_RE = /\bcssVars\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/g;
|
|
246
|
+
function extractUsedKeys(text, into) {
|
|
247
|
+
for (const match of text.matchAll(USAGE_RE)) {
|
|
248
|
+
into.add(match[1] ?? match[3]);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async function scanUsedKeys(patterns, exclude) {
|
|
252
|
+
const normalized = normalizePatterns(patterns);
|
|
253
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
254
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
255
|
+
const perFile = await Promise.all(
|
|
256
|
+
files.map(async (file) => {
|
|
257
|
+
const used = /* @__PURE__ */ new Set();
|
|
258
|
+
let text;
|
|
259
|
+
try {
|
|
260
|
+
text = await (0, import_promises.readFile)(file, "utf8");
|
|
261
|
+
} catch (err) {
|
|
262
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
263
|
+
return used;
|
|
182
264
|
}
|
|
265
|
+
extractUsedKeys(text, used);
|
|
266
|
+
return used;
|
|
183
267
|
})
|
|
184
268
|
);
|
|
185
|
-
|
|
269
|
+
const all = /* @__PURE__ */ new Set();
|
|
270
|
+
for (const used of perFile) {
|
|
271
|
+
for (const key of used) all.add(key);
|
|
272
|
+
}
|
|
273
|
+
return all;
|
|
186
274
|
}
|
|
187
275
|
|
|
188
276
|
// src/generator.ts
|
|
189
|
-
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
277
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab", "constant", "pascal"];
|
|
190
278
|
function convertCase(value, naming) {
|
|
191
279
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
192
280
|
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
193
281
|
}
|
|
194
282
|
if (naming === "kebab") return value;
|
|
195
|
-
|
|
283
|
+
if (naming === "snake") return value.replace(/-+/g, "_");
|
|
284
|
+
if (naming === "constant") return value.replace(/-+/g, "_").toUpperCase();
|
|
285
|
+
if (naming === "pascal") return value.replace(/(^|-+)([a-z0-9])/g, (_, __, c) => c.toUpperCase()).replace(/-/g, "");
|
|
286
|
+
return value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
196
287
|
}
|
|
197
|
-
function
|
|
198
|
-
const key = convertCase(
|
|
288
|
+
function toKeyFragment(fragment, naming = "camelCase") {
|
|
289
|
+
const key = convertCase(fragment, naming);
|
|
199
290
|
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
200
291
|
}
|
|
292
|
+
function toKey(cssVarName, naming = "camelCase") {
|
|
293
|
+
return toKeyFragment(cssVarName.replace(/^--/, ""), naming);
|
|
294
|
+
}
|
|
295
|
+
function splitGroupKey(cssVarName) {
|
|
296
|
+
const stripped = cssVarName.replace(/^--/, "");
|
|
297
|
+
const dashIndex = stripped.indexOf("-");
|
|
298
|
+
if (dashIndex === -1) return null;
|
|
299
|
+
return { group: stripped.slice(0, dashIndex), rest: stripped.slice(dashIndex + 1) };
|
|
300
|
+
}
|
|
201
301
|
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
202
302
|
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
203
303
|
var SINGLE_QUOTE_ESCAPES = {};
|
|
@@ -223,13 +323,16 @@ function applyPrefix(key, prefix, naming = "camelCase") {
|
|
|
223
323
|
}
|
|
224
324
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
225
325
|
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
226
|
-
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
326
|
+
if (naming === "snake" || naming === "constant") return `${normalizedPrefix}_${key}`;
|
|
227
327
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
228
328
|
}
|
|
229
329
|
function formatKey(key, naming = "camelCase") {
|
|
230
330
|
if (naming === "kebab") return `'${key}'`;
|
|
231
331
|
return key;
|
|
232
332
|
}
|
|
333
|
+
function computeKey(cssVarName, prefix, naming) {
|
|
334
|
+
return applyPrefix(toKey(cssVarName, naming), prefix, naming);
|
|
335
|
+
}
|
|
233
336
|
function findKeyCollisions(varNames, prefix, naming) {
|
|
234
337
|
const byKey = /* @__PURE__ */ new Map();
|
|
235
338
|
for (const name of varNames) {
|
|
@@ -248,16 +351,112 @@ function warnOnCollisions(varNames, prefix, naming) {
|
|
|
248
351
|
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
249
352
|
}
|
|
250
353
|
}
|
|
251
|
-
function buildEntries(varNames, prefix, naming) {
|
|
354
|
+
function buildEntries(varNames, prefix, naming, declarations, properties) {
|
|
252
355
|
const byKey = /* @__PURE__ */ new Map();
|
|
253
356
|
for (const name of varNames) {
|
|
254
357
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
255
|
-
byKey.set(key, name);
|
|
358
|
+
byKey.set(key, { name, value: declarations?.get(name), syntax: properties?.get(name)?.syntax });
|
|
359
|
+
}
|
|
360
|
+
return [...byKey].map(([key, entry]) => ({ key, ...entry }));
|
|
361
|
+
}
|
|
362
|
+
function buildGroupedEntries(varNames, naming, declarations, properties) {
|
|
363
|
+
const groupMap = /* @__PURE__ */ new Map();
|
|
364
|
+
const ungroupedMap = /* @__PURE__ */ new Map();
|
|
365
|
+
for (const name of varNames) {
|
|
366
|
+
const value = declarations?.get(name);
|
|
367
|
+
const syntax = properties?.get(name)?.syntax;
|
|
368
|
+
const split = splitGroupKey(name);
|
|
369
|
+
if (!split) {
|
|
370
|
+
ungroupedMap.set(formatKey(toKey(name, naming), naming), { name, value, syntax });
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
const groupKey = formatKey(toKeyFragment(split.group, naming), naming);
|
|
374
|
+
const leafKey = formatKey(toKeyFragment(split.rest, naming), naming);
|
|
375
|
+
let leafMap = groupMap.get(groupKey);
|
|
376
|
+
if (!leafMap) {
|
|
377
|
+
leafMap = /* @__PURE__ */ new Map();
|
|
378
|
+
groupMap.set(groupKey, leafMap);
|
|
379
|
+
}
|
|
380
|
+
leafMap.set(leafKey, { name, value, syntax });
|
|
381
|
+
}
|
|
382
|
+
for (const groupKey of groupMap.keys()) ungroupedMap.delete(groupKey);
|
|
383
|
+
const groups = [...groupMap].map(([key, leafMap]) => ({
|
|
384
|
+
key,
|
|
385
|
+
entries: [...leafMap].map(([key2, entry]) => ({ key: key2, ...entry }))
|
|
386
|
+
}));
|
|
387
|
+
const ungrouped = [...ungroupedMap].map(([key, entry]) => ({ key, ...entry }));
|
|
388
|
+
return { groups, ungrouped };
|
|
389
|
+
}
|
|
390
|
+
function formatDefaultComment(value, indent = " ") {
|
|
391
|
+
return `${indent}/** @default ${value.replace(/\*\//g, "*\\/")} */`;
|
|
392
|
+
}
|
|
393
|
+
function unquoteCssString(value) {
|
|
394
|
+
const quote = value[0];
|
|
395
|
+
if ((quote === '"' || quote === "'") && value[value.length - 1] === quote) {
|
|
396
|
+
return value.slice(1, -1);
|
|
397
|
+
}
|
|
398
|
+
return value;
|
|
399
|
+
}
|
|
400
|
+
function formatSyntaxComment(syntax, indent = " ") {
|
|
401
|
+
return `${indent}/** @syntax ${unquoteCssString(syntax).replace(/\*\//g, "*\\/")} */`;
|
|
402
|
+
}
|
|
403
|
+
var ENUM_SYNTAX_RE = /^[\w-]+(?:\s*\|\s*[\w-]+)+$/;
|
|
404
|
+
function parseEnumSyntax(syntax) {
|
|
405
|
+
const unquoted = unquoteCssString(syntax).trim();
|
|
406
|
+
if (!ENUM_SYNTAX_RE.test(unquoted)) return null;
|
|
407
|
+
return unquoted.split("|").map((v) => v.trim());
|
|
408
|
+
}
|
|
409
|
+
function syntaxTypeName(cssVarName) {
|
|
410
|
+
const pascal = convertCase(cssVarName.replace(/^--/, ""), "pascal");
|
|
411
|
+
return (/^\d/.test(pascal) ? `_${pascal}` : pascal) + "Syntax";
|
|
412
|
+
}
|
|
413
|
+
function buildSyntaxTypes(varNames, properties) {
|
|
414
|
+
if (!properties) return [];
|
|
415
|
+
const byName = /* @__PURE__ */ new Map();
|
|
416
|
+
for (const name of varNames) {
|
|
417
|
+
const values = parseEnumSyntax(properties.get(name)?.syntax ?? "");
|
|
418
|
+
if (values) byName.set(syntaxTypeName(name), values);
|
|
256
419
|
}
|
|
257
|
-
return [...
|
|
420
|
+
return [...byName].map(([name, values]) => ({ name, values }));
|
|
258
421
|
}
|
|
259
|
-
function
|
|
260
|
-
|
|
422
|
+
function renderSyntaxTypeLines(types) {
|
|
423
|
+
return types.map(({ name, values }) => `export type ${name} = ${values.map((v) => `'${escapeSingleQuoted(v)}'`).join(" | ")};`);
|
|
424
|
+
}
|
|
425
|
+
function buildTailBlocks(varNames, group, properties) {
|
|
426
|
+
const blocks = [];
|
|
427
|
+
if (!group) blocks.push(["export type CssVarName = keyof typeof cssVars;"]);
|
|
428
|
+
const syntaxLines = renderSyntaxTypeLines(buildSyntaxTypes(varNames, properties));
|
|
429
|
+
if (syntaxLines.length > 0) blocks.push(syntaxLines);
|
|
430
|
+
return blocks;
|
|
431
|
+
}
|
|
432
|
+
function renderEntryLines(entries, indent, lineFor) {
|
|
433
|
+
return entries.flatMap(({ key, name, value, syntax }) => [
|
|
434
|
+
...value ? [formatDefaultComment(value, indent)] : [],
|
|
435
|
+
...syntax ? [formatSyntaxComment(syntax, indent)] : [],
|
|
436
|
+
`${indent}${lineFor(key, name)}`
|
|
437
|
+
]);
|
|
438
|
+
}
|
|
439
|
+
function renderGroupedBlock(varNames, naming, declarations, properties, indent, closeSuffix, lineFor) {
|
|
440
|
+
const { groups, ungrouped } = buildGroupedEntries(varNames, naming, declarations, properties);
|
|
441
|
+
const lines = [];
|
|
442
|
+
for (const group of groups) {
|
|
443
|
+
lines.push(`${indent}${group.key}: {`);
|
|
444
|
+
lines.push(...renderEntryLines(group.entries, indent + " ", lineFor));
|
|
445
|
+
lines.push(`${indent}}${closeSuffix}`);
|
|
446
|
+
}
|
|
447
|
+
lines.push(...renderEntryLines(ungrouped, indent, lineFor));
|
|
448
|
+
return lines;
|
|
449
|
+
}
|
|
450
|
+
function assertGroupPrefixCompatible(prefix, group) {
|
|
451
|
+
if (group && prefix) {
|
|
452
|
+
throw new Error('css-typed-vars: the "group" option cannot be combined with "prefix".');
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
var codeLineFor = (key, name) => `${key}: 'var(${name})',`;
|
|
456
|
+
var declarationLineFor = (key, name) => `${key}: 'var(${name})';`;
|
|
457
|
+
function generateJs(varNames, prefix, naming, declarations, group, properties) {
|
|
458
|
+
assertGroupPrefixCompatible(prefix, group);
|
|
459
|
+
const entries = group ? renderGroupedBlock(varNames, naming, declarations, properties, " ", ",", codeLineFor) : renderEntryLines(buildEntries(varNames, prefix, naming, declarations, properties), " ", codeLineFor);
|
|
261
460
|
return [
|
|
262
461
|
"// generated \u2014 do not edit",
|
|
263
462
|
"export const cssVars = {",
|
|
@@ -266,13 +465,16 @@ function generateJs(varNames, prefix, naming) {
|
|
|
266
465
|
""
|
|
267
466
|
].join("\n");
|
|
268
467
|
}
|
|
269
|
-
function generateDeclaration(varNames, prefix, naming) {
|
|
270
|
-
|
|
468
|
+
function generateDeclaration(varNames, prefix, naming, declarations, group, properties) {
|
|
469
|
+
assertGroupPrefixCompatible(prefix, group);
|
|
470
|
+
const entries = group ? renderGroupedBlock(varNames, naming, declarations, properties, " ", ";", declarationLineFor) : renderEntryLines(buildEntries(varNames, prefix, naming, declarations, properties), " ", declarationLineFor);
|
|
471
|
+
const blocks = buildTailBlocks(varNames, group, properties);
|
|
472
|
+
const tailBody = blocks.flatMap((block, i) => i === 0 ? block : ["", ...block]);
|
|
271
473
|
return [
|
|
272
474
|
"export declare const cssVars: {",
|
|
273
475
|
...entries,
|
|
274
476
|
"};",
|
|
275
|
-
|
|
477
|
+
...tailBody,
|
|
276
478
|
""
|
|
277
479
|
].join("\n");
|
|
278
480
|
}
|
|
@@ -285,7 +487,27 @@ function getDtsPath(options) {
|
|
|
285
487
|
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
286
488
|
}
|
|
287
489
|
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
288
|
-
|
|
490
|
+
if (options.prune) {
|
|
491
|
+
if (options.group) throw new Error('css-typed-vars: "prune" cannot be combined with "group".');
|
|
492
|
+
if (!options.usage) throw new Error('css-typed-vars: "prune" requires "usage" (glob(s) of source files to scan).');
|
|
493
|
+
}
|
|
494
|
+
let cachedScan = null;
|
|
495
|
+
const scan = () => scanCss(options.input, options.exclude, options.selectors);
|
|
496
|
+
const applyPrune = async (names) => {
|
|
497
|
+
if (!options.prune) return names;
|
|
498
|
+
const usedKeys = await scanUsedKeys(options.usage, options.usageExclude);
|
|
499
|
+
const isUsed = (name) => usedKeys.has(computeKey(name, options.prefix, options.naming));
|
|
500
|
+
const unused = names.filter((name) => !isUsed(name));
|
|
501
|
+
if (unused.length > 0) {
|
|
502
|
+
console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
|
|
503
|
+
}
|
|
504
|
+
return names.filter(isUsed);
|
|
505
|
+
};
|
|
506
|
+
const resolveNames = async (declarations) => {
|
|
507
|
+
const names = [...declarations.keys()].sort();
|
|
508
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
509
|
+
return applyPrune(names);
|
|
510
|
+
};
|
|
289
511
|
return {
|
|
290
512
|
name: "css-typed-vars",
|
|
291
513
|
vite: {
|
|
@@ -296,14 +518,15 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
296
518
|
let writeQueue = Promise.resolve();
|
|
297
519
|
const rescan = async () => {
|
|
298
520
|
const myGeneration = ++generation;
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
const
|
|
521
|
+
const scanPromise = scan();
|
|
522
|
+
cachedScan = scanPromise;
|
|
523
|
+
const { declarations, properties } = await scanPromise;
|
|
524
|
+
if (myGeneration !== generation) return;
|
|
525
|
+
const names = await resolveNames(declarations);
|
|
302
526
|
if (myGeneration !== generation) return;
|
|
303
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
304
527
|
const dtsPath = getDtsPath(options);
|
|
305
528
|
if (dtsPath) {
|
|
306
|
-
const content = generateDeclaration(names, options.prefix, options.naming);
|
|
529
|
+
const content = generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties);
|
|
307
530
|
writeQueue = writeQueue.then(async () => {
|
|
308
531
|
if (myGeneration !== generation) return;
|
|
309
532
|
await (0, import_promises2.writeFile)(dtsPath, content, "utf8");
|
|
@@ -331,20 +554,21 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
331
554
|
}
|
|
332
555
|
},
|
|
333
556
|
async buildStart() {
|
|
334
|
-
|
|
335
|
-
const
|
|
336
|
-
|
|
557
|
+
cachedScan = scan();
|
|
558
|
+
const { declarations, properties } = await cachedScan;
|
|
559
|
+
const names = await resolveNames(declarations);
|
|
337
560
|
const dtsPath = getDtsPath(options);
|
|
338
561
|
if (!dtsPath) return;
|
|
339
|
-
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
562
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties), "utf8");
|
|
340
563
|
},
|
|
341
564
|
resolveId(id) {
|
|
342
565
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
343
566
|
},
|
|
344
567
|
async load(id) {
|
|
345
568
|
if (id === RESOLVED_ID) {
|
|
346
|
-
const
|
|
347
|
-
|
|
569
|
+
const { declarations, properties } = await (cachedScan ?? scan());
|
|
570
|
+
const names = await applyPrune([...declarations.keys()].sort());
|
|
571
|
+
return generateJs(names, options.prefix, options.naming, declarations, options.group, properties);
|
|
348
572
|
}
|
|
349
573
|
}
|
|
350
574
|
};
|
package/dist/webpack.d.cts
CHANGED
package/dist/webpack.d.ts
CHANGED
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "css-typed-vars",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Generate TypeScript typed constants from CSS custom properties",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -62,7 +62,10 @@
|
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsup && node scripts/postbuild.mjs",
|
|
64
64
|
"test": "vitest run --passWithNoTests",
|
|
65
|
-
"typecheck": "tsc --noEmit"
|
|
65
|
+
"typecheck": "tsc --noEmit",
|
|
66
|
+
"docs:dev": "vitepress dev docs",
|
|
67
|
+
"docs:build": "vitepress build docs",
|
|
68
|
+
"docs:preview": "vitepress preview docs"
|
|
66
69
|
},
|
|
67
70
|
"dependencies": {
|
|
68
71
|
"chokidar": "^3.6.0",
|
|
@@ -73,6 +76,7 @@
|
|
|
73
76
|
"@types/node": "^25.5.2",
|
|
74
77
|
"tsup": "^8.0.0",
|
|
75
78
|
"typescript": "^5.4.0",
|
|
79
|
+
"vitepress": "^1.6.4",
|
|
76
80
|
"vitest": "^3.0.0"
|
|
77
81
|
}
|
|
78
82
|
}
|
package/dist/chunk-EHRNYU34.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
generateCode,
|
|
3
|
-
generateDeclaration,
|
|
4
|
-
generateJs,
|
|
5
|
-
scanVarNames,
|
|
6
|
-
warnOnCollisions
|
|
7
|
-
} from "./chunk-WYLYZXQJ.js";
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
import { writeFile, mkdir } from "fs/promises";
|
|
11
|
-
import { dirname, resolve } from "path";
|
|
12
|
-
async function generate(options) {
|
|
13
|
-
const names = await scanVarNames(options.input, options.exclude, options.selectors);
|
|
14
|
-
if (names.length === 0) {
|
|
15
|
-
console.warn("css-typed-vars: no CSS custom properties found.");
|
|
16
|
-
}
|
|
17
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
18
|
-
const outPath = resolve(options.output);
|
|
19
|
-
await mkdir(dirname(outPath), { recursive: true });
|
|
20
|
-
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
|
21
|
-
if (jsExtMatch) {
|
|
22
|
-
await writeFile(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
23
|
-
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
24
|
-
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
25
|
-
} else {
|
|
26
|
-
await writeFile(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export {
|
|
31
|
-
generate
|
|
32
|
-
};
|