css-typed-vars 0.12.0 → 0.13.1

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/index.cjs CHANGED
@@ -177,9 +177,11 @@ function maskQuotedContent(text) {
177
177
  }
178
178
  return result;
179
179
  }
180
+ var CLOSERS = { "(": ")", "[": "]", "{": "}" };
180
181
  function extractValue(text, start) {
181
182
  let i = start;
182
183
  let quote = null;
184
+ const open = [];
183
185
  for (; i < text.length; i++) {
184
186
  const char = text[i];
185
187
  if (quote) {
@@ -192,16 +194,20 @@ function extractValue(text, start) {
192
194
  quote = char;
193
195
  continue;
194
196
  }
195
- if (char === ";" || char === "}") break;
197
+ if (char in CLOSERS) open.push(CLOSERS[char]);
198
+ else if (char === open[open.length - 1]) open.pop();
199
+ else if (char === "}" || char === ";" && open.length === 0) break;
196
200
  }
197
201
  return text.slice(start, i).trim();
198
202
  }
203
+ var IMPORTANT = /\s*!\s*important$/i;
204
+ var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
199
205
  function forEachProperty(block, onProperty, offset = 0) {
200
206
  const masked = maskQuotedContent(block);
201
- for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
207
+ for (const prop of masked.matchAll(DECLARATION_NAME)) {
202
208
  const name = prop[0];
203
209
  const colonIndex = block.indexOf(":", prop.index + name.length);
204
- onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
210
+ onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
205
211
  }
206
212
  }
207
213
  function forEachSelectorBlock(stripped, selectors, onBlock) {
@@ -357,13 +363,14 @@ function toRawPosition(stripped, strippedPos) {
357
363
  }
358
364
  return rawPos;
359
365
  }
366
+ var CSS_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(?:(,)|\))/gi;
360
367
  function parseVarReferences(css) {
361
368
  const stripped = stripCommentsWithMap(css);
362
369
  const masked = maskQuotedContent(stripped.text);
363
370
  const lineStarts = [0];
364
371
  for (let i = 0; i < css.length; i++) if (css[i] === "\n") lineStarts.push(i + 1);
365
372
  const refs = [];
366
- for (const match of masked.matchAll(/\bvar\(\s*(--[\w-]+)\s*(,)?/gi)) {
373
+ for (const match of masked.matchAll(CSS_REF_RE)) {
367
374
  const rawPos = toRawPosition(stripped, match.index);
368
375
  let line = lineStarts.length - 1;
369
376
  while (lineStarts[line] > rawPos) line--;
@@ -371,7 +378,7 @@ function parseVarReferences(css) {
371
378
  }
372
379
  return refs;
373
380
  }
374
- var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(,)?|(?<=[\w-]-)\((?:[\w-]+:)?(--[\w-]+)\)/g;
381
+ var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(?:(,)|\))|(?<=[\w-]-)\((?:[\w-]+:)?(--[\w-]+)\)/g;
375
382
  function parseSourceVarReferences(text) {
376
383
  const lineStarts = [0];
377
384
  for (let i = 0; i < text.length; i++) if (text[i] === "\n") lineStarts.push(i + 1);
@@ -397,7 +404,7 @@ function parseSourceDeclaredNames(text) {
397
404
  function parseDeclaredNames(css) {
398
405
  const masked = maskQuotedContent(stripComments(css));
399
406
  const names = /* @__PURE__ */ new Set();
400
- for (const match of masked.matchAll(/(?<![\w-])(--[\w-]+)\s*:/g)) names.add(match[1]);
407
+ for (const match of masked.matchAll(/(?<![\w&-])(--[\w-]+)\s*:/g)) names.add(match[1]);
401
408
  for (const match of masked.matchAll(/@property\s+(--[\w-]+)/g)) names.add(match[1]);
402
409
  return names;
403
410
  }
@@ -494,19 +501,46 @@ function rootPattern(text) {
494
501
  const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
495
502
  return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
496
503
  }
497
- var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
504
+ var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
498
505
  function readAccess(text, from) {
499
506
  ACCESS_RE.lastIndex = from;
500
507
  const match = ACCESS_RE.exec(text);
501
508
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
502
509
  }
510
+ var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
511
+ function readPatternKeys(text, at) {
512
+ let i = at - 1;
513
+ while (i >= 0 && /\s/.test(text[i])) i--;
514
+ if (text[i] !== "=") return [];
515
+ i--;
516
+ while (i >= 0 && /\s/.test(text[i])) i--;
517
+ if (text[i] !== "}") return [];
518
+ const open = text.lastIndexOf("{", i);
519
+ if (open === -1) return [];
520
+ const body = text.slice(open + 1, i);
521
+ if (body.includes("}")) return [];
522
+ const keys = [];
523
+ for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
524
+ const match = PATTERN_KEY_RE.exec(part);
525
+ if (match) keys.push(match[1] ?? match[3]);
526
+ }
527
+ return keys;
528
+ }
503
529
  function extractUsage(text, into) {
504
530
  for (const root of text.matchAll(rootPattern(text))) {
505
531
  const first = readAccess(text, root.index + root[0].length);
506
- if (!first) continue;
532
+ const second = first && readAccess(text, first.end);
533
+ const destructured = second ? [] : readPatternKeys(text, root.index);
534
+ if (!first) {
535
+ for (const key of destructured) {
536
+ into.keys.add(key);
537
+ into.wholeKeys.add(key);
538
+ }
539
+ continue;
540
+ }
507
541
  into.keys.add(first.key);
508
- const second = readAccess(text, first.end);
509
542
  if (second) into.paths.add(`${first.key}\0${second.key}`);
543
+ else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
510
544
  else into.wholeKeys.add(first.key);
511
545
  }
512
546
  }
@@ -797,6 +831,12 @@ var HELPERS_TS = [
797
831
  "",
798
832
  "export function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F> {",
799
833
  " return `${ref.slice(0, -1)}, ${fallback})` as CssVarWithFallback<R, F>;",
834
+ "}",
835
+ "",
836
+ "export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
837
+ "",
838
+ "export function varName<R extends CssVarRef>(ref: R): CssVarProperty<R> {",
839
+ " return ref.slice(4, -1) as CssVarProperty<R>;",
800
840
  "}"
801
841
  ];
802
842
  var HELPERS_JS = [
@@ -810,13 +850,19 @@ var HELPERS_JS = [
810
850
  "",
811
851
  "export function withFallback(ref, fallback) {",
812
852
  " return `${ref.slice(0, -1)}, ${fallback})`;",
853
+ "}",
854
+ "",
855
+ "export function varName(ref) {",
856
+ " return ref.slice(4, -1);",
813
857
  "}"
814
858
  ];
815
859
  var HELPERS_DTS = [
816
860
  "export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
817
861
  "export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
818
862
  "export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
819
- "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
863
+ "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
864
+ "export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
865
+ "export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
820
866
  ];
821
867
  function generateCode(varNames, ...args) {
822
868
  const options = normalizeOptions(args);
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
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';
1
+ import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-BokM5zux.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-BokM5zux.cjs';
3
3
 
4
4
  interface ScannedCss {
5
5
  declarations: Map<string, string>;
@@ -43,7 +43,7 @@ interface GenerateOptions {
43
43
  /** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true, or when calling `findUnusedVars`. */
44
44
  usage?: string | string[];
45
45
  usageExclude?: string | string[];
46
- /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers. */
46
+ /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
47
47
  helpers?: boolean;
48
48
  /** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
49
49
  refsIgnore?: string[];
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
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';
1
+ import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-BokM5zux.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-BokM5zux.js';
3
3
 
4
4
  interface ScannedCss {
5
5
  declarations: Map<string, string>;
@@ -43,7 +43,7 @@ interface GenerateOptions {
43
43
  /** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true, or when calling `findUnusedVars`. */
44
44
  usage?: string | string[];
45
45
  usageExclude?: string | string[];
46
- /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers. */
46
+ /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
47
47
  helpers?: boolean;
48
48
  /** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
49
49
  refsIgnore?: string[];
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  findUndefinedRefs,
5
5
  findUnusedVars,
6
6
  generate
7
- } from "./chunk-ULIGESN5.js";
7
+ } from "./chunk-DIGUOQES.js";
8
8
  import {
9
9
  generateCode,
10
10
  generateDeclaration,
@@ -21,7 +21,7 @@ import {
21
21
  scanUsedKeys,
22
22
  scanVarDeclarations,
23
23
  scanVarNames
24
- } from "./chunk-RZLASYCF.js";
24
+ } from "./chunk-L2T4LBKN.js";
25
25
  export {
26
26
  checkGenerated,
27
27
  defineConfig,
package/dist/rollup.cjs CHANGED
@@ -162,9 +162,11 @@ function maskQuotedContent(text) {
162
162
  }
163
163
  return result;
164
164
  }
165
+ var CLOSERS = { "(": ")", "[": "]", "{": "}" };
165
166
  function extractValue(text, start) {
166
167
  let i = start;
167
168
  let quote = null;
169
+ const open = [];
168
170
  for (; i < text.length; i++) {
169
171
  const char = text[i];
170
172
  if (quote) {
@@ -177,16 +179,20 @@ function extractValue(text, start) {
177
179
  quote = char;
178
180
  continue;
179
181
  }
180
- if (char === ";" || char === "}") break;
182
+ if (char in CLOSERS) open.push(CLOSERS[char]);
183
+ else if (char === open[open.length - 1]) open.pop();
184
+ else if (char === "}" || char === ";" && open.length === 0) break;
181
185
  }
182
186
  return text.slice(start, i).trim();
183
187
  }
188
+ var IMPORTANT = /\s*!\s*important$/i;
189
+ var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
184
190
  function forEachProperty(block, onProperty, offset = 0) {
185
191
  const masked = maskQuotedContent(block);
186
- for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
192
+ for (const prop of masked.matchAll(DECLARATION_NAME)) {
187
193
  const name = prop[0];
188
194
  const colonIndex = block.indexOf(":", prop.index + name.length);
189
- onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
195
+ onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
190
196
  }
191
197
  }
192
198
  function forEachSelectorBlock(stripped, selectors, onBlock) {
@@ -444,19 +450,46 @@ function rootPattern(text) {
444
450
  const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
445
451
  return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
446
452
  }
447
- var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
453
+ var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
448
454
  function readAccess(text, from) {
449
455
  ACCESS_RE.lastIndex = from;
450
456
  const match = ACCESS_RE.exec(text);
451
457
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
452
458
  }
459
+ var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
460
+ function readPatternKeys(text, at) {
461
+ let i = at - 1;
462
+ while (i >= 0 && /\s/.test(text[i])) i--;
463
+ if (text[i] !== "=") return [];
464
+ i--;
465
+ while (i >= 0 && /\s/.test(text[i])) i--;
466
+ if (text[i] !== "}") return [];
467
+ const open = text.lastIndexOf("{", i);
468
+ if (open === -1) return [];
469
+ const body = text.slice(open + 1, i);
470
+ if (body.includes("}")) return [];
471
+ const keys = [];
472
+ for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
473
+ const match = PATTERN_KEY_RE.exec(part);
474
+ if (match) keys.push(match[1] ?? match[3]);
475
+ }
476
+ return keys;
477
+ }
453
478
  function extractUsage(text, into) {
454
479
  for (const root of text.matchAll(rootPattern(text))) {
455
480
  const first = readAccess(text, root.index + root[0].length);
456
- if (!first) continue;
481
+ const second = first && readAccess(text, first.end);
482
+ const destructured = second ? [] : readPatternKeys(text, root.index);
483
+ if (!first) {
484
+ for (const key of destructured) {
485
+ into.keys.add(key);
486
+ into.wholeKeys.add(key);
487
+ }
488
+ continue;
489
+ }
457
490
  into.keys.add(first.key);
458
- const second = readAccess(text, first.end);
459
491
  if (second) into.paths.add(`${first.key}\0${second.key}`);
492
+ else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
460
493
  else into.wholeKeys.add(first.key);
461
494
  }
462
495
  }
@@ -719,13 +752,19 @@ var HELPERS_JS = [
719
752
  "",
720
753
  "export function withFallback(ref, fallback) {",
721
754
  " return `${ref.slice(0, -1)}, ${fallback})`;",
755
+ "}",
756
+ "",
757
+ "export function varName(ref) {",
758
+ " return ref.slice(4, -1);",
722
759
  "}"
723
760
  ];
724
761
  var HELPERS_DTS = [
725
762
  "export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
726
763
  "export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
727
764
  "export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
728
- "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
765
+ "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
766
+ "export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
767
+ "export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
729
768
  ];
730
769
  function generateJs(varNames, ...args) {
731
770
  const options = normalizeOptions(args);
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-DJ9uOYBP.cjs';
4
+ import './generator-BokM5zux.cjs';
5
5
 
6
6
  declare const _default: (options: Options) => rollup.Plugin<any> | rollup.Plugin<any>[];
7
7
 
package/dist/rollup.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as rollup from 'rollup';
2
2
  import { Options } from './unplugin.js';
3
3
  import 'unplugin';
4
- import './generator-DJ9uOYBP.js';
4
+ import './generator-BokM5zux.js';
5
5
 
6
6
  declare const _default: (options: Options) => rollup.Plugin<any> | rollup.Plugin<any>[];
7
7
 
package/dist/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-XWMRTHOE.js";
4
- import "./chunk-RZLASYCF.js";
3
+ } from "./chunk-3QCGO2EN.js";
4
+ import "./chunk-L2T4LBKN.js";
5
5
 
6
6
  // src/rollup.ts
7
7
  var rollup_default = unplugin_default.rollup;
package/dist/unplugin.cjs CHANGED
@@ -160,9 +160,11 @@ function maskQuotedContent(text) {
160
160
  }
161
161
  return result;
162
162
  }
163
+ var CLOSERS = { "(": ")", "[": "]", "{": "}" };
163
164
  function extractValue(text, start) {
164
165
  let i = start;
165
166
  let quote = null;
167
+ const open = [];
166
168
  for (; i < text.length; i++) {
167
169
  const char = text[i];
168
170
  if (quote) {
@@ -175,16 +177,20 @@ function extractValue(text, start) {
175
177
  quote = char;
176
178
  continue;
177
179
  }
178
- if (char === ";" || char === "}") break;
180
+ if (char in CLOSERS) open.push(CLOSERS[char]);
181
+ else if (char === open[open.length - 1]) open.pop();
182
+ else if (char === "}" || char === ";" && open.length === 0) break;
179
183
  }
180
184
  return text.slice(start, i).trim();
181
185
  }
186
+ var IMPORTANT = /\s*!\s*important$/i;
187
+ var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
182
188
  function forEachProperty(block, onProperty, offset = 0) {
183
189
  const masked = maskQuotedContent(block);
184
- for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
190
+ for (const prop of masked.matchAll(DECLARATION_NAME)) {
185
191
  const name = prop[0];
186
192
  const colonIndex = block.indexOf(":", prop.index + name.length);
187
- onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
193
+ onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
188
194
  }
189
195
  }
190
196
  function forEachSelectorBlock(stripped, selectors, onBlock) {
@@ -442,19 +448,46 @@ function rootPattern(text) {
442
448
  const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
443
449
  return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
444
450
  }
445
- var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
451
+ var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
446
452
  function readAccess(text, from) {
447
453
  ACCESS_RE.lastIndex = from;
448
454
  const match = ACCESS_RE.exec(text);
449
455
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
450
456
  }
457
+ var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
458
+ function readPatternKeys(text, at) {
459
+ let i = at - 1;
460
+ while (i >= 0 && /\s/.test(text[i])) i--;
461
+ if (text[i] !== "=") return [];
462
+ i--;
463
+ while (i >= 0 && /\s/.test(text[i])) i--;
464
+ if (text[i] !== "}") return [];
465
+ const open = text.lastIndexOf("{", i);
466
+ if (open === -1) return [];
467
+ const body = text.slice(open + 1, i);
468
+ if (body.includes("}")) return [];
469
+ const keys = [];
470
+ for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
471
+ const match = PATTERN_KEY_RE.exec(part);
472
+ if (match) keys.push(match[1] ?? match[3]);
473
+ }
474
+ return keys;
475
+ }
451
476
  function extractUsage(text, into) {
452
477
  for (const root of text.matchAll(rootPattern(text))) {
453
478
  const first = readAccess(text, root.index + root[0].length);
454
- if (!first) continue;
479
+ const second = first && readAccess(text, first.end);
480
+ const destructured = second ? [] : readPatternKeys(text, root.index);
481
+ if (!first) {
482
+ for (const key of destructured) {
483
+ into.keys.add(key);
484
+ into.wholeKeys.add(key);
485
+ }
486
+ continue;
487
+ }
455
488
  into.keys.add(first.key);
456
- const second = readAccess(text, first.end);
457
489
  if (second) into.paths.add(`${first.key}\0${second.key}`);
490
+ else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
458
491
  else into.wholeKeys.add(first.key);
459
492
  }
460
493
  }
@@ -717,13 +750,19 @@ var HELPERS_JS = [
717
750
  "",
718
751
  "export function withFallback(ref, fallback) {",
719
752
  " return `${ref.slice(0, -1)}, ${fallback})`;",
753
+ "}",
754
+ "",
755
+ "export function varName(ref) {",
756
+ " return ref.slice(4, -1);",
720
757
  "}"
721
758
  ];
722
759
  var HELPERS_DTS = [
723
760
  "export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
724
761
  "export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
725
762
  "export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
726
- "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
763
+ "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
764
+ "export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
765
+ "export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
727
766
  ];
728
767
  function generateJs(varNames, ...args) {
729
768
  const options = normalizeOptions(args);
@@ -1,5 +1,5 @@
1
1
  import * as unplugin from 'unplugin';
2
- import { N as NamingConvention } from './generator-DJ9uOYBP.cjs';
2
+ import { N as NamingConvention } from './generator-BokM5zux.cjs';
3
3
 
4
4
  interface Options {
5
5
  input: string | string[];
@@ -28,7 +28,7 @@ interface Options {
28
28
  /** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true. */
29
29
  usage?: string | string[];
30
30
  usageExclude?: string | string[];
31
- /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers. */
31
+ /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
32
32
  helpers?: boolean;
33
33
  }
34
34
  declare const _default: unplugin.UnpluginInstance<Options, boolean>;
@@ -1,5 +1,5 @@
1
1
  import * as unplugin from 'unplugin';
2
- import { N as NamingConvention } from './generator-DJ9uOYBP.js';
2
+ import { N as NamingConvention } from './generator-BokM5zux.js';
3
3
 
4
4
  interface Options {
5
5
  input: string | string[];
@@ -28,7 +28,7 @@ interface Options {
28
28
  /** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true. */
29
29
  usage?: string | string[];
30
30
  usageExclude?: string | string[];
31
- /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers. */
31
+ /** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
32
32
  helpers?: boolean;
33
33
  }
34
34
  declare const _default: unplugin.UnpluginInstance<Options, boolean>;
package/dist/unplugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-XWMRTHOE.js";
4
- import "./chunk-RZLASYCF.js";
3
+ } from "./chunk-3QCGO2EN.js";
4
+ import "./chunk-L2T4LBKN.js";
5
5
  export {
6
6
  unplugin_default as default
7
7
  };
package/dist/vite.cjs CHANGED
@@ -162,9 +162,11 @@ function maskQuotedContent(text) {
162
162
  }
163
163
  return result;
164
164
  }
165
+ var CLOSERS = { "(": ")", "[": "]", "{": "}" };
165
166
  function extractValue(text, start) {
166
167
  let i = start;
167
168
  let quote = null;
169
+ const open = [];
168
170
  for (; i < text.length; i++) {
169
171
  const char = text[i];
170
172
  if (quote) {
@@ -177,16 +179,20 @@ function extractValue(text, start) {
177
179
  quote = char;
178
180
  continue;
179
181
  }
180
- if (char === ";" || char === "}") break;
182
+ if (char in CLOSERS) open.push(CLOSERS[char]);
183
+ else if (char === open[open.length - 1]) open.pop();
184
+ else if (char === "}" || char === ";" && open.length === 0) break;
181
185
  }
182
186
  return text.slice(start, i).trim();
183
187
  }
188
+ var IMPORTANT = /\s*!\s*important$/i;
189
+ var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
184
190
  function forEachProperty(block, onProperty, offset = 0) {
185
191
  const masked = maskQuotedContent(block);
186
- for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
192
+ for (const prop of masked.matchAll(DECLARATION_NAME)) {
187
193
  const name = prop[0];
188
194
  const colonIndex = block.indexOf(":", prop.index + name.length);
189
- onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
195
+ onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
190
196
  }
191
197
  }
192
198
  function forEachSelectorBlock(stripped, selectors, onBlock) {
@@ -444,19 +450,46 @@ function rootPattern(text) {
444
450
  const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
445
451
  return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
446
452
  }
447
- var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
453
+ var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
448
454
  function readAccess(text, from) {
449
455
  ACCESS_RE.lastIndex = from;
450
456
  const match = ACCESS_RE.exec(text);
451
457
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
452
458
  }
459
+ var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
460
+ function readPatternKeys(text, at) {
461
+ let i = at - 1;
462
+ while (i >= 0 && /\s/.test(text[i])) i--;
463
+ if (text[i] !== "=") return [];
464
+ i--;
465
+ while (i >= 0 && /\s/.test(text[i])) i--;
466
+ if (text[i] !== "}") return [];
467
+ const open = text.lastIndexOf("{", i);
468
+ if (open === -1) return [];
469
+ const body = text.slice(open + 1, i);
470
+ if (body.includes("}")) return [];
471
+ const keys = [];
472
+ for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
473
+ const match = PATTERN_KEY_RE.exec(part);
474
+ if (match) keys.push(match[1] ?? match[3]);
475
+ }
476
+ return keys;
477
+ }
453
478
  function extractUsage(text, into) {
454
479
  for (const root of text.matchAll(rootPattern(text))) {
455
480
  const first = readAccess(text, root.index + root[0].length);
456
- if (!first) continue;
481
+ const second = first && readAccess(text, first.end);
482
+ const destructured = second ? [] : readPatternKeys(text, root.index);
483
+ if (!first) {
484
+ for (const key of destructured) {
485
+ into.keys.add(key);
486
+ into.wholeKeys.add(key);
487
+ }
488
+ continue;
489
+ }
457
490
  into.keys.add(first.key);
458
- const second = readAccess(text, first.end);
459
491
  if (second) into.paths.add(`${first.key}\0${second.key}`);
492
+ else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
460
493
  else into.wholeKeys.add(first.key);
461
494
  }
462
495
  }
@@ -719,13 +752,19 @@ var HELPERS_JS = [
719
752
  "",
720
753
  "export function withFallback(ref, fallback) {",
721
754
  " return `${ref.slice(0, -1)}, ${fallback})`;",
755
+ "}",
756
+ "",
757
+ "export function varName(ref) {",
758
+ " return ref.slice(4, -1);",
722
759
  "}"
723
760
  ];
724
761
  var HELPERS_DTS = [
725
762
  "export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
726
763
  "export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
727
764
  "export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
728
- "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
765
+ "export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
766
+ "export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
767
+ "export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
729
768
  ];
730
769
  function generateJs(varNames, ...args) {
731
770
  const options = normalizeOptions(args);
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-DJ9uOYBP.cjs';
4
+ import './generator-BokM5zux.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-DJ9uOYBP.js';
4
+ import './generator-BokM5zux.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-XWMRTHOE.js";
4
- import "./chunk-RZLASYCF.js";
3
+ } from "./chunk-3QCGO2EN.js";
4
+ import "./chunk-L2T4LBKN.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;