css-typed-vars 0.7.0 → 0.9.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 +119 -16
- package/dist/{chunk-HBUSLYYB.js → chunk-66NEMXYL.js} +14 -8
- package/dist/{chunk-6YWE4A6S.js → chunk-DHBPPGOE.js} +186 -31
- package/dist/{chunk-PLO2JWR7.js → chunk-TB5PSGEF.js} +24 -8
- package/dist/cli.cjs +464 -126
- package/dist/cli.js +280 -89
- package/dist/esbuild.cjs +145 -39
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/{generator-C1iVNsry.d.ts → generator-DJ9uOYBP.d.cts} +21 -1
- package/dist/{generator-C1iVNsry.d.cts → generator-DJ9uOYBP.d.ts} +21 -1
- package/dist/index.cjs +200 -39
- package/dist/index.d.cts +24 -4
- package/dist/index.d.ts +24 -4
- package/dist/index.js +12 -2
- package/dist/rollup.cjs +145 -39
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +145 -39
- package/dist/unplugin.d.cts +1 -1
- package/dist/unplugin.d.ts +1 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +145 -39
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +145 -39
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +3 -1
|
@@ -5,6 +5,17 @@ var getFilename = () => fileURLToPath(import.meta.url);
|
|
|
5
5
|
var getDirname = () => path.dirname(getFilename());
|
|
6
6
|
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
7
|
|
|
8
|
+
// src/write.ts
|
|
9
|
+
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
10
|
+
import { dirname } from "path";
|
|
11
|
+
async function writeOutput(path2, content) {
|
|
12
|
+
const current = await readFile(path2, "utf8").catch(() => null);
|
|
13
|
+
if (current === content) return false;
|
|
14
|
+
await mkdir(dirname(path2), { recursive: true });
|
|
15
|
+
await writeFile(path2, content, "utf8");
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
8
19
|
// src/parser.ts
|
|
9
20
|
function stepQuote(text, i, quote) {
|
|
10
21
|
const char = text[i];
|
|
@@ -13,6 +24,11 @@ function stepQuote(text, i, quote) {
|
|
|
13
24
|
return { consumed: 1, closed: false };
|
|
14
25
|
}
|
|
15
26
|
function stripComments(text) {
|
|
27
|
+
return stripCommentsWithMap(text).text;
|
|
28
|
+
}
|
|
29
|
+
function stripCommentsWithMap(text) {
|
|
30
|
+
const removed = [];
|
|
31
|
+
const comments = [];
|
|
16
32
|
let result = "";
|
|
17
33
|
let quote = null;
|
|
18
34
|
let urlDepth = 0;
|
|
@@ -47,17 +63,20 @@ function stripComments(text) {
|
|
|
47
63
|
result += char;
|
|
48
64
|
continue;
|
|
49
65
|
}
|
|
66
|
+
comments.push([i, end + 2]);
|
|
67
|
+
removed.push({ at: result.length, length: end + 2 - i });
|
|
50
68
|
i = end + 1;
|
|
51
69
|
continue;
|
|
52
70
|
}
|
|
53
71
|
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
54
72
|
const nl = text.indexOf("\n", i);
|
|
73
|
+
removed.push({ at: result.length, length: (nl === -1 ? text.length : nl) - i });
|
|
55
74
|
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
56
75
|
continue;
|
|
57
76
|
}
|
|
58
77
|
result += char;
|
|
59
78
|
}
|
|
60
|
-
return result;
|
|
79
|
+
return { text: result, removed, comments };
|
|
61
80
|
}
|
|
62
81
|
function extractBlock(text, start) {
|
|
63
82
|
let depth = 1;
|
|
@@ -121,12 +140,12 @@ function extractValue(text, start) {
|
|
|
121
140
|
}
|
|
122
141
|
return text.slice(start, i).trim();
|
|
123
142
|
}
|
|
124
|
-
function forEachProperty(block, onProperty) {
|
|
143
|
+
function forEachProperty(block, onProperty, offset = 0) {
|
|
125
144
|
const masked = maskQuotedContent(block);
|
|
126
145
|
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
127
146
|
const name = prop[0];
|
|
128
147
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
129
|
-
onProperty(name, extractValue(block, colonIndex + 1));
|
|
148
|
+
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
130
149
|
}
|
|
131
150
|
}
|
|
132
151
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -147,7 +166,7 @@ var BASE_SELECTORS = [":root", "@theme"];
|
|
|
147
166
|
function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
|
|
148
167
|
forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
|
|
149
168
|
if (skip.some(([s, e]) => bodyStart >= s && bodyStart < e)) return;
|
|
150
|
-
forEachProperty(block, onProperty);
|
|
169
|
+
forEachProperty(block, onProperty, bodyStart);
|
|
151
170
|
});
|
|
152
171
|
}
|
|
153
172
|
function parseVarNames(css, selectors) {
|
|
@@ -177,30 +196,73 @@ function forEachMediaBlock(stripped, media, onBlock) {
|
|
|
177
196
|
}
|
|
178
197
|
}
|
|
179
198
|
}
|
|
199
|
+
function parseDocComment(body) {
|
|
200
|
+
const lines = body.split("\n").map((line) => line.replace(/^\s*\*?\s?/, "").trimEnd());
|
|
201
|
+
const doc = {};
|
|
202
|
+
const description = [];
|
|
203
|
+
let inTags = false;
|
|
204
|
+
for (const line of lines) {
|
|
205
|
+
const tag = /^@(\w+)\s*(.*)$/.exec(line.trim());
|
|
206
|
+
if (tag) {
|
|
207
|
+
inTags = true;
|
|
208
|
+
if (tag[1] === "deprecated") doc.deprecated = tag[2].trim() || true;
|
|
209
|
+
} else if (!inTags) {
|
|
210
|
+
description.push(line);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const text = description.join("\n").trim();
|
|
214
|
+
if (text) doc.description = text;
|
|
215
|
+
return doc.description !== void 0 || doc.deprecated !== void 0 ? doc : void 0;
|
|
216
|
+
}
|
|
217
|
+
function makeDocReader(css, stripped) {
|
|
218
|
+
const commentStartByEnd = new Map(stripped.comments.map(([start, end]) => [end, start]));
|
|
219
|
+
return (strippedPos) => {
|
|
220
|
+
let end = toRawPosition(stripped, strippedPos);
|
|
221
|
+
while (end > 0 && /\s/.test(css[end - 1])) end--;
|
|
222
|
+
const start = commentStartByEnd.get(end);
|
|
223
|
+
if (start === void 0 || !css.startsWith("/**", start) || end - start <= 4) return void 0;
|
|
224
|
+
return parseDocComment(css.slice(start + 3, end - 2));
|
|
225
|
+
};
|
|
226
|
+
}
|
|
180
227
|
function parseThemedDeclarations(css, selectors, themes) {
|
|
181
|
-
const
|
|
228
|
+
const strippedCss = stripCommentsWithMap(css);
|
|
229
|
+
const stripped = strippedCss.text;
|
|
230
|
+
const docAt = makeDocReader(css, strippedCss);
|
|
231
|
+
const themeDocs = /* @__PURE__ */ new Map();
|
|
182
232
|
const themeValues = /* @__PURE__ */ new Map();
|
|
183
233
|
const skip = [];
|
|
184
234
|
for (const [themeName, themeSelector] of Object.entries(themes)) {
|
|
185
235
|
const values = /* @__PURE__ */ new Map();
|
|
186
236
|
themeValues.set(themeName, values);
|
|
187
|
-
const collect = (block) => forEachProperty(block, (name, value) =>
|
|
237
|
+
const collect = (block, offset) => forEachProperty(block, (name, value, position) => {
|
|
238
|
+
values.set(name, value);
|
|
239
|
+
const doc = docAt(position);
|
|
240
|
+
if (doc) themeDocs.set(name, doc);
|
|
241
|
+
}, offset);
|
|
188
242
|
const selector = themeSelector.trim();
|
|
189
243
|
if (/^@media\b/i.test(selector)) {
|
|
190
244
|
forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
|
|
191
245
|
skip.push(range);
|
|
192
|
-
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, collect);
|
|
246
|
+
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
|
|
193
247
|
});
|
|
194
248
|
} else {
|
|
195
249
|
forEachSelectorBlock(stripped, [selector], (block, range) => {
|
|
196
250
|
skip.push(range);
|
|
197
|
-
collect(block);
|
|
251
|
+
collect(block, range[0]);
|
|
198
252
|
});
|
|
199
253
|
}
|
|
200
254
|
}
|
|
201
255
|
const declarations = /* @__PURE__ */ new Map();
|
|
202
|
-
|
|
203
|
-
|
|
256
|
+
const docs = /* @__PURE__ */ new Map();
|
|
257
|
+
scanDeclarations(css, selectors, (name, value, position) => {
|
|
258
|
+
declarations.set(name, value);
|
|
259
|
+
const doc = docAt(position);
|
|
260
|
+
if (doc) docs.set(name, doc);
|
|
261
|
+
}, skip, stripped);
|
|
262
|
+
for (const [name, doc] of themeDocs) {
|
|
263
|
+
if (!docs.has(name)) docs.set(name, doc);
|
|
264
|
+
}
|
|
265
|
+
return { declarations, themes: themeValues, docs };
|
|
204
266
|
}
|
|
205
267
|
var PROPERTY_DESCRIPTORS = [
|
|
206
268
|
["syntax", "syntax"],
|
|
@@ -231,13 +293,55 @@ function parsePropertyRules(css) {
|
|
|
231
293
|
}
|
|
232
294
|
return rules;
|
|
233
295
|
}
|
|
296
|
+
function toRawPosition(stripped, strippedPos) {
|
|
297
|
+
let rawPos = strippedPos;
|
|
298
|
+
for (const { at, length } of stripped.removed) {
|
|
299
|
+
if (at > strippedPos) break;
|
|
300
|
+
rawPos += length;
|
|
301
|
+
}
|
|
302
|
+
return rawPos;
|
|
303
|
+
}
|
|
304
|
+
function parseVarReferences(css) {
|
|
305
|
+
const stripped = stripCommentsWithMap(css);
|
|
306
|
+
const masked = maskQuotedContent(stripped.text);
|
|
307
|
+
const lineStarts = [0];
|
|
308
|
+
for (let i = 0; i < css.length; i++) if (css[i] === "\n") lineStarts.push(i + 1);
|
|
309
|
+
const refs = [];
|
|
310
|
+
for (const match of masked.matchAll(/\bvar\(\s*(--[\w-]+)\s*(,)?/gi)) {
|
|
311
|
+
const rawPos = toRawPosition(stripped, match.index);
|
|
312
|
+
let line = lineStarts.length - 1;
|
|
313
|
+
while (lineStarts[line] > rawPos) line--;
|
|
314
|
+
refs.push({ name: match[1], hasFallback: match[2] === ",", line: line + 1, column: rawPos - lineStarts[line] + 1 });
|
|
315
|
+
}
|
|
316
|
+
return refs;
|
|
317
|
+
}
|
|
318
|
+
function parseDeclaredNames(css) {
|
|
319
|
+
const masked = maskQuotedContent(stripComments(css));
|
|
320
|
+
const names = /* @__PURE__ */ new Set();
|
|
321
|
+
for (const match of masked.matchAll(/(?<![\w-])(--[\w-]+)\s*:/g)) names.add(match[1]);
|
|
322
|
+
for (const match of masked.matchAll(/@property\s+(--[\w-]+)/g)) names.add(match[1]);
|
|
323
|
+
return names;
|
|
324
|
+
}
|
|
234
325
|
|
|
235
326
|
// src/scanner.ts
|
|
236
|
-
import { readFile } from "fs/promises";
|
|
327
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
328
|
+
import { relative, resolve } from "path";
|
|
237
329
|
import fg from "fast-glob";
|
|
330
|
+
import picomatch from "picomatch";
|
|
238
331
|
function normalizePatterns(patterns) {
|
|
239
332
|
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
240
333
|
}
|
|
334
|
+
function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
335
|
+
const compile = (globs) => picomatch(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
|
|
336
|
+
const include = compile(patterns);
|
|
337
|
+
const ignore = exclude ? compile(exclude) : () => false;
|
|
338
|
+
return (file) => {
|
|
339
|
+
const absolute = resolve(cwd, file).replace(/\\/g, "/");
|
|
340
|
+
const fromCwd = relative(cwd, absolute).replace(/\\/g, "/");
|
|
341
|
+
const matches = (test) => test(fromCwd) || test(absolute);
|
|
342
|
+
return matches(include) && !matches(ignore);
|
|
343
|
+
};
|
|
344
|
+
}
|
|
241
345
|
function collectVarNames(scanned) {
|
|
242
346
|
const names = new Set(scanned.declarations.keys());
|
|
243
347
|
for (const values of scanned.themes.values()) {
|
|
@@ -253,7 +357,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
253
357
|
files.map(async (file) => {
|
|
254
358
|
let css;
|
|
255
359
|
try {
|
|
256
|
-
css = await
|
|
360
|
+
css = await readFile2(file, "utf8");
|
|
257
361
|
} catch (err) {
|
|
258
362
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
259
363
|
return null;
|
|
@@ -264,15 +368,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
264
368
|
const declarations = /* @__PURE__ */ new Map();
|
|
265
369
|
const properties = /* @__PURE__ */ new Map();
|
|
266
370
|
const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
371
|
+
const docs = /* @__PURE__ */ new Map();
|
|
267
372
|
for (const result of perFile) {
|
|
268
373
|
if (!result) continue;
|
|
269
374
|
for (const [name, value] of result.declarations) declarations.set(name, value);
|
|
270
375
|
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
376
|
+
for (const [name, doc] of result.docs) docs.set(name, doc);
|
|
271
377
|
for (const [theme, values] of result.themes) {
|
|
272
378
|
for (const [name, value] of values) themeValues.get(theme).set(name, value);
|
|
273
379
|
}
|
|
274
380
|
}
|
|
275
|
-
return { declarations, properties, themes: themeValues };
|
|
381
|
+
return { declarations, properties, themes: themeValues, docs };
|
|
276
382
|
}
|
|
277
383
|
async function scanVarDeclarations(patterns, exclude, selectors) {
|
|
278
384
|
return (await scanCss(patterns, exclude, selectors)).declarations;
|
|
@@ -306,7 +412,7 @@ async function scanUsage(patterns, exclude) {
|
|
|
306
412
|
const texts = await Promise.all(
|
|
307
413
|
files.map(async (file) => {
|
|
308
414
|
try {
|
|
309
|
-
return await
|
|
415
|
+
return await readFile2(file, "utf8");
|
|
310
416
|
} catch (err) {
|
|
311
417
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
312
418
|
return "";
|
|
@@ -316,6 +422,29 @@ async function scanUsage(patterns, exclude) {
|
|
|
316
422
|
for (const text of texts) extractUsage(text, usage);
|
|
317
423
|
return usage;
|
|
318
424
|
}
|
|
425
|
+
async function scanReferences(patterns, exclude) {
|
|
426
|
+
const normalized = normalizePatterns(patterns);
|
|
427
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
428
|
+
const files = (await fg(normalized, { absolute: true, ignore: normalizedExclude })).sort();
|
|
429
|
+
const perFile = await Promise.all(
|
|
430
|
+
files.map(async (file) => {
|
|
431
|
+
try {
|
|
432
|
+
return { file, css: await readFile2(file, "utf8") };
|
|
433
|
+
} catch (err) {
|
|
434
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
})
|
|
438
|
+
);
|
|
439
|
+
const references = [];
|
|
440
|
+
const declared = /* @__PURE__ */ new Set();
|
|
441
|
+
for (const result of perFile) {
|
|
442
|
+
if (!result) continue;
|
|
443
|
+
for (const ref of parseVarReferences(result.css)) references.push({ ...ref, file: result.file });
|
|
444
|
+
for (const name of parseDeclaredNames(result.css)) declared.add(name);
|
|
445
|
+
}
|
|
446
|
+
return { references, declared };
|
|
447
|
+
}
|
|
319
448
|
async function scanUsedKeys(patterns, exclude) {
|
|
320
449
|
return (await scanUsage(patterns, exclude)).keys;
|
|
321
450
|
}
|
|
@@ -414,7 +543,8 @@ function entryMeta(name, options) {
|
|
|
414
543
|
name,
|
|
415
544
|
value: options.declarations?.get(name),
|
|
416
545
|
syntax: options.properties?.get(name)?.syntax,
|
|
417
|
-
...themes.length > 0 ? { themes } : {}
|
|
546
|
+
...themes.length > 0 ? { themes } : {},
|
|
547
|
+
...options.docs?.has(name) ? { doc: options.docs.get(name) } : {}
|
|
418
548
|
};
|
|
419
549
|
}
|
|
420
550
|
function buildEntries(varNames, options) {
|
|
@@ -454,8 +584,12 @@ function buildGroupedEntries(varNames, options) {
|
|
|
454
584
|
const ungrouped = [...ungroupedMap].map(([key, entry]) => ({ key, ...entry }));
|
|
455
585
|
return { groups, ungrouped };
|
|
456
586
|
}
|
|
457
|
-
|
|
458
|
-
|
|
587
|
+
var escapeComment = (text) => text.replace(/\*\//g, "*\\/");
|
|
588
|
+
function renderJsDoc(tags, indent) {
|
|
589
|
+
if (tags.length === 0) return [];
|
|
590
|
+
if (tags.length === 1 && !tags[0].includes("\n")) return [`${indent}/** ${escapeComment(tags[0])} */`];
|
|
591
|
+
const body = tags.flatMap((tag) => escapeComment(tag).split("\n")).map((line) => `${indent} * ${line}`.trimEnd());
|
|
592
|
+
return [`${indent}/**`, ...body, `${indent} */`];
|
|
459
593
|
}
|
|
460
594
|
function unquoteCssString(value) {
|
|
461
595
|
const quote = value[0];
|
|
@@ -464,11 +598,15 @@ function unquoteCssString(value) {
|
|
|
464
598
|
}
|
|
465
599
|
return value;
|
|
466
600
|
}
|
|
467
|
-
function
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
601
|
+
function entryTags({ value, syntax, themes, doc }) {
|
|
602
|
+
const deprecated = doc?.deprecated;
|
|
603
|
+
return [
|
|
604
|
+
...doc?.description ? [doc.description] : [],
|
|
605
|
+
...value ? [`@default ${value}`] : [],
|
|
606
|
+
...(themes ?? []).map(([theme, themeValue]) => `@theme ${theme} ${themeValue}`),
|
|
607
|
+
...syntax ? [`@syntax ${unquoteCssString(syntax)}`] : [],
|
|
608
|
+
...deprecated ? [deprecated === true ? "@deprecated" : `@deprecated ${deprecated}`] : []
|
|
609
|
+
];
|
|
472
610
|
}
|
|
473
611
|
var ENUM_SYNTAX_RE = /^[\w-]+(?:\s*\|\s*[\w-]+)+$/;
|
|
474
612
|
function parseEnumSyntax(syntax) {
|
|
@@ -500,11 +638,9 @@ function buildTailBlocks(varNames, group, properties) {
|
|
|
500
638
|
return blocks;
|
|
501
639
|
}
|
|
502
640
|
function renderEntryLines(entries, indent, lineFor) {
|
|
503
|
-
return entries.flatMap((
|
|
504
|
-
...
|
|
505
|
-
|
|
506
|
-
...syntax ? [formatSyntaxComment(syntax, indent)] : [],
|
|
507
|
-
`${indent}${lineFor(key, name)}`
|
|
641
|
+
return entries.flatMap((entry) => [
|
|
642
|
+
...renderJsDoc(entryTags(entry), indent),
|
|
643
|
+
`${indent}${lineFor(entry.key, entry.name)}`
|
|
508
644
|
]);
|
|
509
645
|
}
|
|
510
646
|
function renderGroupedBlock(varNames, options, indent, closeSuffix, lineFor) {
|
|
@@ -551,6 +687,12 @@ var HELPERS_TS = [
|
|
|
551
687
|
"",
|
|
552
688
|
"export function setCssVar(ref: CssVarRef, value: string, el: HTMLElement = document.documentElement): void {",
|
|
553
689
|
" el.style.setProperty(ref.slice(4, -1), value);",
|
|
690
|
+
"}",
|
|
691
|
+
"",
|
|
692
|
+
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
693
|
+
"",
|
|
694
|
+
"export function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F> {",
|
|
695
|
+
" return `${ref.slice(0, -1)}, ${fallback})` as CssVarWithFallback<R, F>;",
|
|
554
696
|
"}"
|
|
555
697
|
];
|
|
556
698
|
var HELPERS_JS = [
|
|
@@ -560,11 +702,17 @@ var HELPERS_JS = [
|
|
|
560
702
|
"",
|
|
561
703
|
"export function setCssVar(ref, value, el = document.documentElement) {",
|
|
562
704
|
" el.style.setProperty(ref.slice(4, -1), value);",
|
|
705
|
+
"}",
|
|
706
|
+
"",
|
|
707
|
+
"export function withFallback(ref, fallback) {",
|
|
708
|
+
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
563
709
|
"}"
|
|
564
710
|
];
|
|
565
711
|
var HELPERS_DTS = [
|
|
566
712
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
567
|
-
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;"
|
|
713
|
+
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
714
|
+
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
715
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
568
716
|
];
|
|
569
717
|
function generateCode(varNames, ...args) {
|
|
570
718
|
const options = normalizeOptions(args);
|
|
@@ -624,17 +772,19 @@ function generateTokens(varNames, options = {}) {
|
|
|
624
772
|
return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
|
|
625
773
|
});
|
|
626
774
|
const paths = new Map(tokens.map(({ meta, path: path2 }) => [meta.name, path2]));
|
|
627
|
-
const
|
|
775
|
+
const resolve2 = (value) => {
|
|
628
776
|
const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
|
|
629
777
|
return target ? `{${target.join(".")}}` : value;
|
|
630
778
|
};
|
|
631
779
|
const root = {};
|
|
632
780
|
for (const { meta, value, path: path2 } of tokens) {
|
|
633
|
-
const token = { $value:
|
|
781
|
+
const token = { $value: resolve2(value) };
|
|
634
782
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
635
783
|
if (type) token.$type = type;
|
|
784
|
+
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
785
|
+
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
636
786
|
if (meta.themes) {
|
|
637
|
-
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme,
|
|
787
|
+
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
|
|
638
788
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
639
789
|
}
|
|
640
790
|
const [key, leaf] = path2;
|
|
@@ -670,15 +820,20 @@ function partitionUnused(names, usage, options) {
|
|
|
670
820
|
|
|
671
821
|
export {
|
|
672
822
|
__dirname,
|
|
823
|
+
writeOutput,
|
|
673
824
|
parseVarNames,
|
|
674
825
|
parseVarDeclarations,
|
|
675
826
|
parseThemedDeclarations,
|
|
676
827
|
parsePropertyRules,
|
|
828
|
+
parseVarReferences,
|
|
829
|
+
parseDeclaredNames,
|
|
830
|
+
createFileMatcher,
|
|
677
831
|
collectVarNames,
|
|
678
832
|
scanCss,
|
|
679
833
|
scanVarDeclarations,
|
|
680
834
|
scanVarNames,
|
|
681
835
|
scanUsage,
|
|
836
|
+
scanReferences,
|
|
682
837
|
scanUsedKeys,
|
|
683
838
|
VALID_NAMINGS,
|
|
684
839
|
warnOnCollisions,
|
|
@@ -7,13 +7,18 @@ import {
|
|
|
7
7
|
generateTokens,
|
|
8
8
|
partitionUnused,
|
|
9
9
|
scanCss,
|
|
10
|
+
scanReferences,
|
|
10
11
|
scanUsage,
|
|
11
|
-
warnOnCollisions
|
|
12
|
-
|
|
12
|
+
warnOnCollisions,
|
|
13
|
+
writeOutput
|
|
14
|
+
} from "./chunk-DHBPPGOE.js";
|
|
13
15
|
|
|
14
16
|
// src/index.ts
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
+
import { readFile } from "fs/promises";
|
|
18
|
+
import { resolve } from "path";
|
|
19
|
+
function defineConfig(config) {
|
|
20
|
+
return config;
|
|
21
|
+
}
|
|
17
22
|
async function findUnusedVars(options) {
|
|
18
23
|
if (!options.usage) throw new Error('css-typed-vars: findUnusedVars requires "usage" (glob(s) of source files to scan).');
|
|
19
24
|
const [scanned, usage] = await Promise.all([
|
|
@@ -23,6 +28,14 @@ async function findUnusedVars(options) {
|
|
|
23
28
|
const names = collectVarNames(scanned);
|
|
24
29
|
return partitionUnused(names, usage, options).unused;
|
|
25
30
|
}
|
|
31
|
+
function matchesIgnore(name, patterns) {
|
|
32
|
+
return patterns.some((pattern) => pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
|
|
33
|
+
}
|
|
34
|
+
async function findUndefinedRefs(options) {
|
|
35
|
+
const { references, declared } = await scanReferences(options.input, options.exclude);
|
|
36
|
+
const ignore = options.refsIgnore ?? [];
|
|
37
|
+
return references.filter((ref) => !declared.has(ref.name) && !matchesIgnore(ref.name, ignore));
|
|
38
|
+
}
|
|
26
39
|
async function computeOutputs(options) {
|
|
27
40
|
assertPruneCompatible(options);
|
|
28
41
|
const scanned = await scanCss(options.input, options.exclude, options.selectors, options.themes);
|
|
@@ -48,7 +61,8 @@ async function computeOutputs(options) {
|
|
|
48
61
|
group: options.group,
|
|
49
62
|
properties,
|
|
50
63
|
helpers: options.helpers,
|
|
51
|
-
themes: scanned.themes
|
|
64
|
+
themes: scanned.themes,
|
|
65
|
+
docs: scanned.docs
|
|
52
66
|
};
|
|
53
67
|
if (/\.json$/i.test(options.output)) {
|
|
54
68
|
return { outPath, content: generateTokens(names, generatorOptions) };
|
|
@@ -64,11 +78,11 @@ async function computeOutputs(options) {
|
|
|
64
78
|
}
|
|
65
79
|
async function generate(options) {
|
|
66
80
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
67
|
-
await
|
|
68
|
-
await writeFile(outPath, content, "utf8");
|
|
81
|
+
let written = await writeOutput(outPath, content);
|
|
69
82
|
if (dtsPath && dtsContent !== void 0) {
|
|
70
|
-
await
|
|
83
|
+
if (await writeOutput(dtsPath, dtsContent)) written = true;
|
|
71
84
|
}
|
|
85
|
+
return written;
|
|
72
86
|
}
|
|
73
87
|
async function checkGenerated(options) {
|
|
74
88
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
@@ -82,7 +96,9 @@ async function checkGenerated(options) {
|
|
|
82
96
|
}
|
|
83
97
|
|
|
84
98
|
export {
|
|
99
|
+
defineConfig,
|
|
85
100
|
findUnusedVars,
|
|
101
|
+
findUndefinedRefs,
|
|
86
102
|
generate,
|
|
87
103
|
checkGenerated
|
|
88
104
|
};
|