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/vite.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/vite.d.cts
CHANGED
package/dist/vite.d.ts
CHANGED
package/dist/vite.js
CHANGED
package/dist/webpack.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/webpack.d.cts
CHANGED
package/dist/webpack.d.ts
CHANGED
package/dist/webpack.js
CHANGED