css-typed-vars 0.7.0 → 0.8.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 +106 -11
- package/dist/{chunk-HBUSLYYB.js → chunk-32V3V6WT.js} +4 -3
- package/dist/{chunk-6YWE4A6S.js → chunk-5HPTHW7S.js} +154 -25
- package/dist/{chunk-PLO2JWR7.js → chunk-YDXCJQPL.js} +13 -2
- package/dist/cli.cjs +294 -97
- package/dist/cli.js +138 -75
- package/dist/esbuild.cjs +104 -27
- 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 +169 -26
- package/dist/index.d.cts +15 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.js +10 -2
- package/dist/rollup.cjs +104 -27
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +104 -27
- package/dist/unplugin.d.cts +1 -1
- package/dist/unplugin.d.ts +1 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +104 -27
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +104 -27
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
declare function parseVarNames(css: string, selectors?: string[]): string[];
|
|
2
2
|
declare function parseVarDeclarations(css: string, selectors?: string[]): Map<string, string>;
|
|
3
|
+
interface VarDoc {
|
|
4
|
+
/** Free text of the comment, before any tag. */
|
|
5
|
+
description?: string;
|
|
6
|
+
/** `true` for a bare `@deprecated`, or its reason text. */
|
|
7
|
+
deprecated?: string | true;
|
|
8
|
+
}
|
|
3
9
|
interface ThemedDeclarations {
|
|
4
10
|
/** Base values, from `:root`/`@theme`/`selectors` blocks outside any theme block. */
|
|
5
11
|
declarations: Map<string, string>;
|
|
6
12
|
/** Per theme name, the values declared inside that theme's blocks. */
|
|
7
13
|
themes: Map<string, Map<string, string>>;
|
|
14
|
+
/** `/** ... *\/` doc comments placed directly before a declaration — from a base block, else from a theme block. */
|
|
15
|
+
docs: Map<string, VarDoc>;
|
|
8
16
|
}
|
|
9
17
|
declare function parseThemedDeclarations(css: string, selectors: string[] | undefined, themes: Record<string, string>): ThemedDeclarations;
|
|
10
18
|
interface PropertyRule {
|
|
@@ -13,6 +21,16 @@ interface PropertyRule {
|
|
|
13
21
|
initialValue?: string;
|
|
14
22
|
}
|
|
15
23
|
declare function parsePropertyRules(css: string): Map<string, PropertyRule>;
|
|
24
|
+
interface VarReference {
|
|
25
|
+
name: string;
|
|
26
|
+
/** Whether the reference has a fallback, e.g. `var(--x, red)`. */
|
|
27
|
+
hasFallback: boolean;
|
|
28
|
+
/** 1-based line and column of `var(` in the original css. */
|
|
29
|
+
line: number;
|
|
30
|
+
column: number;
|
|
31
|
+
}
|
|
32
|
+
declare function parseVarReferences(css: string): VarReference[];
|
|
33
|
+
declare function parseDeclaredNames(css: string): Set<string>;
|
|
16
34
|
|
|
17
35
|
type NamingConvention = 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal';
|
|
18
36
|
interface GeneratorOptions {
|
|
@@ -27,6 +45,8 @@ interface GeneratorOptions {
|
|
|
27
45
|
helpers?: boolean;
|
|
28
46
|
/** Per theme name, the values declared for it, emitted as `@theme <name> <value>` JSDoc tags. */
|
|
29
47
|
themes?: Map<string, Map<string, string>>;
|
|
48
|
+
/** CSS doc comments per var name, emitted as the JSDoc description and `@deprecated` tag. */
|
|
49
|
+
docs?: Map<string, VarDoc>;
|
|
30
50
|
}
|
|
31
51
|
type LegacyArgs = [
|
|
32
52
|
prefix?: string,
|
|
@@ -46,4 +66,4 @@ declare function generateDeclaration(varNames: string[], options?: GeneratorOpti
|
|
|
46
66
|
declare function generateDeclaration(varNames: string[], ...args: LegacyArgs): string;
|
|
47
67
|
declare function generateTokens(varNames: string[], options?: GeneratorOptions): string;
|
|
48
68
|
|
|
49
|
-
export { type GeneratorOptions as G, type NamingConvention as N, type PropertyRule as P, type ThemedDeclarations as T,
|
|
69
|
+
export { type GeneratorOptions as G, type NamingConvention as N, type PropertyRule as P, type ThemedDeclarations as T, type VarReference as V, type VarDoc as a, generateDeclaration as b, generateJs as c, generateTokens as d, parsePropertyRules as e, parseThemedDeclarations as f, generateCode as g, parseVarDeclarations as h, parseVarNames as i, parseVarReferences as j, parseDeclaredNames as p };
|
package/dist/index.cjs
CHANGED
|
@@ -31,17 +31,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
checkGenerated: () => checkGenerated,
|
|
34
|
+
findUndefinedRefs: () => findUndefinedRefs,
|
|
34
35
|
findUnusedVars: () => findUnusedVars,
|
|
35
36
|
generate: () => generate,
|
|
36
37
|
generateCode: () => generateCode,
|
|
37
38
|
generateDeclaration: () => generateDeclaration,
|
|
38
39
|
generateJs: () => generateJs,
|
|
39
40
|
generateTokens: () => generateTokens,
|
|
41
|
+
parseDeclaredNames: () => parseDeclaredNames,
|
|
40
42
|
parsePropertyRules: () => parsePropertyRules,
|
|
41
43
|
parseThemedDeclarations: () => parseThemedDeclarations,
|
|
42
44
|
parseVarDeclarations: () => parseVarDeclarations,
|
|
43
45
|
parseVarNames: () => parseVarNames,
|
|
46
|
+
parseVarReferences: () => parseVarReferences,
|
|
44
47
|
scanCss: () => scanCss,
|
|
48
|
+
scanReferences: () => scanReferences,
|
|
45
49
|
scanUsedKeys: () => scanUsedKeys,
|
|
46
50
|
scanVarDeclarations: () => scanVarDeclarations,
|
|
47
51
|
scanVarNames: () => scanVarNames
|
|
@@ -62,6 +66,11 @@ function stepQuote(text, i, quote) {
|
|
|
62
66
|
return { consumed: 1, closed: false };
|
|
63
67
|
}
|
|
64
68
|
function stripComments(text) {
|
|
69
|
+
return stripCommentsWithMap(text).text;
|
|
70
|
+
}
|
|
71
|
+
function stripCommentsWithMap(text) {
|
|
72
|
+
const removed = [];
|
|
73
|
+
const comments = [];
|
|
65
74
|
let result = "";
|
|
66
75
|
let quote = null;
|
|
67
76
|
let urlDepth = 0;
|
|
@@ -96,17 +105,20 @@ function stripComments(text) {
|
|
|
96
105
|
result += char;
|
|
97
106
|
continue;
|
|
98
107
|
}
|
|
108
|
+
comments.push([i, end + 2]);
|
|
109
|
+
removed.push({ at: result.length, length: end + 2 - i });
|
|
99
110
|
i = end + 1;
|
|
100
111
|
continue;
|
|
101
112
|
}
|
|
102
113
|
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
103
114
|
const nl = text.indexOf("\n", i);
|
|
115
|
+
removed.push({ at: result.length, length: (nl === -1 ? text.length : nl) - i });
|
|
104
116
|
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
105
117
|
continue;
|
|
106
118
|
}
|
|
107
119
|
result += char;
|
|
108
120
|
}
|
|
109
|
-
return result;
|
|
121
|
+
return { text: result, removed, comments };
|
|
110
122
|
}
|
|
111
123
|
function extractBlock(text, start) {
|
|
112
124
|
let depth = 1;
|
|
@@ -170,12 +182,12 @@ function extractValue(text, start) {
|
|
|
170
182
|
}
|
|
171
183
|
return text.slice(start, i).trim();
|
|
172
184
|
}
|
|
173
|
-
function forEachProperty(block, onProperty) {
|
|
185
|
+
function forEachProperty(block, onProperty, offset = 0) {
|
|
174
186
|
const masked = maskQuotedContent(block);
|
|
175
187
|
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
176
188
|
const name = prop[0];
|
|
177
189
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
178
|
-
onProperty(name, extractValue(block, colonIndex + 1));
|
|
190
|
+
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
179
191
|
}
|
|
180
192
|
}
|
|
181
193
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -196,7 +208,7 @@ var BASE_SELECTORS = [":root", "@theme"];
|
|
|
196
208
|
function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
|
|
197
209
|
forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
|
|
198
210
|
if (skip.some(([s, e]) => bodyStart >= s && bodyStart < e)) return;
|
|
199
|
-
forEachProperty(block, onProperty);
|
|
211
|
+
forEachProperty(block, onProperty, bodyStart);
|
|
200
212
|
});
|
|
201
213
|
}
|
|
202
214
|
function parseVarNames(css, selectors) {
|
|
@@ -226,30 +238,73 @@ function forEachMediaBlock(stripped, media, onBlock) {
|
|
|
226
238
|
}
|
|
227
239
|
}
|
|
228
240
|
}
|
|
241
|
+
function parseDocComment(body) {
|
|
242
|
+
const lines = body.split("\n").map((line) => line.replace(/^\s*\*?\s?/, "").trimEnd());
|
|
243
|
+
const doc = {};
|
|
244
|
+
const description = [];
|
|
245
|
+
let inTags = false;
|
|
246
|
+
for (const line of lines) {
|
|
247
|
+
const tag = /^@(\w+)\s*(.*)$/.exec(line.trim());
|
|
248
|
+
if (tag) {
|
|
249
|
+
inTags = true;
|
|
250
|
+
if (tag[1] === "deprecated") doc.deprecated = tag[2].trim() || true;
|
|
251
|
+
} else if (!inTags) {
|
|
252
|
+
description.push(line);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const text = description.join("\n").trim();
|
|
256
|
+
if (text) doc.description = text;
|
|
257
|
+
return doc.description !== void 0 || doc.deprecated !== void 0 ? doc : void 0;
|
|
258
|
+
}
|
|
259
|
+
function makeDocReader(css, stripped) {
|
|
260
|
+
const commentStartByEnd = new Map(stripped.comments.map(([start, end]) => [end, start]));
|
|
261
|
+
return (strippedPos) => {
|
|
262
|
+
let end = toRawPosition(stripped, strippedPos);
|
|
263
|
+
while (end > 0 && /\s/.test(css[end - 1])) end--;
|
|
264
|
+
const start = commentStartByEnd.get(end);
|
|
265
|
+
if (start === void 0 || !css.startsWith("/**", start) || end - start <= 4) return void 0;
|
|
266
|
+
return parseDocComment(css.slice(start + 3, end - 2));
|
|
267
|
+
};
|
|
268
|
+
}
|
|
229
269
|
function parseThemedDeclarations(css, selectors, themes) {
|
|
230
|
-
const
|
|
270
|
+
const strippedCss = stripCommentsWithMap(css);
|
|
271
|
+
const stripped = strippedCss.text;
|
|
272
|
+
const docAt = makeDocReader(css, strippedCss);
|
|
273
|
+
const themeDocs = /* @__PURE__ */ new Map();
|
|
231
274
|
const themeValues = /* @__PURE__ */ new Map();
|
|
232
275
|
const skip = [];
|
|
233
276
|
for (const [themeName, themeSelector] of Object.entries(themes)) {
|
|
234
277
|
const values = /* @__PURE__ */ new Map();
|
|
235
278
|
themeValues.set(themeName, values);
|
|
236
|
-
const collect = (block) => forEachProperty(block, (name, value) =>
|
|
279
|
+
const collect = (block, offset) => forEachProperty(block, (name, value, position) => {
|
|
280
|
+
values.set(name, value);
|
|
281
|
+
const doc = docAt(position);
|
|
282
|
+
if (doc) themeDocs.set(name, doc);
|
|
283
|
+
}, offset);
|
|
237
284
|
const selector = themeSelector.trim();
|
|
238
285
|
if (/^@media\b/i.test(selector)) {
|
|
239
286
|
forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
|
|
240
287
|
skip.push(range);
|
|
241
|
-
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, collect);
|
|
288
|
+
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
|
|
242
289
|
});
|
|
243
290
|
} else {
|
|
244
291
|
forEachSelectorBlock(stripped, [selector], (block, range) => {
|
|
245
292
|
skip.push(range);
|
|
246
|
-
collect(block);
|
|
293
|
+
collect(block, range[0]);
|
|
247
294
|
});
|
|
248
295
|
}
|
|
249
296
|
}
|
|
250
297
|
const declarations = /* @__PURE__ */ new Map();
|
|
251
|
-
|
|
252
|
-
|
|
298
|
+
const docs = /* @__PURE__ */ new Map();
|
|
299
|
+
scanDeclarations(css, selectors, (name, value, position) => {
|
|
300
|
+
declarations.set(name, value);
|
|
301
|
+
const doc = docAt(position);
|
|
302
|
+
if (doc) docs.set(name, doc);
|
|
303
|
+
}, skip, stripped);
|
|
304
|
+
for (const [name, doc] of themeDocs) {
|
|
305
|
+
if (!docs.has(name)) docs.set(name, doc);
|
|
306
|
+
}
|
|
307
|
+
return { declarations, themes: themeValues, docs };
|
|
253
308
|
}
|
|
254
309
|
var PROPERTY_DESCRIPTORS = [
|
|
255
310
|
["syntax", "syntax"],
|
|
@@ -280,6 +335,35 @@ function parsePropertyRules(css) {
|
|
|
280
335
|
}
|
|
281
336
|
return rules;
|
|
282
337
|
}
|
|
338
|
+
function toRawPosition(stripped, strippedPos) {
|
|
339
|
+
let rawPos = strippedPos;
|
|
340
|
+
for (const { at, length } of stripped.removed) {
|
|
341
|
+
if (at > strippedPos) break;
|
|
342
|
+
rawPos += length;
|
|
343
|
+
}
|
|
344
|
+
return rawPos;
|
|
345
|
+
}
|
|
346
|
+
function parseVarReferences(css) {
|
|
347
|
+
const stripped = stripCommentsWithMap(css);
|
|
348
|
+
const masked = maskQuotedContent(stripped.text);
|
|
349
|
+
const lineStarts = [0];
|
|
350
|
+
for (let i = 0; i < css.length; i++) if (css[i] === "\n") lineStarts.push(i + 1);
|
|
351
|
+
const refs = [];
|
|
352
|
+
for (const match of masked.matchAll(/\bvar\(\s*(--[\w-]+)\s*(,)?/gi)) {
|
|
353
|
+
const rawPos = toRawPosition(stripped, match.index);
|
|
354
|
+
let line = lineStarts.length - 1;
|
|
355
|
+
while (lineStarts[line] > rawPos) line--;
|
|
356
|
+
refs.push({ name: match[1], hasFallback: match[2] === ",", line: line + 1, column: rawPos - lineStarts[line] + 1 });
|
|
357
|
+
}
|
|
358
|
+
return refs;
|
|
359
|
+
}
|
|
360
|
+
function parseDeclaredNames(css) {
|
|
361
|
+
const masked = maskQuotedContent(stripComments(css));
|
|
362
|
+
const names = /* @__PURE__ */ new Set();
|
|
363
|
+
for (const match of masked.matchAll(/(?<![\w-])(--[\w-]+)\s*:/g)) names.add(match[1]);
|
|
364
|
+
for (const match of masked.matchAll(/@property\s+(--[\w-]+)/g)) names.add(match[1]);
|
|
365
|
+
return names;
|
|
366
|
+
}
|
|
283
367
|
|
|
284
368
|
// src/scanner.ts
|
|
285
369
|
function normalizePatterns(patterns) {
|
|
@@ -311,15 +395,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
311
395
|
const declarations = /* @__PURE__ */ new Map();
|
|
312
396
|
const properties = /* @__PURE__ */ new Map();
|
|
313
397
|
const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
398
|
+
const docs = /* @__PURE__ */ new Map();
|
|
314
399
|
for (const result of perFile) {
|
|
315
400
|
if (!result) continue;
|
|
316
401
|
for (const [name, value] of result.declarations) declarations.set(name, value);
|
|
317
402
|
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
403
|
+
for (const [name, doc] of result.docs) docs.set(name, doc);
|
|
318
404
|
for (const [theme, values] of result.themes) {
|
|
319
405
|
for (const [name, value] of values) themeValues.get(theme).set(name, value);
|
|
320
406
|
}
|
|
321
407
|
}
|
|
322
|
-
return { declarations, properties, themes: themeValues };
|
|
408
|
+
return { declarations, properties, themes: themeValues, docs };
|
|
323
409
|
}
|
|
324
410
|
async function scanVarDeclarations(patterns, exclude, selectors) {
|
|
325
411
|
return (await scanCss(patterns, exclude, selectors)).declarations;
|
|
@@ -363,6 +449,29 @@ async function scanUsage(patterns, exclude) {
|
|
|
363
449
|
for (const text of texts) extractUsage(text, usage);
|
|
364
450
|
return usage;
|
|
365
451
|
}
|
|
452
|
+
async function scanReferences(patterns, exclude) {
|
|
453
|
+
const normalized = normalizePatterns(patterns);
|
|
454
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
455
|
+
const files = (await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude })).sort();
|
|
456
|
+
const perFile = await Promise.all(
|
|
457
|
+
files.map(async (file) => {
|
|
458
|
+
try {
|
|
459
|
+
return { file, css: await (0, import_promises.readFile)(file, "utf8") };
|
|
460
|
+
} catch (err) {
|
|
461
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
})
|
|
465
|
+
);
|
|
466
|
+
const references = [];
|
|
467
|
+
const declared = /* @__PURE__ */ new Set();
|
|
468
|
+
for (const result of perFile) {
|
|
469
|
+
if (!result) continue;
|
|
470
|
+
for (const ref of parseVarReferences(result.css)) references.push({ ...ref, file: result.file });
|
|
471
|
+
for (const name of parseDeclaredNames(result.css)) declared.add(name);
|
|
472
|
+
}
|
|
473
|
+
return { references, declared };
|
|
474
|
+
}
|
|
366
475
|
async function scanUsedKeys(patterns, exclude) {
|
|
367
476
|
return (await scanUsage(patterns, exclude)).keys;
|
|
368
477
|
}
|
|
@@ -461,7 +570,8 @@ function entryMeta(name, options) {
|
|
|
461
570
|
name,
|
|
462
571
|
value: options.declarations?.get(name),
|
|
463
572
|
syntax: options.properties?.get(name)?.syntax,
|
|
464
|
-
...themes.length > 0 ? { themes } : {}
|
|
573
|
+
...themes.length > 0 ? { themes } : {},
|
|
574
|
+
...options.docs?.has(name) ? { doc: options.docs.get(name) } : {}
|
|
465
575
|
};
|
|
466
576
|
}
|
|
467
577
|
function buildEntries(varNames, options) {
|
|
@@ -501,8 +611,12 @@ function buildGroupedEntries(varNames, options) {
|
|
|
501
611
|
const ungrouped = [...ungroupedMap].map(([key, entry]) => ({ key, ...entry }));
|
|
502
612
|
return { groups, ungrouped };
|
|
503
613
|
}
|
|
504
|
-
|
|
505
|
-
|
|
614
|
+
var escapeComment = (text) => text.replace(/\*\//g, "*\\/");
|
|
615
|
+
function renderJsDoc(tags, indent) {
|
|
616
|
+
if (tags.length === 0) return [];
|
|
617
|
+
if (tags.length === 1 && !tags[0].includes("\n")) return [`${indent}/** ${escapeComment(tags[0])} */`];
|
|
618
|
+
const body = tags.flatMap((tag) => escapeComment(tag).split("\n")).map((line) => `${indent} * ${line}`.trimEnd());
|
|
619
|
+
return [`${indent}/**`, ...body, `${indent} */`];
|
|
506
620
|
}
|
|
507
621
|
function unquoteCssString(value) {
|
|
508
622
|
const quote = value[0];
|
|
@@ -511,11 +625,15 @@ function unquoteCssString(value) {
|
|
|
511
625
|
}
|
|
512
626
|
return value;
|
|
513
627
|
}
|
|
514
|
-
function
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
628
|
+
function entryTags({ value, syntax, themes, doc }) {
|
|
629
|
+
const deprecated = doc?.deprecated;
|
|
630
|
+
return [
|
|
631
|
+
...doc?.description ? [doc.description] : [],
|
|
632
|
+
...value ? [`@default ${value}`] : [],
|
|
633
|
+
...(themes ?? []).map(([theme, themeValue]) => `@theme ${theme} ${themeValue}`),
|
|
634
|
+
...syntax ? [`@syntax ${unquoteCssString(syntax)}`] : [],
|
|
635
|
+
...deprecated ? [deprecated === true ? "@deprecated" : `@deprecated ${deprecated}`] : []
|
|
636
|
+
];
|
|
519
637
|
}
|
|
520
638
|
var ENUM_SYNTAX_RE = /^[\w-]+(?:\s*\|\s*[\w-]+)+$/;
|
|
521
639
|
function parseEnumSyntax(syntax) {
|
|
@@ -547,11 +665,9 @@ function buildTailBlocks(varNames, group, properties) {
|
|
|
547
665
|
return blocks;
|
|
548
666
|
}
|
|
549
667
|
function renderEntryLines(entries, indent, lineFor) {
|
|
550
|
-
return entries.flatMap((
|
|
551
|
-
...
|
|
552
|
-
|
|
553
|
-
...syntax ? [formatSyntaxComment(syntax, indent)] : [],
|
|
554
|
-
`${indent}${lineFor(key, name)}`
|
|
668
|
+
return entries.flatMap((entry) => [
|
|
669
|
+
...renderJsDoc(entryTags(entry), indent),
|
|
670
|
+
`${indent}${lineFor(entry.key, entry.name)}`
|
|
555
671
|
]);
|
|
556
672
|
}
|
|
557
673
|
function renderGroupedBlock(varNames, options, indent, closeSuffix, lineFor) {
|
|
@@ -598,6 +714,12 @@ var HELPERS_TS = [
|
|
|
598
714
|
"",
|
|
599
715
|
"export function setCssVar(ref: CssVarRef, value: string, el: HTMLElement = document.documentElement): void {",
|
|
600
716
|
" el.style.setProperty(ref.slice(4, -1), value);",
|
|
717
|
+
"}",
|
|
718
|
+
"",
|
|
719
|
+
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
720
|
+
"",
|
|
721
|
+
"export function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F> {",
|
|
722
|
+
" return `${ref.slice(0, -1)}, ${fallback})` as CssVarWithFallback<R, F>;",
|
|
601
723
|
"}"
|
|
602
724
|
];
|
|
603
725
|
var HELPERS_JS = [
|
|
@@ -607,11 +729,17 @@ var HELPERS_JS = [
|
|
|
607
729
|
"",
|
|
608
730
|
"export function setCssVar(ref, value, el = document.documentElement) {",
|
|
609
731
|
" el.style.setProperty(ref.slice(4, -1), value);",
|
|
732
|
+
"}",
|
|
733
|
+
"",
|
|
734
|
+
"export function withFallback(ref, fallback) {",
|
|
735
|
+
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
610
736
|
"}"
|
|
611
737
|
];
|
|
612
738
|
var HELPERS_DTS = [
|
|
613
739
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
614
|
-
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;"
|
|
740
|
+
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
741
|
+
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
742
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
615
743
|
];
|
|
616
744
|
function generateCode(varNames, ...args) {
|
|
617
745
|
const options = normalizeOptions(args);
|
|
@@ -680,6 +808,8 @@ function generateTokens(varNames, options = {}) {
|
|
|
680
808
|
const token = { $value: resolve2(value) };
|
|
681
809
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
682
810
|
if (type) token.$type = type;
|
|
811
|
+
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
812
|
+
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
683
813
|
if (meta.themes) {
|
|
684
814
|
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
|
|
685
815
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
@@ -725,6 +855,14 @@ async function findUnusedVars(options) {
|
|
|
725
855
|
const names = collectVarNames(scanned);
|
|
726
856
|
return partitionUnused(names, usage, options).unused;
|
|
727
857
|
}
|
|
858
|
+
function matchesIgnore(name, patterns) {
|
|
859
|
+
return patterns.some((pattern) => pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
|
|
860
|
+
}
|
|
861
|
+
async function findUndefinedRefs(options) {
|
|
862
|
+
const { references, declared } = await scanReferences(options.input, options.exclude);
|
|
863
|
+
const ignore = options.refsIgnore ?? [];
|
|
864
|
+
return references.filter((ref) => !declared.has(ref.name) && !matchesIgnore(ref.name, ignore));
|
|
865
|
+
}
|
|
728
866
|
async function computeOutputs(options) {
|
|
729
867
|
assertPruneCompatible(options);
|
|
730
868
|
const scanned = await scanCss(options.input, options.exclude, options.selectors, options.themes);
|
|
@@ -750,7 +888,8 @@ async function computeOutputs(options) {
|
|
|
750
888
|
group: options.group,
|
|
751
889
|
properties,
|
|
752
890
|
helpers: options.helpers,
|
|
753
|
-
themes: scanned.themes
|
|
891
|
+
themes: scanned.themes,
|
|
892
|
+
docs: scanned.docs
|
|
754
893
|
};
|
|
755
894
|
if (/\.json$/i.test(options.output)) {
|
|
756
895
|
return { outPath, content: generateTokens(names, generatorOptions) };
|
|
@@ -785,17 +924,21 @@ async function checkGenerated(options) {
|
|
|
785
924
|
// Annotate the CommonJS export names for ESM import in node:
|
|
786
925
|
0 && (module.exports = {
|
|
787
926
|
checkGenerated,
|
|
927
|
+
findUndefinedRefs,
|
|
788
928
|
findUnusedVars,
|
|
789
929
|
generate,
|
|
790
930
|
generateCode,
|
|
791
931
|
generateDeclaration,
|
|
792
932
|
generateJs,
|
|
793
933
|
generateTokens,
|
|
934
|
+
parseDeclaredNames,
|
|
794
935
|
parsePropertyRules,
|
|
795
936
|
parseThemedDeclarations,
|
|
796
937
|
parseVarDeclarations,
|
|
797
938
|
parseVarNames,
|
|
939
|
+
parseVarReferences,
|
|
798
940
|
scanCss,
|
|
941
|
+
scanReferences,
|
|
799
942
|
scanUsedKeys,
|
|
800
943
|
scanVarDeclarations,
|
|
801
944
|
scanVarNames
|
package/dist/index.d.cts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
|
-
import { P as PropertyRule, N as NamingConvention } from './generator-
|
|
2
|
-
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode,
|
|
1
|
+
import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-DJ9uOYBP.cjs';
|
|
2
|
+
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode, b as generateDeclaration, c as generateJs, d as generateTokens, p as parseDeclaredNames, e as parsePropertyRules, f as parseThemedDeclarations, h as parseVarDeclarations, i as parseVarNames, j as parseVarReferences } from './generator-DJ9uOYBP.cjs';
|
|
3
3
|
|
|
4
4
|
interface ScannedCss {
|
|
5
5
|
declarations: Map<string, string>;
|
|
6
6
|
properties: Map<string, PropertyRule>;
|
|
7
7
|
/** Per theme name (from the `themes` option), the values declared for it. Empty without `themes`. */
|
|
8
8
|
themes: Map<string, Map<string, string>>;
|
|
9
|
+
/** `/** ... *\/` doc comments placed directly before a declaration. */
|
|
10
|
+
docs: Map<string, VarDoc>;
|
|
9
11
|
}
|
|
10
12
|
declare function scanCss(patterns: string | string[], exclude?: string | string[], selectors?: string[], themes?: Record<string, string>): Promise<ScannedCss>;
|
|
11
13
|
declare function scanVarDeclarations(patterns: string | string[], exclude?: string | string[], selectors?: string[]): Promise<Map<string, string>>;
|
|
12
14
|
declare function scanVarNames(patterns: string | string[], exclude?: string | string[], selectors?: string[]): Promise<string[]>;
|
|
15
|
+
interface FileReference extends VarReference {
|
|
16
|
+
file: string;
|
|
17
|
+
}
|
|
18
|
+
declare function scanReferences(patterns: string | string[], exclude?: string | string[]): Promise<{
|
|
19
|
+
references: FileReference[];
|
|
20
|
+
declared: Set<string>;
|
|
21
|
+
}>;
|
|
13
22
|
declare function scanUsedKeys(patterns: string | string[], exclude?: string | string[]): Promise<Set<string>>;
|
|
14
23
|
|
|
15
24
|
interface GenerateOptions {
|
|
@@ -34,9 +43,12 @@ interface GenerateOptions {
|
|
|
34
43
|
usageExclude?: string | string[];
|
|
35
44
|
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers. */
|
|
36
45
|
helpers?: boolean;
|
|
46
|
+
/** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
|
|
47
|
+
refsIgnore?: string[];
|
|
37
48
|
}
|
|
38
49
|
declare function findUnusedVars(options: Pick<GenerateOptions, 'input' | 'exclude' | 'selectors' | 'themes' | 'prefix' | 'naming' | 'group' | 'usage' | 'usageExclude'>): Promise<string[]>;
|
|
50
|
+
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore'>): Promise<FileReference[]>;
|
|
39
51
|
declare function generate(options: GenerateOptions): Promise<void>;
|
|
40
52
|
declare function checkGenerated(options: GenerateOptions): Promise<boolean>;
|
|
41
53
|
|
|
42
|
-
export { type GenerateOptions, NamingConvention, PropertyRule, checkGenerated, findUnusedVars, generate, scanCss, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
|
54
|
+
export { type FileReference, type GenerateOptions, NamingConvention, PropertyRule, VarDoc, VarReference, checkGenerated, findUndefinedRefs, findUnusedVars, generate, scanCss, scanReferences, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
|
-
import { P as PropertyRule, N as NamingConvention } from './generator-
|
|
2
|
-
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode,
|
|
1
|
+
import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-DJ9uOYBP.js';
|
|
2
|
+
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode, b as generateDeclaration, c as generateJs, d as generateTokens, p as parseDeclaredNames, e as parsePropertyRules, f as parseThemedDeclarations, h as parseVarDeclarations, i as parseVarNames, j as parseVarReferences } from './generator-DJ9uOYBP.js';
|
|
3
3
|
|
|
4
4
|
interface ScannedCss {
|
|
5
5
|
declarations: Map<string, string>;
|
|
6
6
|
properties: Map<string, PropertyRule>;
|
|
7
7
|
/** Per theme name (from the `themes` option), the values declared for it. Empty without `themes`. */
|
|
8
8
|
themes: Map<string, Map<string, string>>;
|
|
9
|
+
/** `/** ... *\/` doc comments placed directly before a declaration. */
|
|
10
|
+
docs: Map<string, VarDoc>;
|
|
9
11
|
}
|
|
10
12
|
declare function scanCss(patterns: string | string[], exclude?: string | string[], selectors?: string[], themes?: Record<string, string>): Promise<ScannedCss>;
|
|
11
13
|
declare function scanVarDeclarations(patterns: string | string[], exclude?: string | string[], selectors?: string[]): Promise<Map<string, string>>;
|
|
12
14
|
declare function scanVarNames(patterns: string | string[], exclude?: string | string[], selectors?: string[]): Promise<string[]>;
|
|
15
|
+
interface FileReference extends VarReference {
|
|
16
|
+
file: string;
|
|
17
|
+
}
|
|
18
|
+
declare function scanReferences(patterns: string | string[], exclude?: string | string[]): Promise<{
|
|
19
|
+
references: FileReference[];
|
|
20
|
+
declared: Set<string>;
|
|
21
|
+
}>;
|
|
13
22
|
declare function scanUsedKeys(patterns: string | string[], exclude?: string | string[]): Promise<Set<string>>;
|
|
14
23
|
|
|
15
24
|
interface GenerateOptions {
|
|
@@ -34,9 +43,12 @@ interface GenerateOptions {
|
|
|
34
43
|
usageExclude?: string | string[];
|
|
35
44
|
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers. */
|
|
36
45
|
helpers?: boolean;
|
|
46
|
+
/** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
|
|
47
|
+
refsIgnore?: string[];
|
|
37
48
|
}
|
|
38
49
|
declare function findUnusedVars(options: Pick<GenerateOptions, 'input' | 'exclude' | 'selectors' | 'themes' | 'prefix' | 'naming' | 'group' | 'usage' | 'usageExclude'>): Promise<string[]>;
|
|
50
|
+
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore'>): Promise<FileReference[]>;
|
|
39
51
|
declare function generate(options: GenerateOptions): Promise<void>;
|
|
40
52
|
declare function checkGenerated(options: GenerateOptions): Promise<boolean>;
|
|
41
53
|
|
|
42
|
-
export { type GenerateOptions, NamingConvention, PropertyRule, checkGenerated, findUnusedVars, generate, scanCss, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
|
54
|
+
export { type FileReference, type GenerateOptions, NamingConvention, PropertyRule, VarDoc, VarReference, checkGenerated, findUndefinedRefs, findUnusedVars, generate, scanCss, scanReferences, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
package/dist/index.js
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
import {
|
|
2
2
|
checkGenerated,
|
|
3
|
+
findUndefinedRefs,
|
|
3
4
|
findUnusedVars,
|
|
4
5
|
generate
|
|
5
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-YDXCJQPL.js";
|
|
6
7
|
import {
|
|
7
8
|
generateCode,
|
|
8
9
|
generateDeclaration,
|
|
9
10
|
generateJs,
|
|
10
11
|
generateTokens,
|
|
12
|
+
parseDeclaredNames,
|
|
11
13
|
parsePropertyRules,
|
|
12
14
|
parseThemedDeclarations,
|
|
13
15
|
parseVarDeclarations,
|
|
14
16
|
parseVarNames,
|
|
17
|
+
parseVarReferences,
|
|
15
18
|
scanCss,
|
|
19
|
+
scanReferences,
|
|
16
20
|
scanUsedKeys,
|
|
17
21
|
scanVarDeclarations,
|
|
18
22
|
scanVarNames
|
|
19
|
-
} from "./chunk-
|
|
23
|
+
} from "./chunk-5HPTHW7S.js";
|
|
20
24
|
export {
|
|
21
25
|
checkGenerated,
|
|
26
|
+
findUndefinedRefs,
|
|
22
27
|
findUnusedVars,
|
|
23
28
|
generate,
|
|
24
29
|
generateCode,
|
|
25
30
|
generateDeclaration,
|
|
26
31
|
generateJs,
|
|
27
32
|
generateTokens,
|
|
33
|
+
parseDeclaredNames,
|
|
28
34
|
parsePropertyRules,
|
|
29
35
|
parseThemedDeclarations,
|
|
30
36
|
parseVarDeclarations,
|
|
31
37
|
parseVarNames,
|
|
38
|
+
parseVarReferences,
|
|
32
39
|
scanCss,
|
|
40
|
+
scanReferences,
|
|
33
41
|
scanUsedKeys,
|
|
34
42
|
scanVarDeclarations,
|
|
35
43
|
scanVarNames
|