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
package/dist/rollup.cjs
CHANGED
|
@@ -51,6 +51,11 @@ function stepQuote(text, i, quote) {
|
|
|
51
51
|
return { consumed: 1, closed: false };
|
|
52
52
|
}
|
|
53
53
|
function stripComments(text) {
|
|
54
|
+
return stripCommentsWithMap(text).text;
|
|
55
|
+
}
|
|
56
|
+
function stripCommentsWithMap(text) {
|
|
57
|
+
const removed = [];
|
|
58
|
+
const comments = [];
|
|
54
59
|
let result = "";
|
|
55
60
|
let quote = null;
|
|
56
61
|
let urlDepth = 0;
|
|
@@ -85,17 +90,20 @@ function stripComments(text) {
|
|
|
85
90
|
result += char;
|
|
86
91
|
continue;
|
|
87
92
|
}
|
|
93
|
+
comments.push([i, end + 2]);
|
|
94
|
+
removed.push({ at: result.length, length: end + 2 - i });
|
|
88
95
|
i = end + 1;
|
|
89
96
|
continue;
|
|
90
97
|
}
|
|
91
98
|
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
92
99
|
const nl = text.indexOf("\n", i);
|
|
100
|
+
removed.push({ at: result.length, length: (nl === -1 ? text.length : nl) - i });
|
|
93
101
|
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
94
102
|
continue;
|
|
95
103
|
}
|
|
96
104
|
result += char;
|
|
97
105
|
}
|
|
98
|
-
return result;
|
|
106
|
+
return { text: result, removed, comments };
|
|
99
107
|
}
|
|
100
108
|
function extractBlock(text, start) {
|
|
101
109
|
let depth = 1;
|
|
@@ -159,12 +167,12 @@ function extractValue(text, start) {
|
|
|
159
167
|
}
|
|
160
168
|
return text.slice(start, i).trim();
|
|
161
169
|
}
|
|
162
|
-
function forEachProperty(block, onProperty) {
|
|
170
|
+
function forEachProperty(block, onProperty, offset = 0) {
|
|
163
171
|
const masked = maskQuotedContent(block);
|
|
164
172
|
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
165
173
|
const name = prop[0];
|
|
166
174
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
167
|
-
onProperty(name, extractValue(block, colonIndex + 1));
|
|
175
|
+
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
168
176
|
}
|
|
169
177
|
}
|
|
170
178
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -185,7 +193,7 @@ var BASE_SELECTORS = [":root", "@theme"];
|
|
|
185
193
|
function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
|
|
186
194
|
forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
|
|
187
195
|
if (skip.some(([s, e]) => bodyStart >= s && bodyStart < e)) return;
|
|
188
|
-
forEachProperty(block, onProperty);
|
|
196
|
+
forEachProperty(block, onProperty, bodyStart);
|
|
189
197
|
});
|
|
190
198
|
}
|
|
191
199
|
var normalizePrelude = (prelude) => prelude.replace(/\s+/g, "").toLowerCase();
|
|
@@ -205,30 +213,73 @@ function forEachMediaBlock(stripped, media, onBlock) {
|
|
|
205
213
|
}
|
|
206
214
|
}
|
|
207
215
|
}
|
|
216
|
+
function parseDocComment(body) {
|
|
217
|
+
const lines = body.split("\n").map((line) => line.replace(/^\s*\*?\s?/, "").trimEnd());
|
|
218
|
+
const doc = {};
|
|
219
|
+
const description = [];
|
|
220
|
+
let inTags = false;
|
|
221
|
+
for (const line of lines) {
|
|
222
|
+
const tag = /^@(\w+)\s*(.*)$/.exec(line.trim());
|
|
223
|
+
if (tag) {
|
|
224
|
+
inTags = true;
|
|
225
|
+
if (tag[1] === "deprecated") doc.deprecated = tag[2].trim() || true;
|
|
226
|
+
} else if (!inTags) {
|
|
227
|
+
description.push(line);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const text = description.join("\n").trim();
|
|
231
|
+
if (text) doc.description = text;
|
|
232
|
+
return doc.description !== void 0 || doc.deprecated !== void 0 ? doc : void 0;
|
|
233
|
+
}
|
|
234
|
+
function makeDocReader(css, stripped) {
|
|
235
|
+
const commentStartByEnd = new Map(stripped.comments.map(([start, end]) => [end, start]));
|
|
236
|
+
return (strippedPos) => {
|
|
237
|
+
let end = toRawPosition(stripped, strippedPos);
|
|
238
|
+
while (end > 0 && /\s/.test(css[end - 1])) end--;
|
|
239
|
+
const start = commentStartByEnd.get(end);
|
|
240
|
+
if (start === void 0 || !css.startsWith("/**", start) || end - start <= 4) return void 0;
|
|
241
|
+
return parseDocComment(css.slice(start + 3, end - 2));
|
|
242
|
+
};
|
|
243
|
+
}
|
|
208
244
|
function parseThemedDeclarations(css, selectors, themes) {
|
|
209
|
-
const
|
|
245
|
+
const strippedCss = stripCommentsWithMap(css);
|
|
246
|
+
const stripped = strippedCss.text;
|
|
247
|
+
const docAt = makeDocReader(css, strippedCss);
|
|
248
|
+
const themeDocs = /* @__PURE__ */ new Map();
|
|
210
249
|
const themeValues = /* @__PURE__ */ new Map();
|
|
211
250
|
const skip = [];
|
|
212
251
|
for (const [themeName, themeSelector] of Object.entries(themes)) {
|
|
213
252
|
const values = /* @__PURE__ */ new Map();
|
|
214
253
|
themeValues.set(themeName, values);
|
|
215
|
-
const collect = (block) => forEachProperty(block, (name, value) =>
|
|
254
|
+
const collect = (block, offset) => forEachProperty(block, (name, value, position) => {
|
|
255
|
+
values.set(name, value);
|
|
256
|
+
const doc = docAt(position);
|
|
257
|
+
if (doc) themeDocs.set(name, doc);
|
|
258
|
+
}, offset);
|
|
216
259
|
const selector = themeSelector.trim();
|
|
217
260
|
if (/^@media\b/i.test(selector)) {
|
|
218
261
|
forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
|
|
219
262
|
skip.push(range);
|
|
220
|
-
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, collect);
|
|
263
|
+
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
|
|
221
264
|
});
|
|
222
265
|
} else {
|
|
223
266
|
forEachSelectorBlock(stripped, [selector], (block, range) => {
|
|
224
267
|
skip.push(range);
|
|
225
|
-
collect(block);
|
|
268
|
+
collect(block, range[0]);
|
|
226
269
|
});
|
|
227
270
|
}
|
|
228
271
|
}
|
|
229
272
|
const declarations = /* @__PURE__ */ new Map();
|
|
230
|
-
|
|
231
|
-
|
|
273
|
+
const docs = /* @__PURE__ */ new Map();
|
|
274
|
+
scanDeclarations(css, selectors, (name, value, position) => {
|
|
275
|
+
declarations.set(name, value);
|
|
276
|
+
const doc = docAt(position);
|
|
277
|
+
if (doc) docs.set(name, doc);
|
|
278
|
+
}, skip, stripped);
|
|
279
|
+
for (const [name, doc] of themeDocs) {
|
|
280
|
+
if (!docs.has(name)) docs.set(name, doc);
|
|
281
|
+
}
|
|
282
|
+
return { declarations, themes: themeValues, docs };
|
|
232
283
|
}
|
|
233
284
|
var PROPERTY_DESCRIPTORS = [
|
|
234
285
|
["syntax", "syntax"],
|
|
@@ -259,6 +310,14 @@ function parsePropertyRules(css) {
|
|
|
259
310
|
}
|
|
260
311
|
return rules;
|
|
261
312
|
}
|
|
313
|
+
function toRawPosition(stripped, strippedPos) {
|
|
314
|
+
let rawPos = strippedPos;
|
|
315
|
+
for (const { at, length } of stripped.removed) {
|
|
316
|
+
if (at > strippedPos) break;
|
|
317
|
+
rawPos += length;
|
|
318
|
+
}
|
|
319
|
+
return rawPos;
|
|
320
|
+
}
|
|
262
321
|
|
|
263
322
|
// src/scanner.ts
|
|
264
323
|
function normalizePatterns(patterns) {
|
|
@@ -290,15 +349,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
290
349
|
const declarations = /* @__PURE__ */ new Map();
|
|
291
350
|
const properties = /* @__PURE__ */ new Map();
|
|
292
351
|
const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
352
|
+
const docs = /* @__PURE__ */ new Map();
|
|
293
353
|
for (const result of perFile) {
|
|
294
354
|
if (!result) continue;
|
|
295
355
|
for (const [name, value] of result.declarations) declarations.set(name, value);
|
|
296
356
|
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
357
|
+
for (const [name, doc] of result.docs) docs.set(name, doc);
|
|
297
358
|
for (const [theme, values] of result.themes) {
|
|
298
359
|
for (const [name, value] of values) themeValues.get(theme).set(name, value);
|
|
299
360
|
}
|
|
300
361
|
}
|
|
301
|
-
return { declarations, properties, themes: themeValues };
|
|
362
|
+
return { declarations, properties, themes: themeValues, docs };
|
|
302
363
|
}
|
|
303
364
|
var ROOT_RE = /\bcssVars\b/g;
|
|
304
365
|
var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
|
|
@@ -430,7 +491,8 @@ function entryMeta(name, options) {
|
|
|
430
491
|
name,
|
|
431
492
|
value: options.declarations?.get(name),
|
|
432
493
|
syntax: options.properties?.get(name)?.syntax,
|
|
433
|
-
...themes.length > 0 ? { themes } : {}
|
|
494
|
+
...themes.length > 0 ? { themes } : {},
|
|
495
|
+
...options.docs?.has(name) ? { doc: options.docs.get(name) } : {}
|
|
434
496
|
};
|
|
435
497
|
}
|
|
436
498
|
function buildEntries(varNames, options) {
|
|
@@ -470,8 +532,12 @@ function buildGroupedEntries(varNames, options) {
|
|
|
470
532
|
const ungrouped = [...ungroupedMap].map(([key, entry]) => ({ key, ...entry }));
|
|
471
533
|
return { groups, ungrouped };
|
|
472
534
|
}
|
|
473
|
-
|
|
474
|
-
|
|
535
|
+
var escapeComment = (text) => text.replace(/\*\//g, "*\\/");
|
|
536
|
+
function renderJsDoc(tags, indent) {
|
|
537
|
+
if (tags.length === 0) return [];
|
|
538
|
+
if (tags.length === 1 && !tags[0].includes("\n")) return [`${indent}/** ${escapeComment(tags[0])} */`];
|
|
539
|
+
const body = tags.flatMap((tag) => escapeComment(tag).split("\n")).map((line) => `${indent} * ${line}`.trimEnd());
|
|
540
|
+
return [`${indent}/**`, ...body, `${indent} */`];
|
|
475
541
|
}
|
|
476
542
|
function unquoteCssString(value) {
|
|
477
543
|
const quote = value[0];
|
|
@@ -480,11 +546,15 @@ function unquoteCssString(value) {
|
|
|
480
546
|
}
|
|
481
547
|
return value;
|
|
482
548
|
}
|
|
483
|
-
function
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
549
|
+
function entryTags({ value, syntax, themes, doc }) {
|
|
550
|
+
const deprecated = doc?.deprecated;
|
|
551
|
+
return [
|
|
552
|
+
...doc?.description ? [doc.description] : [],
|
|
553
|
+
...value ? [`@default ${value}`] : [],
|
|
554
|
+
...(themes ?? []).map(([theme, themeValue]) => `@theme ${theme} ${themeValue}`),
|
|
555
|
+
...syntax ? [`@syntax ${unquoteCssString(syntax)}`] : [],
|
|
556
|
+
...deprecated ? [deprecated === true ? "@deprecated" : `@deprecated ${deprecated}`] : []
|
|
557
|
+
];
|
|
488
558
|
}
|
|
489
559
|
var ENUM_SYNTAX_RE = /^[\w-]+(?:\s*\|\s*[\w-]+)+$/;
|
|
490
560
|
function parseEnumSyntax(syntax) {
|
|
@@ -516,11 +586,9 @@ function buildTailBlocks(varNames, group, properties) {
|
|
|
516
586
|
return blocks;
|
|
517
587
|
}
|
|
518
588
|
function renderEntryLines(entries, indent, lineFor) {
|
|
519
|
-
return entries.flatMap((
|
|
520
|
-
...
|
|
521
|
-
|
|
522
|
-
...syntax ? [formatSyntaxComment(syntax, indent)] : [],
|
|
523
|
-
`${indent}${lineFor(key, name)}`
|
|
589
|
+
return entries.flatMap((entry) => [
|
|
590
|
+
...renderJsDoc(entryTags(entry), indent),
|
|
591
|
+
`${indent}${lineFor(entry.key, entry.name)}`
|
|
524
592
|
]);
|
|
525
593
|
}
|
|
526
594
|
function renderGroupedBlock(varNames, options, indent, closeSuffix, lineFor) {
|
|
@@ -567,11 +635,17 @@ var HELPERS_JS = [
|
|
|
567
635
|
"",
|
|
568
636
|
"export function setCssVar(ref, value, el = document.documentElement) {",
|
|
569
637
|
" el.style.setProperty(ref.slice(4, -1), value);",
|
|
638
|
+
"}",
|
|
639
|
+
"",
|
|
640
|
+
"export function withFallback(ref, fallback) {",
|
|
641
|
+
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
570
642
|
"}"
|
|
571
643
|
];
|
|
572
644
|
var HELPERS_DTS = [
|
|
573
645
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
574
|
-
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;"
|
|
646
|
+
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
647
|
+
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
648
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
575
649
|
];
|
|
576
650
|
function generateJs(varNames, ...args) {
|
|
577
651
|
const options = normalizeOptions(args);
|
|
@@ -625,6 +699,8 @@ function generateTokens(varNames, options = {}) {
|
|
|
625
699
|
const token = { $value: resolve2(value) };
|
|
626
700
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
627
701
|
if (type) token.$type = type;
|
|
702
|
+
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
703
|
+
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
628
704
|
if (meta.themes) {
|
|
629
705
|
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
|
|
630
706
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
@@ -687,14 +763,15 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
687
763
|
}
|
|
688
764
|
return used;
|
|
689
765
|
};
|
|
690
|
-
const generatorOptions = ({ declarations, properties, themes }) => ({
|
|
766
|
+
const generatorOptions = ({ declarations, properties, themes, docs }) => ({
|
|
691
767
|
prefix: options.prefix,
|
|
692
768
|
naming: options.naming,
|
|
693
769
|
declarations,
|
|
694
770
|
group: options.group,
|
|
695
771
|
properties,
|
|
696
772
|
helpers: options.helpers,
|
|
697
|
-
themes
|
|
773
|
+
themes,
|
|
774
|
+
docs
|
|
698
775
|
});
|
|
699
776
|
const resolveNames = async (scanned) => {
|
|
700
777
|
const names = collectVarNames(scanned);
|
package/dist/rollup.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as rollup from 'rollup';
|
|
2
2
|
import { Options } from './unplugin.cjs';
|
|
3
3
|
import 'unplugin';
|
|
4
|
-
import './generator-
|
|
4
|
+
import './generator-DJ9uOYBP.cjs';
|
|
5
5
|
|
|
6
6
|
declare const _default: (options: Options) => rollup.Plugin<any> | rollup.Plugin<any>[];
|
|
7
7
|
|
package/dist/rollup.d.ts
CHANGED
package/dist/rollup.js
CHANGED
package/dist/unplugin.cjs
CHANGED
|
@@ -49,6 +49,11 @@ function stepQuote(text, i, quote) {
|
|
|
49
49
|
return { consumed: 1, closed: false };
|
|
50
50
|
}
|
|
51
51
|
function stripComments(text) {
|
|
52
|
+
return stripCommentsWithMap(text).text;
|
|
53
|
+
}
|
|
54
|
+
function stripCommentsWithMap(text) {
|
|
55
|
+
const removed = [];
|
|
56
|
+
const comments = [];
|
|
52
57
|
let result = "";
|
|
53
58
|
let quote = null;
|
|
54
59
|
let urlDepth = 0;
|
|
@@ -83,17 +88,20 @@ function stripComments(text) {
|
|
|
83
88
|
result += char;
|
|
84
89
|
continue;
|
|
85
90
|
}
|
|
91
|
+
comments.push([i, end + 2]);
|
|
92
|
+
removed.push({ at: result.length, length: end + 2 - i });
|
|
86
93
|
i = end + 1;
|
|
87
94
|
continue;
|
|
88
95
|
}
|
|
89
96
|
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
90
97
|
const nl = text.indexOf("\n", i);
|
|
98
|
+
removed.push({ at: result.length, length: (nl === -1 ? text.length : nl) - i });
|
|
91
99
|
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
92
100
|
continue;
|
|
93
101
|
}
|
|
94
102
|
result += char;
|
|
95
103
|
}
|
|
96
|
-
return result;
|
|
104
|
+
return { text: result, removed, comments };
|
|
97
105
|
}
|
|
98
106
|
function extractBlock(text, start) {
|
|
99
107
|
let depth = 1;
|
|
@@ -157,12 +165,12 @@ function extractValue(text, start) {
|
|
|
157
165
|
}
|
|
158
166
|
return text.slice(start, i).trim();
|
|
159
167
|
}
|
|
160
|
-
function forEachProperty(block, onProperty) {
|
|
168
|
+
function forEachProperty(block, onProperty, offset = 0) {
|
|
161
169
|
const masked = maskQuotedContent(block);
|
|
162
170
|
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
163
171
|
const name = prop[0];
|
|
164
172
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
165
|
-
onProperty(name, extractValue(block, colonIndex + 1));
|
|
173
|
+
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
166
174
|
}
|
|
167
175
|
}
|
|
168
176
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -183,7 +191,7 @@ var BASE_SELECTORS = [":root", "@theme"];
|
|
|
183
191
|
function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
|
|
184
192
|
forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
|
|
185
193
|
if (skip.some(([s, e]) => bodyStart >= s && bodyStart < e)) return;
|
|
186
|
-
forEachProperty(block, onProperty);
|
|
194
|
+
forEachProperty(block, onProperty, bodyStart);
|
|
187
195
|
});
|
|
188
196
|
}
|
|
189
197
|
var normalizePrelude = (prelude) => prelude.replace(/\s+/g, "").toLowerCase();
|
|
@@ -203,30 +211,73 @@ function forEachMediaBlock(stripped, media, onBlock) {
|
|
|
203
211
|
}
|
|
204
212
|
}
|
|
205
213
|
}
|
|
214
|
+
function parseDocComment(body) {
|
|
215
|
+
const lines = body.split("\n").map((line) => line.replace(/^\s*\*?\s?/, "").trimEnd());
|
|
216
|
+
const doc = {};
|
|
217
|
+
const description = [];
|
|
218
|
+
let inTags = false;
|
|
219
|
+
for (const line of lines) {
|
|
220
|
+
const tag = /^@(\w+)\s*(.*)$/.exec(line.trim());
|
|
221
|
+
if (tag) {
|
|
222
|
+
inTags = true;
|
|
223
|
+
if (tag[1] === "deprecated") doc.deprecated = tag[2].trim() || true;
|
|
224
|
+
} else if (!inTags) {
|
|
225
|
+
description.push(line);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const text = description.join("\n").trim();
|
|
229
|
+
if (text) doc.description = text;
|
|
230
|
+
return doc.description !== void 0 || doc.deprecated !== void 0 ? doc : void 0;
|
|
231
|
+
}
|
|
232
|
+
function makeDocReader(css, stripped) {
|
|
233
|
+
const commentStartByEnd = new Map(stripped.comments.map(([start, end]) => [end, start]));
|
|
234
|
+
return (strippedPos) => {
|
|
235
|
+
let end = toRawPosition(stripped, strippedPos);
|
|
236
|
+
while (end > 0 && /\s/.test(css[end - 1])) end--;
|
|
237
|
+
const start = commentStartByEnd.get(end);
|
|
238
|
+
if (start === void 0 || !css.startsWith("/**", start) || end - start <= 4) return void 0;
|
|
239
|
+
return parseDocComment(css.slice(start + 3, end - 2));
|
|
240
|
+
};
|
|
241
|
+
}
|
|
206
242
|
function parseThemedDeclarations(css, selectors, themes) {
|
|
207
|
-
const
|
|
243
|
+
const strippedCss = stripCommentsWithMap(css);
|
|
244
|
+
const stripped = strippedCss.text;
|
|
245
|
+
const docAt = makeDocReader(css, strippedCss);
|
|
246
|
+
const themeDocs = /* @__PURE__ */ new Map();
|
|
208
247
|
const themeValues = /* @__PURE__ */ new Map();
|
|
209
248
|
const skip = [];
|
|
210
249
|
for (const [themeName, themeSelector] of Object.entries(themes)) {
|
|
211
250
|
const values = /* @__PURE__ */ new Map();
|
|
212
251
|
themeValues.set(themeName, values);
|
|
213
|
-
const collect = (block) => forEachProperty(block, (name, value) =>
|
|
252
|
+
const collect = (block, offset) => forEachProperty(block, (name, value, position) => {
|
|
253
|
+
values.set(name, value);
|
|
254
|
+
const doc = docAt(position);
|
|
255
|
+
if (doc) themeDocs.set(name, doc);
|
|
256
|
+
}, offset);
|
|
214
257
|
const selector = themeSelector.trim();
|
|
215
258
|
if (/^@media\b/i.test(selector)) {
|
|
216
259
|
forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
|
|
217
260
|
skip.push(range);
|
|
218
|
-
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, collect);
|
|
261
|
+
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
|
|
219
262
|
});
|
|
220
263
|
} else {
|
|
221
264
|
forEachSelectorBlock(stripped, [selector], (block, range) => {
|
|
222
265
|
skip.push(range);
|
|
223
|
-
collect(block);
|
|
266
|
+
collect(block, range[0]);
|
|
224
267
|
});
|
|
225
268
|
}
|
|
226
269
|
}
|
|
227
270
|
const declarations = /* @__PURE__ */ new Map();
|
|
228
|
-
|
|
229
|
-
|
|
271
|
+
const docs = /* @__PURE__ */ new Map();
|
|
272
|
+
scanDeclarations(css, selectors, (name, value, position) => {
|
|
273
|
+
declarations.set(name, value);
|
|
274
|
+
const doc = docAt(position);
|
|
275
|
+
if (doc) docs.set(name, doc);
|
|
276
|
+
}, skip, stripped);
|
|
277
|
+
for (const [name, doc] of themeDocs) {
|
|
278
|
+
if (!docs.has(name)) docs.set(name, doc);
|
|
279
|
+
}
|
|
280
|
+
return { declarations, themes: themeValues, docs };
|
|
230
281
|
}
|
|
231
282
|
var PROPERTY_DESCRIPTORS = [
|
|
232
283
|
["syntax", "syntax"],
|
|
@@ -257,6 +308,14 @@ function parsePropertyRules(css) {
|
|
|
257
308
|
}
|
|
258
309
|
return rules;
|
|
259
310
|
}
|
|
311
|
+
function toRawPosition(stripped, strippedPos) {
|
|
312
|
+
let rawPos = strippedPos;
|
|
313
|
+
for (const { at, length } of stripped.removed) {
|
|
314
|
+
if (at > strippedPos) break;
|
|
315
|
+
rawPos += length;
|
|
316
|
+
}
|
|
317
|
+
return rawPos;
|
|
318
|
+
}
|
|
260
319
|
|
|
261
320
|
// src/scanner.ts
|
|
262
321
|
function normalizePatterns(patterns) {
|
|
@@ -288,15 +347,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
288
347
|
const declarations = /* @__PURE__ */ new Map();
|
|
289
348
|
const properties = /* @__PURE__ */ new Map();
|
|
290
349
|
const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
350
|
+
const docs = /* @__PURE__ */ new Map();
|
|
291
351
|
for (const result of perFile) {
|
|
292
352
|
if (!result) continue;
|
|
293
353
|
for (const [name, value] of result.declarations) declarations.set(name, value);
|
|
294
354
|
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
355
|
+
for (const [name, doc] of result.docs) docs.set(name, doc);
|
|
295
356
|
for (const [theme, values] of result.themes) {
|
|
296
357
|
for (const [name, value] of values) themeValues.get(theme).set(name, value);
|
|
297
358
|
}
|
|
298
359
|
}
|
|
299
|
-
return { declarations, properties, themes: themeValues };
|
|
360
|
+
return { declarations, properties, themes: themeValues, docs };
|
|
300
361
|
}
|
|
301
362
|
var ROOT_RE = /\bcssVars\b/g;
|
|
302
363
|
var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
|
|
@@ -428,7 +489,8 @@ function entryMeta(name, options) {
|
|
|
428
489
|
name,
|
|
429
490
|
value: options.declarations?.get(name),
|
|
430
491
|
syntax: options.properties?.get(name)?.syntax,
|
|
431
|
-
...themes.length > 0 ? { themes } : {}
|
|
492
|
+
...themes.length > 0 ? { themes } : {},
|
|
493
|
+
...options.docs?.has(name) ? { doc: options.docs.get(name) } : {}
|
|
432
494
|
};
|
|
433
495
|
}
|
|
434
496
|
function buildEntries(varNames, options) {
|
|
@@ -468,8 +530,12 @@ function buildGroupedEntries(varNames, options) {
|
|
|
468
530
|
const ungrouped = [...ungroupedMap].map(([key, entry]) => ({ key, ...entry }));
|
|
469
531
|
return { groups, ungrouped };
|
|
470
532
|
}
|
|
471
|
-
|
|
472
|
-
|
|
533
|
+
var escapeComment = (text) => text.replace(/\*\//g, "*\\/");
|
|
534
|
+
function renderJsDoc(tags, indent) {
|
|
535
|
+
if (tags.length === 0) return [];
|
|
536
|
+
if (tags.length === 1 && !tags[0].includes("\n")) return [`${indent}/** ${escapeComment(tags[0])} */`];
|
|
537
|
+
const body = tags.flatMap((tag) => escapeComment(tag).split("\n")).map((line) => `${indent} * ${line}`.trimEnd());
|
|
538
|
+
return [`${indent}/**`, ...body, `${indent} */`];
|
|
473
539
|
}
|
|
474
540
|
function unquoteCssString(value) {
|
|
475
541
|
const quote = value[0];
|
|
@@ -478,11 +544,15 @@ function unquoteCssString(value) {
|
|
|
478
544
|
}
|
|
479
545
|
return value;
|
|
480
546
|
}
|
|
481
|
-
function
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
547
|
+
function entryTags({ value, syntax, themes, doc }) {
|
|
548
|
+
const deprecated = doc?.deprecated;
|
|
549
|
+
return [
|
|
550
|
+
...doc?.description ? [doc.description] : [],
|
|
551
|
+
...value ? [`@default ${value}`] : [],
|
|
552
|
+
...(themes ?? []).map(([theme, themeValue]) => `@theme ${theme} ${themeValue}`),
|
|
553
|
+
...syntax ? [`@syntax ${unquoteCssString(syntax)}`] : [],
|
|
554
|
+
...deprecated ? [deprecated === true ? "@deprecated" : `@deprecated ${deprecated}`] : []
|
|
555
|
+
];
|
|
486
556
|
}
|
|
487
557
|
var ENUM_SYNTAX_RE = /^[\w-]+(?:\s*\|\s*[\w-]+)+$/;
|
|
488
558
|
function parseEnumSyntax(syntax) {
|
|
@@ -514,11 +584,9 @@ function buildTailBlocks(varNames, group, properties) {
|
|
|
514
584
|
return blocks;
|
|
515
585
|
}
|
|
516
586
|
function renderEntryLines(entries, indent, lineFor) {
|
|
517
|
-
return entries.flatMap((
|
|
518
|
-
...
|
|
519
|
-
|
|
520
|
-
...syntax ? [formatSyntaxComment(syntax, indent)] : [],
|
|
521
|
-
`${indent}${lineFor(key, name)}`
|
|
587
|
+
return entries.flatMap((entry) => [
|
|
588
|
+
...renderJsDoc(entryTags(entry), indent),
|
|
589
|
+
`${indent}${lineFor(entry.key, entry.name)}`
|
|
522
590
|
]);
|
|
523
591
|
}
|
|
524
592
|
function renderGroupedBlock(varNames, options, indent, closeSuffix, lineFor) {
|
|
@@ -565,11 +633,17 @@ var HELPERS_JS = [
|
|
|
565
633
|
"",
|
|
566
634
|
"export function setCssVar(ref, value, el = document.documentElement) {",
|
|
567
635
|
" el.style.setProperty(ref.slice(4, -1), value);",
|
|
636
|
+
"}",
|
|
637
|
+
"",
|
|
638
|
+
"export function withFallback(ref, fallback) {",
|
|
639
|
+
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
568
640
|
"}"
|
|
569
641
|
];
|
|
570
642
|
var HELPERS_DTS = [
|
|
571
643
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
572
|
-
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;"
|
|
644
|
+
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
645
|
+
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
646
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
573
647
|
];
|
|
574
648
|
function generateJs(varNames, ...args) {
|
|
575
649
|
const options = normalizeOptions(args);
|
|
@@ -623,6 +697,8 @@ function generateTokens(varNames, options = {}) {
|
|
|
623
697
|
const token = { $value: resolve2(value) };
|
|
624
698
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
625
699
|
if (type) token.$type = type;
|
|
700
|
+
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
701
|
+
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
626
702
|
if (meta.themes) {
|
|
627
703
|
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
|
|
628
704
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
@@ -685,14 +761,15 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
685
761
|
}
|
|
686
762
|
return used;
|
|
687
763
|
};
|
|
688
|
-
const generatorOptions = ({ declarations, properties, themes }) => ({
|
|
764
|
+
const generatorOptions = ({ declarations, properties, themes, docs }) => ({
|
|
689
765
|
prefix: options.prefix,
|
|
690
766
|
naming: options.naming,
|
|
691
767
|
declarations,
|
|
692
768
|
group: options.group,
|
|
693
769
|
properties,
|
|
694
770
|
helpers: options.helpers,
|
|
695
|
-
themes
|
|
771
|
+
themes,
|
|
772
|
+
docs
|
|
696
773
|
});
|
|
697
774
|
const resolveNames = async (scanned) => {
|
|
698
775
|
const names = collectVarNames(scanned);
|
package/dist/unplugin.d.cts
CHANGED
package/dist/unplugin.d.ts
CHANGED