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/README.md +18 -4
- package/dist/{chunk-XWMRTHOE.js → chunk-3QCGO2EN.js} +1 -1
- package/dist/{chunk-ULIGESN5.js → chunk-DIGUOQES.js} +1 -1
- package/dist/{chunk-RZLASYCF.js → chunk-L2T4LBKN.js} +56 -10
- package/dist/cli.cjs +57 -11
- package/dist/cli.js +3 -3
- package/dist/esbuild.cjs +46 -7
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/{generator-DJ9uOYBP.d.ts → generator-BokM5zux.d.cts} +1 -1
- package/dist/{generator-DJ9uOYBP.d.cts → generator-BokM5zux.d.ts} +1 -1
- package/dist/index.cjs +56 -10
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/rollup.cjs +46 -7
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +46 -7
- package/dist/unplugin.d.cts +2 -2
- package/dist/unplugin.d.ts +2 -2
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +46 -7
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +46 -7
- 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/schema.json +1 -1
package/README.md
CHANGED
|
@@ -139,7 +139,7 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
139
139
|
| `--selector` | Extra CSS selector to scan for variables (repeatable: `--selector ".dark" --selector "[data-theme='dark']"`) |
|
|
140
140
|
| `--theme` | Named theme block, kept out of the default value and emitted as `@theme <name> <value>` JSDoc tags (repeatable: `--theme dark='[data-theme="dark"]' --theme contrast='@media (prefers-contrast: more)'`) — see [Theme values](#theme-values) |
|
|
141
141
|
| `--group` | Nest keys under an object named after each variable's first hyphen segment: `--color-primary`/`--color-secondary` → `cssVars.color.primary`/`cssVars.color.secondary`. A variable with no hyphen stays top-level. Can't be combined with `--prefix`. |
|
|
142
|
-
| `--helpers` | Also emit the `CssVarRef` type and typed `getCssVar(ref, el?)`/`setCssVar(ref, value, el?)`/`withFallback(ref, fallback)` helpers — see [Usage](#usage). |
|
|
142
|
+
| `--helpers` | Also emit the `CssVarRef` type and typed `getCssVar(ref, el?)`/`setCssVar(ref, value, el?)`/`withFallback(ref, fallback)`/`varName(ref)` helpers — see [Usage](#usage). |
|
|
143
143
|
| `--prune` | Drop generated vars with no detected `cssVars.<key>` usage. Requires `--usage <glob>` (repeatable). With `--group`, matches `cssVars.<group>.<key>` and keeps a whole group referenced as `cssVars.<group>`. |
|
|
144
144
|
| `--prune-check` | Like `--prune`, but only reports unused vars without writing; exits 1 if any are found. Doesn't need `--output`. Useful in CI. |
|
|
145
145
|
| `--lint-refs` | Report `var(--name)` references to variables not declared anywhere in the input; exits 1 if any (without a fallback) are found — see [Linting var() references](#linting-var-references) |
|
|
@@ -154,8 +154,9 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
154
154
|
|
|
155
155
|
### Pruning unused variables
|
|
156
156
|
|
|
157
|
-
`--prune`/`--prune-check` scan your source files for `cssVars.<key>` (dot access)
|
|
157
|
+
`--prune`/`--prune-check` scan your source files for `cssVars.<key>` (dot access), `cssVars['<key>']`/`cssVars["<key>"]` (bracket access) or `const { <key> } = cssVars` (destructuring) usage — optional chaining (`cssVars?.<key>`) included — and compare that against the generated keys. This is a textual scan, not a full parser, so it has limitations worth knowing:
|
|
158
158
|
|
|
159
|
+
- Destructuring is read one level deep — `const { colorPrimary, gap: g } = cssVars`, or `const { primary } = cssVars.color` with `--group` — but not a nested pattern (`{ color: { primary } }`), a `...rest` element or a destructured function parameter.
|
|
159
160
|
- A renamed import (`import { cssVars as vars } from './cssVars'`) is followed within the file that declares it, but not a re-export under another name or an assignment (`const v = cssVars`).
|
|
160
161
|
- A match inside a comment or string still counts as "used".
|
|
161
162
|
|
|
@@ -182,6 +183,7 @@ css-typed-vars: 2 reference(s) to undeclared variables:
|
|
|
182
183
|
- A variable counts as declared if any input file declares it in any block (not just `:root`), or registers it with `@property`.
|
|
183
184
|
- References without a fallback are errors and make the command exit `1`. References with a fallback (`var(--x, 4px)`) still work at runtime, so they're only reported as warnings.
|
|
184
185
|
- References inside comments and strings are ignored; positions point to the original file.
|
|
186
|
+
- A name with an interpolated part — SCSS `var(--color-#{$name})`, Less `var(--size-@{n})` — isn't a reference to one variable, and is skipped.
|
|
185
187
|
- Variables set from JavaScript or defined by another stylesheet you don't scan can be skipped with `--refs-ignore` (repeatable, a trailing `*` matches a prefix): `--refs-ignore "--tw-*" --refs-ignore --app-height`, or `refsIgnore` in the config file.
|
|
186
188
|
|
|
187
189
|
#### In source files
|
|
@@ -198,6 +200,7 @@ css-typed-vars: 1 reference(s) to undeclared variables:
|
|
|
198
200
|
```
|
|
199
201
|
|
|
200
202
|
- Finds `var(--name)` anywhere in the file — in strings, template literals, Tailwind classes like `bg-[var(--brand)]` — and Tailwind v4's `bg-(--brand)` shorthand.
|
|
203
|
+
- A name built at runtime is skipped: `` `var(--color-${name})` ``, `'var(--size-' + n + ')'`.
|
|
201
204
|
- Variables a source file sets itself count as declared: an object key (`{ '--gap': '4px' }`), a `style` attribute (`style="--gap: 4px"`), `setProperty('--gap', ...)` or a Tailwind arbitrary property (`[--gap:4px]`). This applies to the CSS references too.
|
|
202
205
|
- Like the `--prune` usage scan, this is textual: a reference inside a comment counts too.
|
|
203
206
|
- The target's own output file is skipped even if the glob matches it — `--check` is what tells whether it's stale.
|
|
@@ -365,7 +368,7 @@ Turbopack does not yet have a public plugin API for virtual modules. Use the CLI
|
|
|
365
368
|
| `selectors` | `string[]` | — | Extra CSS selectors to scan, e.g. `['.dark', '[data-theme="dark"]']` |
|
|
366
369
|
| `themes` | `Record<string, string>` | — | Theme name → selector or `@media` prelude; values become `@theme` JSDoc tags — see [Theme values](#theme-values) |
|
|
367
370
|
| `group` | `boolean` | `false` | Nest keys by each variable's first hyphen segment, e.g. `cssVars.color.primary`. Can't be combined with `prefix` |
|
|
368
|
-
| `helpers` | `boolean` | `false` | Also export the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback` helpers from `css-typed-vars/vars` |
|
|
371
|
+
| `helpers` | `boolean` | `false` | Also export the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers from `css-typed-vars/vars` |
|
|
369
372
|
| `prune` | `boolean` | `false` | Drop vars with no detected `cssVars.<key>` (or `cssVars.<group>.<key>` with `group`) usage. Requires `usage` |
|
|
370
373
|
| `usage` | `string \| string[]` | — | Glob(s) of source files to scan for `cssVars` usage, for `prune` |
|
|
371
374
|
| `usageExclude` | `string \| string[]` | — | Glob(s) of source files to exclude from the usage scan |
|
|
@@ -435,6 +438,17 @@ withFallback(cssVars.spacingMd, 0); // 'var(--spacing-md, 0)'
|
|
|
435
438
|
withFallback(cssVars.colorLink, cssVars.colorPrimary); // 'var(--color-link, var(--color-primary))'
|
|
436
439
|
```
|
|
437
440
|
|
|
441
|
+
`varName` returns the custom property's own name, typed as the exact literal — for setting a variable from a style object (React inline styles, Vue `:style`), where you'd otherwise type the `--name` string by hand. Also a pure string helper:
|
|
442
|
+
|
|
443
|
+
```tsx
|
|
444
|
+
import { cssVars, varName } from './cssVars';
|
|
445
|
+
|
|
446
|
+
varName(cssVars.colorPrimary); // '--color-primary'
|
|
447
|
+
|
|
448
|
+
const style = { [varName(cssVars.colorPrimary)]: brandColor };
|
|
449
|
+
<div style={style} /> // sets --color-primary on this element
|
|
450
|
+
```
|
|
451
|
+
|
|
438
452
|
---
|
|
439
453
|
|
|
440
454
|
## Programmatic API
|
|
@@ -451,7 +465,7 @@ await generate({
|
|
|
451
465
|
selectors: ['.dark', '[data-theme="dark"]'],
|
|
452
466
|
themes: { dark: '[data-theme="dark"]' }, // per-theme values as @theme JSDoc tags
|
|
453
467
|
group: false, // nest keys by first hyphen segment, e.g. cssVars.color.primary
|
|
454
|
-
helpers: false, // also emit getCssVar/setCssVar/withFallback helpers
|
|
468
|
+
helpers: false, // also emit getCssVar/setCssVar/withFallback/varName helpers
|
|
455
469
|
prune: false, // drop vars with no detected cssVars.<key> usage (requires `usage`)
|
|
456
470
|
usage: 'src/**/*.{ts,tsx}',
|
|
457
471
|
});
|
|
@@ -121,9 +121,11 @@ function maskQuotedContent(text) {
|
|
|
121
121
|
}
|
|
122
122
|
return result;
|
|
123
123
|
}
|
|
124
|
+
var CLOSERS = { "(": ")", "[": "]", "{": "}" };
|
|
124
125
|
function extractValue(text, start) {
|
|
125
126
|
let i = start;
|
|
126
127
|
let quote = null;
|
|
128
|
+
const open = [];
|
|
127
129
|
for (; i < text.length; i++) {
|
|
128
130
|
const char = text[i];
|
|
129
131
|
if (quote) {
|
|
@@ -136,16 +138,20 @@ function extractValue(text, start) {
|
|
|
136
138
|
quote = char;
|
|
137
139
|
continue;
|
|
138
140
|
}
|
|
139
|
-
if (char
|
|
141
|
+
if (char in CLOSERS) open.push(CLOSERS[char]);
|
|
142
|
+
else if (char === open[open.length - 1]) open.pop();
|
|
143
|
+
else if (char === "}" || char === ";" && open.length === 0) break;
|
|
140
144
|
}
|
|
141
145
|
return text.slice(start, i).trim();
|
|
142
146
|
}
|
|
147
|
+
var IMPORTANT = /\s*!\s*important$/i;
|
|
148
|
+
var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
|
|
143
149
|
function forEachProperty(block, onProperty, offset = 0) {
|
|
144
150
|
const masked = maskQuotedContent(block);
|
|
145
|
-
for (const prop of masked.matchAll(
|
|
151
|
+
for (const prop of masked.matchAll(DECLARATION_NAME)) {
|
|
146
152
|
const name = prop[0];
|
|
147
153
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
148
|
-
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
154
|
+
onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
|
|
149
155
|
}
|
|
150
156
|
}
|
|
151
157
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -301,13 +307,14 @@ function toRawPosition(stripped, strippedPos) {
|
|
|
301
307
|
}
|
|
302
308
|
return rawPos;
|
|
303
309
|
}
|
|
310
|
+
var CSS_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(?:(,)|\))/gi;
|
|
304
311
|
function parseVarReferences(css) {
|
|
305
312
|
const stripped = stripCommentsWithMap(css);
|
|
306
313
|
const masked = maskQuotedContent(stripped.text);
|
|
307
314
|
const lineStarts = [0];
|
|
308
315
|
for (let i = 0; i < css.length; i++) if (css[i] === "\n") lineStarts.push(i + 1);
|
|
309
316
|
const refs = [];
|
|
310
|
-
for (const match of masked.matchAll(
|
|
317
|
+
for (const match of masked.matchAll(CSS_REF_RE)) {
|
|
311
318
|
const rawPos = toRawPosition(stripped, match.index);
|
|
312
319
|
let line = lineStarts.length - 1;
|
|
313
320
|
while (lineStarts[line] > rawPos) line--;
|
|
@@ -315,7 +322,7 @@ function parseVarReferences(css) {
|
|
|
315
322
|
}
|
|
316
323
|
return refs;
|
|
317
324
|
}
|
|
318
|
-
var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(,)
|
|
325
|
+
var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(?:(,)|\))|(?<=[\w-]-)\((?:[\w-]+:)?(--[\w-]+)\)/g;
|
|
319
326
|
function parseSourceVarReferences(text) {
|
|
320
327
|
const lineStarts = [0];
|
|
321
328
|
for (let i = 0; i < text.length; i++) if (text[i] === "\n") lineStarts.push(i + 1);
|
|
@@ -341,7 +348,7 @@ function parseSourceDeclaredNames(text) {
|
|
|
341
348
|
function parseDeclaredNames(css) {
|
|
342
349
|
const masked = maskQuotedContent(stripComments(css));
|
|
343
350
|
const names = /* @__PURE__ */ new Set();
|
|
344
|
-
for (const match of masked.matchAll(/(?<![\w
|
|
351
|
+
for (const match of masked.matchAll(/(?<![\w&-])(--[\w-]+)\s*:/g)) names.add(match[1]);
|
|
345
352
|
for (const match of masked.matchAll(/@property\s+(--[\w-]+)/g)) names.add(match[1]);
|
|
346
353
|
return names;
|
|
347
354
|
}
|
|
@@ -468,19 +475,46 @@ function rootPattern(text) {
|
|
|
468
475
|
const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
|
|
469
476
|
return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
|
|
470
477
|
}
|
|
471
|
-
var ACCESS_RE = /\s*(
|
|
478
|
+
var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
|
|
472
479
|
function readAccess(text, from) {
|
|
473
480
|
ACCESS_RE.lastIndex = from;
|
|
474
481
|
const match = ACCESS_RE.exec(text);
|
|
475
482
|
return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
|
|
476
483
|
}
|
|
484
|
+
var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
|
|
485
|
+
function readPatternKeys(text, at) {
|
|
486
|
+
let i = at - 1;
|
|
487
|
+
while (i >= 0 && /\s/.test(text[i])) i--;
|
|
488
|
+
if (text[i] !== "=") return [];
|
|
489
|
+
i--;
|
|
490
|
+
while (i >= 0 && /\s/.test(text[i])) i--;
|
|
491
|
+
if (text[i] !== "}") return [];
|
|
492
|
+
const open = text.lastIndexOf("{", i);
|
|
493
|
+
if (open === -1) return [];
|
|
494
|
+
const body = text.slice(open + 1, i);
|
|
495
|
+
if (body.includes("}")) return [];
|
|
496
|
+
const keys = [];
|
|
497
|
+
for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
|
|
498
|
+
const match = PATTERN_KEY_RE.exec(part);
|
|
499
|
+
if (match) keys.push(match[1] ?? match[3]);
|
|
500
|
+
}
|
|
501
|
+
return keys;
|
|
502
|
+
}
|
|
477
503
|
function extractUsage(text, into) {
|
|
478
504
|
for (const root of text.matchAll(rootPattern(text))) {
|
|
479
505
|
const first = readAccess(text, root.index + root[0].length);
|
|
480
|
-
|
|
506
|
+
const second = first && readAccess(text, first.end);
|
|
507
|
+
const destructured = second ? [] : readPatternKeys(text, root.index);
|
|
508
|
+
if (!first) {
|
|
509
|
+
for (const key of destructured) {
|
|
510
|
+
into.keys.add(key);
|
|
511
|
+
into.wholeKeys.add(key);
|
|
512
|
+
}
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
481
515
|
into.keys.add(first.key);
|
|
482
|
-
const second = readAccess(text, first.end);
|
|
483
516
|
if (second) into.paths.add(`${first.key}\0${second.key}`);
|
|
517
|
+
else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
|
|
484
518
|
else into.wholeKeys.add(first.key);
|
|
485
519
|
}
|
|
486
520
|
}
|
|
@@ -771,6 +805,12 @@ var HELPERS_TS = [
|
|
|
771
805
|
"",
|
|
772
806
|
"export function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F> {",
|
|
773
807
|
" return `${ref.slice(0, -1)}, ${fallback})` as CssVarWithFallback<R, F>;",
|
|
808
|
+
"}",
|
|
809
|
+
"",
|
|
810
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
811
|
+
"",
|
|
812
|
+
"export function varName<R extends CssVarRef>(ref: R): CssVarProperty<R> {",
|
|
813
|
+
" return ref.slice(4, -1) as CssVarProperty<R>;",
|
|
774
814
|
"}"
|
|
775
815
|
];
|
|
776
816
|
var HELPERS_JS = [
|
|
@@ -784,13 +824,19 @@ var HELPERS_JS = [
|
|
|
784
824
|
"",
|
|
785
825
|
"export function withFallback(ref, fallback) {",
|
|
786
826
|
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
827
|
+
"}",
|
|
828
|
+
"",
|
|
829
|
+
"export function varName(ref) {",
|
|
830
|
+
" return ref.slice(4, -1);",
|
|
787
831
|
"}"
|
|
788
832
|
];
|
|
789
833
|
var HELPERS_DTS = [
|
|
790
834
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
791
835
|
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
792
836
|
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
793
|
-
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
837
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
|
|
838
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
839
|
+
"export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
|
|
794
840
|
];
|
|
795
841
|
function generateCode(varNames, ...args) {
|
|
796
842
|
const options = normalizeOptions(args);
|
package/dist/cli.cjs
CHANGED
|
@@ -144,9 +144,11 @@ function maskQuotedContent(text) {
|
|
|
144
144
|
}
|
|
145
145
|
return result;
|
|
146
146
|
}
|
|
147
|
+
var CLOSERS = { "(": ")", "[": "]", "{": "}" };
|
|
147
148
|
function extractValue(text, start) {
|
|
148
149
|
let i = start;
|
|
149
150
|
let quote = null;
|
|
151
|
+
const open = [];
|
|
150
152
|
for (; i < text.length; i++) {
|
|
151
153
|
const char = text[i];
|
|
152
154
|
if (quote) {
|
|
@@ -159,16 +161,20 @@ function extractValue(text, start) {
|
|
|
159
161
|
quote = char;
|
|
160
162
|
continue;
|
|
161
163
|
}
|
|
162
|
-
if (char
|
|
164
|
+
if (char in CLOSERS) open.push(CLOSERS[char]);
|
|
165
|
+
else if (char === open[open.length - 1]) open.pop();
|
|
166
|
+
else if (char === "}" || char === ";" && open.length === 0) break;
|
|
163
167
|
}
|
|
164
168
|
return text.slice(start, i).trim();
|
|
165
169
|
}
|
|
170
|
+
var IMPORTANT = /\s*!\s*important$/i;
|
|
171
|
+
var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
|
|
166
172
|
function forEachProperty(block, onProperty, offset = 0) {
|
|
167
173
|
const masked = maskQuotedContent(block);
|
|
168
|
-
for (const prop of masked.matchAll(
|
|
174
|
+
for (const prop of masked.matchAll(DECLARATION_NAME)) {
|
|
169
175
|
const name = prop[0];
|
|
170
176
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
171
|
-
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
177
|
+
onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
|
|
172
178
|
}
|
|
173
179
|
}
|
|
174
180
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -314,13 +320,14 @@ function toRawPosition(stripped, strippedPos) {
|
|
|
314
320
|
}
|
|
315
321
|
return rawPos;
|
|
316
322
|
}
|
|
323
|
+
var CSS_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(?:(,)|\))/gi;
|
|
317
324
|
function parseVarReferences(css) {
|
|
318
325
|
const stripped = stripCommentsWithMap(css);
|
|
319
326
|
const masked = maskQuotedContent(stripped.text);
|
|
320
327
|
const lineStarts = [0];
|
|
321
328
|
for (let i = 0; i < css.length; i++) if (css[i] === "\n") lineStarts.push(i + 1);
|
|
322
329
|
const refs = [];
|
|
323
|
-
for (const match of masked.matchAll(
|
|
330
|
+
for (const match of masked.matchAll(CSS_REF_RE)) {
|
|
324
331
|
const rawPos = toRawPosition(stripped, match.index);
|
|
325
332
|
let line = lineStarts.length - 1;
|
|
326
333
|
while (lineStarts[line] > rawPos) line--;
|
|
@@ -328,7 +335,7 @@ function parseVarReferences(css) {
|
|
|
328
335
|
}
|
|
329
336
|
return refs;
|
|
330
337
|
}
|
|
331
|
-
var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(,)
|
|
338
|
+
var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(?:(,)|\))|(?<=[\w-]-)\((?:[\w-]+:)?(--[\w-]+)\)/g;
|
|
332
339
|
function parseSourceVarReferences(text) {
|
|
333
340
|
const lineStarts = [0];
|
|
334
341
|
for (let i = 0; i < text.length; i++) if (text[i] === "\n") lineStarts.push(i + 1);
|
|
@@ -354,7 +361,7 @@ function parseSourceDeclaredNames(text) {
|
|
|
354
361
|
function parseDeclaredNames(css) {
|
|
355
362
|
const masked = maskQuotedContent(stripComments(css));
|
|
356
363
|
const names = /* @__PURE__ */ new Set();
|
|
357
|
-
for (const match of masked.matchAll(/(?<![\w
|
|
364
|
+
for (const match of masked.matchAll(/(?<![\w&-])(--[\w-]+)\s*:/g)) names.add(match[1]);
|
|
358
365
|
for (const match of masked.matchAll(/@property\s+(--[\w-]+)/g)) names.add(match[1]);
|
|
359
366
|
return names;
|
|
360
367
|
}
|
|
@@ -467,19 +474,46 @@ function rootPattern(text) {
|
|
|
467
474
|
const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
|
|
468
475
|
return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
|
|
469
476
|
}
|
|
470
|
-
var ACCESS_RE = /\s*(
|
|
477
|
+
var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
|
|
471
478
|
function readAccess(text, from) {
|
|
472
479
|
ACCESS_RE.lastIndex = from;
|
|
473
480
|
const match = ACCESS_RE.exec(text);
|
|
474
481
|
return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
|
|
475
482
|
}
|
|
483
|
+
var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
|
|
484
|
+
function readPatternKeys(text, at) {
|
|
485
|
+
let i = at - 1;
|
|
486
|
+
while (i >= 0 && /\s/.test(text[i])) i--;
|
|
487
|
+
if (text[i] !== "=") return [];
|
|
488
|
+
i--;
|
|
489
|
+
while (i >= 0 && /\s/.test(text[i])) i--;
|
|
490
|
+
if (text[i] !== "}") return [];
|
|
491
|
+
const open = text.lastIndexOf("{", i);
|
|
492
|
+
if (open === -1) return [];
|
|
493
|
+
const body = text.slice(open + 1, i);
|
|
494
|
+
if (body.includes("}")) return [];
|
|
495
|
+
const keys = [];
|
|
496
|
+
for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
|
|
497
|
+
const match = PATTERN_KEY_RE.exec(part);
|
|
498
|
+
if (match) keys.push(match[1] ?? match[3]);
|
|
499
|
+
}
|
|
500
|
+
return keys;
|
|
501
|
+
}
|
|
476
502
|
function extractUsage(text, into) {
|
|
477
503
|
for (const root of text.matchAll(rootPattern(text))) {
|
|
478
504
|
const first = readAccess(text, root.index + root[0].length);
|
|
479
|
-
|
|
505
|
+
const second = first && readAccess(text, first.end);
|
|
506
|
+
const destructured = second ? [] : readPatternKeys(text, root.index);
|
|
507
|
+
if (!first) {
|
|
508
|
+
for (const key of destructured) {
|
|
509
|
+
into.keys.add(key);
|
|
510
|
+
into.wholeKeys.add(key);
|
|
511
|
+
}
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
480
514
|
into.keys.add(first.key);
|
|
481
|
-
const second = readAccess(text, first.end);
|
|
482
515
|
if (second) into.paths.add(`${first.key}\0${second.key}`);
|
|
516
|
+
else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
|
|
483
517
|
else into.wholeKeys.add(first.key);
|
|
484
518
|
}
|
|
485
519
|
}
|
|
@@ -782,6 +816,12 @@ var HELPERS_TS = [
|
|
|
782
816
|
"",
|
|
783
817
|
"export function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F> {",
|
|
784
818
|
" return `${ref.slice(0, -1)}, ${fallback})` as CssVarWithFallback<R, F>;",
|
|
819
|
+
"}",
|
|
820
|
+
"",
|
|
821
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
822
|
+
"",
|
|
823
|
+
"export function varName<R extends CssVarRef>(ref: R): CssVarProperty<R> {",
|
|
824
|
+
" return ref.slice(4, -1) as CssVarProperty<R>;",
|
|
785
825
|
"}"
|
|
786
826
|
];
|
|
787
827
|
var HELPERS_JS = [
|
|
@@ -795,13 +835,19 @@ var HELPERS_JS = [
|
|
|
795
835
|
"",
|
|
796
836
|
"export function withFallback(ref, fallback) {",
|
|
797
837
|
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
838
|
+
"}",
|
|
839
|
+
"",
|
|
840
|
+
"export function varName(ref) {",
|
|
841
|
+
" return ref.slice(4, -1);",
|
|
798
842
|
"}"
|
|
799
843
|
];
|
|
800
844
|
var HELPERS_DTS = [
|
|
801
845
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
802
846
|
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
803
847
|
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
804
|
-
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
848
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
|
|
849
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
850
|
+
"export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
|
|
805
851
|
];
|
|
806
852
|
function generateCode(varNames, ...args2) {
|
|
807
853
|
const options = normalizeOptions(args2);
|
|
@@ -1085,7 +1131,7 @@ Options:
|
|
|
1085
1131
|
--selector <selector> Also scan this selector's blocks, not just :root/@theme (repeatable)
|
|
1086
1132
|
--theme <name>=<sel> Theme block, emitted as @theme JSDoc tags (repeatable)
|
|
1087
1133
|
--group Nest keys by the first hyphen segment (cssVars.color.primary)
|
|
1088
|
-
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback
|
|
1134
|
+
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback/varName
|
|
1089
1135
|
--prune Drop vars with no cssVars.<key> usage (needs --usage)
|
|
1090
1136
|
--usage <glob> Source files to scan for cssVars usage (repeatable)
|
|
1091
1137
|
--usage-exclude <glob> Source files to skip in the usage scan (repeatable)
|
package/dist/cli.js
CHANGED
|
@@ -4,12 +4,12 @@ import {
|
|
|
4
4
|
findUndefinedRefs,
|
|
5
5
|
findUnusedVars,
|
|
6
6
|
generate
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-DIGUOQES.js";
|
|
8
8
|
import {
|
|
9
9
|
VALID_NAMINGS,
|
|
10
10
|
createFileMatcher,
|
|
11
11
|
globWatchRoots
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-L2T4LBKN.js";
|
|
13
13
|
|
|
14
14
|
// src/cli.ts
|
|
15
15
|
import { access, readFile } from "fs/promises";
|
|
@@ -114,7 +114,7 @@ Options:
|
|
|
114
114
|
--selector <selector> Also scan this selector's blocks, not just :root/@theme (repeatable)
|
|
115
115
|
--theme <name>=<sel> Theme block, emitted as @theme JSDoc tags (repeatable)
|
|
116
116
|
--group Nest keys by the first hyphen segment (cssVars.color.primary)
|
|
117
|
-
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback
|
|
117
|
+
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback/varName
|
|
118
118
|
--prune Drop vars with no cssVars.<key> usage (needs --usage)
|
|
119
119
|
--usage <glob> Source files to scan for cssVars usage (repeatable)
|
|
120
120
|
--usage-exclude <glob> Source files to skip in the usage scan (repeatable)
|
package/dist/esbuild.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
|
|
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(
|
|
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*(
|
|
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
|
-
|
|
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/esbuild.d.cts
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
|
@@ -41,7 +41,7 @@ interface GeneratorOptions {
|
|
|
41
41
|
group?: boolean;
|
|
42
42
|
/** `@property` rules per var name, emitted as `@syntax` JSDoc tags and enum union types. */
|
|
43
43
|
properties?: Map<string, PropertyRule>;
|
|
44
|
-
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`
|
|
44
|
+
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
|
|
45
45
|
helpers?: boolean;
|
|
46
46
|
/** Per theme name, the values declared for it, emitted as `@theme <name> <value>` JSDoc tags. */
|
|
47
47
|
themes?: Map<string, Map<string, string>>;
|
|
@@ -41,7 +41,7 @@ interface GeneratorOptions {
|
|
|
41
41
|
group?: boolean;
|
|
42
42
|
/** `@property` rules per var name, emitted as `@syntax` JSDoc tags and enum union types. */
|
|
43
43
|
properties?: Map<string, PropertyRule>;
|
|
44
|
-
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`
|
|
44
|
+
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
|
|
45
45
|
helpers?: boolean;
|
|
46
46
|
/** Per theme name, the values declared for it, emitted as `@theme <name> <value>` JSDoc tags. */
|
|
47
47
|
themes?: Map<string, Map<string, string>>;
|