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/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 stripped = stripComments(css);
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) => values.set(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
- scanDeclarations(css, selectors, (name, value) => declarations.set(name, value), skip, stripped);
231
- return { declarations, themes: themeValues };
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
- function formatDefaultComment(value, indent = " ") {
474
- return `${indent}/** @default ${value.replace(/\*\//g, "*\\/")} */`;
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 formatSyntaxComment(syntax, indent = " ") {
484
- return `${indent}/** @syntax ${unquoteCssString(syntax).replace(/\*\//g, "*\\/")} */`;
485
- }
486
- function formatThemeComment(theme, value, indent = " ") {
487
- return `${indent}/** @theme ${theme} ${value.replace(/\*\//g, "*\\/")} */`;
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(({ key, name, value, syntax, themes }) => [
520
- ...value ? [formatDefaultComment(value, indent)] : [],
521
- ...(themes ?? []).map(([theme, themeValue]) => formatThemeComment(theme, themeValue, indent)),
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
@@ -1,7 +1,7 @@
1
1
  import * as vite from 'vite';
2
2
  import { Options } from './unplugin.cjs';
3
3
  import 'unplugin';
4
- import './generator-C1iVNsry.cjs';
4
+ import './generator-DJ9uOYBP.cjs';
5
5
 
6
6
  declare const _default: (options: Options) => vite.Plugin<any> | vite.Plugin<any>[];
7
7
 
package/dist/vite.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as vite from 'vite';
2
2
  import { Options } from './unplugin.js';
3
3
  import 'unplugin';
4
- import './generator-C1iVNsry.js';
4
+ import './generator-DJ9uOYBP.js';
5
5
 
6
6
  declare const _default: (options: Options) => vite.Plugin<any> | vite.Plugin<any>[];
7
7
 
package/dist/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-HBUSLYYB.js";
4
- import "./chunk-6YWE4A6S.js";
3
+ } from "./chunk-32V3V6WT.js";
4
+ import "./chunk-5HPTHW7S.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;
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 stripped = stripComments(css);
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) => values.set(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
- scanDeclarations(css, selectors, (name, value) => declarations.set(name, value), skip, stripped);
231
- return { declarations, themes: themeValues };
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
- function formatDefaultComment(value, indent = " ") {
474
- return `${indent}/** @default ${value.replace(/\*\//g, "*\\/")} */`;
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 formatSyntaxComment(syntax, indent = " ") {
484
- return `${indent}/** @syntax ${unquoteCssString(syntax).replace(/\*\//g, "*\\/")} */`;
485
- }
486
- function formatThemeComment(theme, value, indent = " ") {
487
- return `${indent}/** @theme ${theme} ${value.replace(/\*\//g, "*\\/")} */`;
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(({ key, name, value, syntax, themes }) => [
520
- ...value ? [formatDefaultComment(value, indent)] : [],
521
- ...(themes ?? []).map(([theme, themeValue]) => formatThemeComment(theme, themeValue, indent)),
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);
@@ -1,6 +1,6 @@
1
1
  import { Options } from './unplugin.cjs';
2
2
  import 'unplugin';
3
- import './generator-C1iVNsry.cjs';
3
+ import './generator-DJ9uOYBP.cjs';
4
4
 
5
5
  declare const _default: (options: Options) => WebpackPluginInstance;
6
6
 
package/dist/webpack.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Options } from './unplugin.js';
2
2
  import 'unplugin';
3
- import './generator-C1iVNsry.js';
3
+ import './generator-DJ9uOYBP.js';
4
4
 
5
5
  declare const _default: (options: Options) => WebpackPluginInstance;
6
6
 
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-HBUSLYYB.js";
4
- import "./chunk-6YWE4A6S.js";
3
+ } from "./chunk-32V3V6WT.js";
4
+ import "./chunk-5HPTHW7S.js";
5
5
 
6
6
  // src/webpack.ts
7
7
  var webpack_default = unplugin_default.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-typed-vars",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",