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/webpack.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);
@@ -1,6 +1,6 @@
1
1
  import { Options } from './unplugin.cjs';
2
2
  import 'unplugin';
3
- import './generator-DJ9uOYBP.cjs';
3
+ import './generator-BokM5zux.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-DJ9uOYBP.js';
3
+ import './generator-BokM5zux.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-XWMRTHOE.js";
4
- import "./chunk-RZLASYCF.js";
3
+ } from "./chunk-3QCGO2EN.js";
4
+ import "./chunk-L2T4LBKN.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.12.0",
3
+ "version": "0.13.1",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
package/schema.json CHANGED
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "helpers": {
61
61
  "type": "boolean",
62
- "description": "Also emit the CssVarRef type and the getCssVar/setCssVar/withFallback helpers."
62
+ "description": "Also emit the CssVarRef type and the getCssVar/setCssVar/withFallback/varName helpers."
63
63
  },
64
64
  "prune": {
65
65
  "type": "boolean",