css-typed-vars 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -9
- package/dist/{chunk-TD7SYMDA.js → chunk-32V3V6WT.js} +46 -29
- package/dist/chunk-5HPTHW7S.js +820 -0
- package/dist/{chunk-VZ2BHHXD.js → chunk-YDXCJQPL.js} +42 -29
- package/dist/cli.cjs +542 -151
- package/dist/cli.js +142 -73
- package/dist/esbuild.cjs +358 -87
- 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.cts +69 -0
- package/dist/generator-DJ9uOYBP.d.ts +69 -0
- package/dist/index.cjs +423 -77
- package/dist/index.d.cts +29 -6
- package/dist/index.d.ts +29 -6
- package/dist/index.js +14 -2
- package/dist/rollup.cjs +358 -87
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +358 -87
- package/dist/unplugin.d.cts +12 -2
- package/dist/unplugin.d.ts +12 -2
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +358 -87
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +358 -87
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-LH53WYFF.js +0 -487
- package/dist/generator-C2AppeJL.d.cts +0 -15
- package/dist/generator-C2AppeJL.d.ts +0 -15
package/README.md
CHANGED
|
@@ -118,6 +118,7 @@ npx css-typed-vars --input "src/styles/**/*.{css,scss}" --output src/cssVars.ts
|
|
|
118
118
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --exclude "**/vendor/**"
|
|
119
119
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prefix theme --naming snake
|
|
120
120
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.js # generates .js + .d.ts
|
|
121
|
+
npx css-typed-vars --input "src/**/*.css" --output tokens.json # W3C design tokens (DTCG) JSON
|
|
121
122
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --selector ".dark" --selector "[data-theme='dark']"
|
|
122
123
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --check # for CI: exits 1 if out of date
|
|
123
124
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --group # nest keys by first name segment
|
|
@@ -128,14 +129,18 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
128
129
|
| Flag | Description |
|
|
129
130
|
|------|-------------|
|
|
130
131
|
| `--input` | Glob pattern for CSS/SCSS/Less files (repeatable: `--input "src/**/*.css" --input "lib/**/*.css"`) |
|
|
131
|
-
| `--output` | Output file. `.ts` → TypeScript, `.js` → JavaScript + `.d.ts` alongside |
|
|
132
|
+
| `--output` | Output file. `.ts` → TypeScript, `.js` → JavaScript + `.d.ts` alongside, `.json` → [design tokens](#design-tokens-dtcg) |
|
|
132
133
|
| `--exclude` | Glob pattern for files to exclude (repeatable: `--exclude "**/a/**" --exclude "**/b/**"`) |
|
|
133
134
|
| `--prefix` | Prefix for generated keys: `--prefix theme` → `themeColorPrimary` |
|
|
134
135
|
| `--naming` | Key naming: `camelCase` (default), `snake`, `kebab`, `constant`, `pascal` |
|
|
135
136
|
| `--selector` | Extra CSS selector to scan for variables (repeatable: `--selector ".dark" --selector "[data-theme='dark']"`) |
|
|
137
|
+
| `--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) |
|
|
136
138
|
| `--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`. |
|
|
137
|
-
| `--
|
|
139
|
+
| `--helpers` | Also emit the `CssVarRef` type and typed `getCssVar(ref, el?)`/`setCssVar(ref, value, el?)`/`withFallback(ref, fallback)` helpers — see [Usage](#usage). |
|
|
140
|
+
| `--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>`. |
|
|
138
141
|
| `--prune-check` | Like `--prune`, but only reports unused vars without writing; exits 1 if any are found. Useful in CI. |
|
|
142
|
+
| `--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) |
|
|
143
|
+
| `--refs-ignore` | Name or `--prefix-*` pattern for `--lint-refs` to skip (repeatable) |
|
|
139
144
|
| `--usage` | Glob of source files to scan for `cssVars` usage, for `--prune`/`--prune-check` (repeatable) |
|
|
140
145
|
| `--usage-exclude` | Glob of source files to exclude from the usage scan (repeatable) |
|
|
141
146
|
| `--watch` | Watch for file changes and regenerate |
|
|
@@ -153,6 +158,27 @@ When in doubt, use `--prune-check` in CI to review what it would remove before t
|
|
|
153
158
|
|
|
154
159
|
CLI flags override values from the config file.
|
|
155
160
|
|
|
161
|
+
### Linting var() references
|
|
162
|
+
|
|
163
|
+
`--lint-refs` checks the input CSS for `var(--name)` references to variables that aren't declared anywhere in it — typos, or variables that were renamed or removed. No output file is needed:
|
|
164
|
+
|
|
165
|
+
```sh
|
|
166
|
+
npx css-typed-vars --input "src/**/*.css" --lint-refs
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
css-typed-vars: 1 reference(s) to undeclared variables with a fallback:
|
|
171
|
+
src/card.css:12:9 var(--card-gap, …)
|
|
172
|
+
css-typed-vars: 2 reference(s) to undeclared variables:
|
|
173
|
+
src/button.css:4:10 var(--colr-primary)
|
|
174
|
+
src/button.css:9:18 var(--radius-xl)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
- A variable counts as declared if any input file declares it in any block (not just `:root`), or registers it with `@property`.
|
|
178
|
+
- 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.
|
|
179
|
+
- References inside comments and strings are ignored; positions point to the original file.
|
|
180
|
+
- 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.
|
|
181
|
+
|
|
156
182
|
### Config file
|
|
157
183
|
|
|
158
184
|
The CLI looks for a config file in the current working directory (in order):
|
|
@@ -172,12 +198,32 @@ export default {
|
|
|
172
198
|
prefix: 'theme',
|
|
173
199
|
naming: 'snake', // 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal'
|
|
174
200
|
selectors: ['.dark', '[data-theme="dark"]'],
|
|
201
|
+
themes: { dark: '[data-theme="dark"]' },
|
|
175
202
|
group: false,
|
|
203
|
+
helpers: false,
|
|
204
|
+
refsIgnore: ['--tw-*'],
|
|
176
205
|
prune: false,
|
|
177
206
|
usage: 'src/**/*.{ts,tsx}',
|
|
178
207
|
};
|
|
179
208
|
```
|
|
180
209
|
|
|
210
|
+
#### Multiple targets
|
|
211
|
+
|
|
212
|
+
Export an array to generate several files in one run — e.g. one per package in a monorepo, or a TypeScript file plus a design tokens file:
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
// css-typed-vars.config.js
|
|
216
|
+
export default [
|
|
217
|
+
{ input: 'packages/ui/src/**/*.css', output: 'packages/ui/src/cssVars.ts' },
|
|
218
|
+
{ input: 'packages/docs/styles/*.css', output: 'packages/docs/cssVars.ts', naming: 'kebab' },
|
|
219
|
+
{ input: 'packages/ui/src/**/*.css', output: 'tokens/ui.json', group: true },
|
|
220
|
+
];
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
- `--check`, `--prune-check` and `--lint-refs` run for every target and exit `1` if any target fails; each message is tagged with its target's output (`css-typed-vars [packages/ui/src/cssVars.ts]: ...`).
|
|
224
|
+
- `--watch` watches each target's `input` and regenerates only that target on change.
|
|
225
|
+
- Flags that set options (`--input`, `--naming`, `--group`, ...) can't be combined with an array config — set them per target instead. Mode flags (`--watch`, `--check`, ...) work as usual.
|
|
226
|
+
|
|
181
227
|
---
|
|
182
228
|
|
|
183
229
|
## Plugin
|
|
@@ -273,11 +319,14 @@ Turbopack does not yet have a public plugin API for virtual modules. Use the CLI
|
|
|
273
319
|
| `prefix` | `string` | — | Prefix for generated keys: `'theme'` → `themeColorPrimary` |
|
|
274
320
|
| `naming` | `'camelCase' \| 'snake' \| 'kebab' \| 'constant' \| 'pascal'` | `'camelCase'` | Key naming convention |
|
|
275
321
|
| `selectors` | `string[]` | — | Extra CSS selectors to scan, e.g. `['.dark', '[data-theme="dark"]']` |
|
|
322
|
+
| `themes` | `Record<string, string>` | — | Theme name → selector or `@media` prelude; values become `@theme` JSDoc tags — see [Theme values](#theme-values) |
|
|
276
323
|
| `group` | `boolean` | `false` | Nest keys by each variable's first hyphen segment, e.g. `cssVars.color.primary`. Can't be combined with `prefix` |
|
|
277
|
-
| `
|
|
324
|
+
| `helpers` | `boolean` | `false` | Also export the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback` helpers from `css-typed-vars/vars` |
|
|
325
|
+
| `prune` | `boolean` | `false` | Drop vars with no detected `cssVars.<key>` (or `cssVars.<group>.<key>` with `group`) usage. Requires `usage` |
|
|
278
326
|
| `usage` | `string \| string[]` | — | Glob(s) of source files to scan for `cssVars` usage, for `prune` |
|
|
279
327
|
| `usageExclude` | `string \| string[]` | — | Glob(s) of source files to exclude from the usage scan |
|
|
280
328
|
| `dts` | `string \| false` | inside `node_modules` | Path to write type declarations. `false` to skip |
|
|
329
|
+
| `tokens` | `string` | — | Also write a [design tokens (DTCG)](#design-tokens-dtcg) JSON file to this path |
|
|
281
330
|
|
|
282
331
|
---
|
|
283
332
|
|
|
@@ -320,6 +369,28 @@ const Button = styled.button`
|
|
|
320
369
|
<div className={`text-[${cssVars.colorPrimary}]`} />
|
|
321
370
|
```
|
|
322
371
|
|
|
372
|
+
**Reading and writing at runtime** (`helpers: true` / `--helpers`):
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
import { cssVars, getCssVar, setCssVar } from './cssVars';
|
|
376
|
+
|
|
377
|
+
getCssVar(cssVars.colorPrimary); // computed value on <html>, e.g. '#3b82f6'
|
|
378
|
+
setCssVar(cssVars.colorPrimary, '#ef4444'); // inline style on <html>
|
|
379
|
+
setCssVar(cssVars.spacingMd, '12px', cardEl); // or on a specific element
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
The helpers take a `cssVars` value (typed as `CssVarRef`, the union of every generated `'var(--name)'`), so they work the same with any `naming`, `prefix` or `group`. `getCssVar`/`setCssVar` use `getComputedStyle`/`style.setProperty`, so call them in the browser only, and the TS output needs the `DOM` lib in your `tsconfig`.
|
|
383
|
+
|
|
384
|
+
`withFallback` builds a `var()` with a fallback value, typed as the exact resulting string — handy in style objects and CSS-in-JS. Unlike the two above, it's a pure string helper and works anywhere, including on the server:
|
|
385
|
+
|
|
386
|
+
```ts
|
|
387
|
+
import { cssVars, withFallback } from './cssVars';
|
|
388
|
+
|
|
389
|
+
withFallback(cssVars.colorPrimary, '#3b82f6'); // 'var(--color-primary, #3b82f6)'
|
|
390
|
+
withFallback(cssVars.spacingMd, 0); // 'var(--spacing-md, 0)'
|
|
391
|
+
withFallback(cssVars.colorLink, cssVars.colorPrimary); // 'var(--color-link, var(--color-primary))'
|
|
392
|
+
```
|
|
393
|
+
|
|
323
394
|
---
|
|
324
395
|
|
|
325
396
|
## Programmatic API
|
|
@@ -329,12 +400,14 @@ import { generate } from 'css-typed-vars';
|
|
|
329
400
|
|
|
330
401
|
await generate({
|
|
331
402
|
input: 'src/styles/**/*.{css,scss}',
|
|
332
|
-
output: 'src/cssVars.ts', // or 'src/cssVars.js' →
|
|
403
|
+
output: 'src/cssVars.ts', // or 'src/cssVars.js' → .js + .d.ts, or 'tokens.json' → design tokens
|
|
333
404
|
exclude: ['**/vendor/**'],
|
|
334
405
|
prefix: 'theme',
|
|
335
406
|
naming: 'snake', // 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal'
|
|
336
407
|
selectors: ['.dark', '[data-theme="dark"]'],
|
|
408
|
+
themes: { dark: '[data-theme="dark"]' }, // per-theme values as @theme JSDoc tags
|
|
337
409
|
group: false, // nest keys by first hyphen segment, e.g. cssVars.color.primary
|
|
410
|
+
helpers: false, // also emit getCssVar/setCssVar/withFallback helpers
|
|
338
411
|
prune: false, // drop vars with no detected cssVars.<key> usage (requires `usage`)
|
|
339
412
|
usage: 'src/**/*.{ts,tsx}',
|
|
340
413
|
});
|
|
@@ -342,12 +415,14 @@ await generate({
|
|
|
342
415
|
|
|
343
416
|
`checkGenerated(options)` takes the same options and returns `Promise<boolean>` — `true` if the existing output already matches what would be generated, without writing anything. Same thing `--check` does on the CLI.
|
|
344
417
|
|
|
345
|
-
`
|
|
418
|
+
`findUndefinedRefs({ input, exclude?, refsIgnore? })` returns `Promise<FileReference[]>` — each undeclared `var()` reference with its `file`, `line`, `column` and `hasFallback`. Same thing `--lint-refs` does on the CLI.
|
|
419
|
+
|
|
420
|
+
`findUnusedVars(options)` takes `input`/`exclude`/`selectors`/`prefix`/`naming`/`group`/`usage`/`usageExclude` and returns `Promise<string[]>` — the CSS variable names with no detected `cssVars.<key>` usage, without writing or pruning anything. Same thing `--prune-check` does on the CLI.
|
|
346
421
|
|
|
347
422
|
Lower-level exports:
|
|
348
423
|
|
|
349
424
|
```ts
|
|
350
|
-
import { parseVarNames, parseVarDeclarations, parsePropertyRules, generateCode, scanVarNames, scanVarDeclarations, scanCss, scanUsedKeys } from 'css-typed-vars';
|
|
425
|
+
import { parseVarNames, parseVarDeclarations, parsePropertyRules, parseThemedDeclarations, generateCode, generateTokens, scanVarNames, scanVarDeclarations, scanCss, scanUsedKeys } from 'css-typed-vars';
|
|
351
426
|
```
|
|
352
427
|
|
|
353
428
|
## Supported formats
|
|
@@ -360,6 +435,109 @@ import { parseVarNames, parseVarDeclarations, parsePropertyRules, generateCode,
|
|
|
360
435
|
|
|
361
436
|
By default, variables are scanned from `:root {}` blocks (including attribute selectors like `:root[data-theme="dark"]`) and Tailwind CSS v4's `@theme {}` blocks. Use the `selectors` option to also pick up variables from other selectors such as `.dark` or `[data-theme="dark"]`.
|
|
362
437
|
|
|
438
|
+
## Theme values
|
|
439
|
+
|
|
440
|
+
By default, a variable redeclared in another block (`:root[data-theme="dark"]`, or a `selectors` entry) simply overwrites its `@default` value — last one wins. The `themes` option names those blocks instead, so each theme's value is kept separately:
|
|
441
|
+
|
|
442
|
+
```js
|
|
443
|
+
// css-typed-vars.config.js
|
|
444
|
+
export default {
|
|
445
|
+
input: 'src/**/*.css',
|
|
446
|
+
output: 'src/cssVars.ts',
|
|
447
|
+
themes: {
|
|
448
|
+
dark: '[data-theme="dark"]', // a selector...
|
|
449
|
+
contrast: '@media (prefers-contrast: more)', // ...or an @media prelude (its :root/@theme blocks are read)
|
|
450
|
+
},
|
|
451
|
+
};
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
```css
|
|
455
|
+
:root { --color-bg: #ffffff; }
|
|
456
|
+
[data-theme="dark"] { --color-bg: #0b0b0f; --glow: 0 0 8px #7c3aed; }
|
|
457
|
+
@media (prefers-contrast: more) { :root { --color-bg: #000000; } }
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
```ts
|
|
461
|
+
export const cssVars = {
|
|
462
|
+
/**
|
|
463
|
+
* @default #ffffff
|
|
464
|
+
* @theme dark #0b0b0f
|
|
465
|
+
* @theme contrast #000000
|
|
466
|
+
*/
|
|
467
|
+
colorBg: 'var(--color-bg)',
|
|
468
|
+
/** @theme dark 0 0 8px #7c3aed */
|
|
469
|
+
glow: 'var(--glow)',
|
|
470
|
+
} as const;
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
- Theme blocks are kept out of the default value, so `@default` always shows the base (`:root`/`@theme`/`selectors`) value.
|
|
474
|
+
- A variable declared only inside a theme (`--glow` above) is still generated, with just its theme tags.
|
|
475
|
+
- An `@media` theme matches by its prelude, ignoring whitespace and case. Other `@media` blocks are read as before.
|
|
476
|
+
- On the CLI, pass `--theme <name>=<selector>` (repeatable): `--theme dark='[data-theme="dark"]'`.
|
|
477
|
+
|
|
478
|
+
## Doc comments
|
|
479
|
+
|
|
480
|
+
A `/** ... */` comment directly before a declaration is carried over to the generated key, so it shows up in editor hover — and `@deprecated` makes the editor strike through every usage:
|
|
481
|
+
|
|
482
|
+
```css
|
|
483
|
+
:root {
|
|
484
|
+
/** Page background. Use for full-bleed surfaces only. */
|
|
485
|
+
--color-bg: #ffffff;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Legacy link color.
|
|
489
|
+
* @deprecated use --color-primary
|
|
490
|
+
*/
|
|
491
|
+
--color-link: #2563eb;
|
|
492
|
+
}
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
```ts
|
|
496
|
+
export const cssVars = {
|
|
497
|
+
/**
|
|
498
|
+
* Page background. Use for full-bleed surfaces only.
|
|
499
|
+
* @default #ffffff
|
|
500
|
+
*/
|
|
501
|
+
colorBg: 'var(--color-bg)',
|
|
502
|
+
/**
|
|
503
|
+
* Legacy link color.
|
|
504
|
+
* @default #2563eb
|
|
505
|
+
* @deprecated use --color-primary
|
|
506
|
+
*/
|
|
507
|
+
colorLink: 'var(--color-link)',
|
|
508
|
+
} as const;
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
- Only `/** */` comments count — plain `/* */` comments (like section headers) are ignored, and the comment must be directly before the declaration (only whitespace in between).
|
|
512
|
+
- The text before the first tag becomes the description; `@deprecated [reason]` is kept, other tags are ignored.
|
|
513
|
+
- If a variable has doc comments in several places, the one in a base block (`:root`/`@theme`/`selectors`) wins over one in a [theme](#theme-values) block.
|
|
514
|
+
- In [design tokens](#design-tokens-dtcg) output they become `$description` and `$deprecated`.
|
|
515
|
+
|
|
516
|
+
## Design tokens (DTCG)
|
|
517
|
+
|
|
518
|
+
Point `output` at a `.json` file to get a [W3C Design Tokens](https://www.designtokens.org/tr/drafts/format/) document instead of TypeScript, for Style Dictionary, Tokens Studio and similar tools:
|
|
519
|
+
|
|
520
|
+
```sh
|
|
521
|
+
npx css-typed-vars --input "src/**/*.css" --output tokens/tokens.json --group
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
```json
|
|
525
|
+
{
|
|
526
|
+
"color": {
|
|
527
|
+
"link": { "$value": "{color.primary}" },
|
|
528
|
+
"primary": { "$value": "#3b82f6", "$type": "color" }
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
- Keys follow `naming`/`prefix`/`group`, like the generated object.
|
|
534
|
+
- `$value` is the declared value (or the `@property` `initial-value`); `$type` is inferred from the `@property` syntax (`<color>`, `<length>`, `<number>`, `<time>`, ...).
|
|
535
|
+
- A value that's exactly `var(--other)` becomes an alias (`{color.primary}`) when `--other` is in the file.
|
|
536
|
+
- [Theme values](#theme-values) go under `$extensions["css-typed-vars"].themes`; [doc comments](#doc-comments) become `$description`/`$deprecated`.
|
|
537
|
+
- The plugin can write the file too: `cssTypedVars({ input, tokens: 'tokens/tokens.json' })`.
|
|
538
|
+
|
|
539
|
+
See the [Design tokens guide](https://stanislavkozachenko.github.io/css-typed-vars/guide/design-tokens) for details.
|
|
540
|
+
|
|
363
541
|
## @property syntax typing
|
|
364
542
|
|
|
365
543
|
If a variable is registered via the [CSS Properties and Values API](https://developer.mozilla.org/en-US/docs/Web/CSS/@property), its `syntax` descriptor is picked up automatically — no config needed:
|
|
@@ -377,8 +555,10 @@ If a variable is registered via the [CSS Properties and Values API](https://deve
|
|
|
377
555
|
|
|
378
556
|
```ts
|
|
379
557
|
export const cssVars = {
|
|
380
|
-
/**
|
|
381
|
-
|
|
558
|
+
/**
|
|
559
|
+
* @default light
|
|
560
|
+
* @syntax light | dark | system
|
|
561
|
+
*/
|
|
382
562
|
themeMode: 'var(--theme-mode)',
|
|
383
563
|
} as const;
|
|
384
564
|
|
|
@@ -387,7 +567,7 @@ export type CssVarName = keyof typeof cssVars;
|
|
|
387
567
|
export type ThemeModeSyntax = 'light' | 'dark' | 'system';
|
|
388
568
|
```
|
|
389
569
|
|
|
390
|
-
Every `syntax` shows up as a `@syntax` JSDoc comment. When it's a pipe-separated list of custom idents (an enum, like above) — as opposed to a generic data type like `<color>` or `<length>` — it also gets its own exported union type, named `<PascalCaseVarName>Syntax`. Useful when setting the property from JS: `el.style.setProperty('--theme-mode', mode satisfies ThemeModeSyntax)`.
|
|
570
|
+
Every `syntax` shows up as a `@syntax` JSDoc tag (in the same comment block as `@default`, so editor hover shows both). When it's a pipe-separated list of custom idents (an enum, like above) — as opposed to a generic data type like `<color>` or `<length>` — it also gets its own exported union type, named `<PascalCaseVarName>Syntax`. Useful when setting the property from JS: `el.style.setProperty('--theme-mode', mode satisfies ThemeModeSyntax)`.
|
|
391
571
|
|
|
392
572
|
Note this doesn't change the type of `cssVars.themeMode` itself — that's always the literal string `'var(--theme-mode)'`, regardless of `@property`, since that's what you actually get at runtime. The union type is a separate, additional export for validating what you *assign* to the variable.
|
|
393
573
|
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__dirname,
|
|
3
|
-
|
|
3
|
+
assertPruneCompatible,
|
|
4
|
+
collectVarNames,
|
|
4
5
|
generateDeclaration,
|
|
5
6
|
generateJs,
|
|
7
|
+
generateTokens,
|
|
8
|
+
partitionUnused,
|
|
6
9
|
scanCss,
|
|
7
|
-
|
|
10
|
+
scanUsage,
|
|
8
11
|
warnOnCollisions
|
|
9
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-5HPTHW7S.js";
|
|
10
13
|
|
|
11
14
|
// src/unplugin.ts
|
|
12
15
|
import { createUnplugin } from "unplugin";
|
|
@@ -18,25 +21,38 @@ function getDtsPath(options) {
|
|
|
18
21
|
if (options.dts === false) return null;
|
|
19
22
|
return options.dts ? resolve(process.cwd(), options.dts) : join(__dirname, "generated.d.ts");
|
|
20
23
|
}
|
|
24
|
+
function outputFiles(options, names, generatorOptions) {
|
|
25
|
+
const files = [];
|
|
26
|
+
const dtsPath = getDtsPath(options);
|
|
27
|
+
if (dtsPath) files.push([dtsPath, generateDeclaration(names, generatorOptions)]);
|
|
28
|
+
if (options.tokens) files.push([resolve(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
|
|
29
|
+
return files;
|
|
30
|
+
}
|
|
21
31
|
var unplugin_default = createUnplugin((options) => {
|
|
22
|
-
|
|
23
|
-
if (options.group) throw new Error('css-typed-vars: "prune" cannot be combined with "group".');
|
|
24
|
-
if (!options.usage) throw new Error('css-typed-vars: "prune" requires "usage" (glob(s) of source files to scan).');
|
|
25
|
-
}
|
|
32
|
+
assertPruneCompatible(options);
|
|
26
33
|
let cachedScan = null;
|
|
27
|
-
const scan = () => scanCss(options.input, options.exclude, options.selectors);
|
|
34
|
+
const scan = () => scanCss(options.input, options.exclude, options.selectors, options.themes);
|
|
28
35
|
const applyPrune = async (names) => {
|
|
29
36
|
if (!options.prune) return names;
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const unused = names.filter((name) => !isUsed(name));
|
|
37
|
+
const usage = await scanUsage(options.usage, options.usageExclude);
|
|
38
|
+
const { used, unused } = partitionUnused(names, usage, options);
|
|
33
39
|
if (unused.length > 0) {
|
|
34
40
|
console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
|
|
35
41
|
}
|
|
36
|
-
return
|
|
42
|
+
return used;
|
|
37
43
|
};
|
|
38
|
-
const
|
|
39
|
-
|
|
44
|
+
const generatorOptions = ({ declarations, properties, themes, docs }) => ({
|
|
45
|
+
prefix: options.prefix,
|
|
46
|
+
naming: options.naming,
|
|
47
|
+
declarations,
|
|
48
|
+
group: options.group,
|
|
49
|
+
properties,
|
|
50
|
+
helpers: options.helpers,
|
|
51
|
+
themes,
|
|
52
|
+
docs
|
|
53
|
+
});
|
|
54
|
+
const resolveNames = async (scanned) => {
|
|
55
|
+
const names = collectVarNames(scanned);
|
|
40
56
|
warnOnCollisions(names, options.prefix, options.naming);
|
|
41
57
|
return applyPrune(names);
|
|
42
58
|
};
|
|
@@ -52,16 +68,17 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
52
68
|
const myGeneration = ++generation;
|
|
53
69
|
const scanPromise = scan();
|
|
54
70
|
cachedScan = scanPromise;
|
|
55
|
-
const
|
|
71
|
+
const scanned = await scanPromise;
|
|
56
72
|
if (myGeneration !== generation) return;
|
|
57
|
-
const names = await resolveNames(
|
|
73
|
+
const names = await resolveNames(scanned);
|
|
58
74
|
if (myGeneration !== generation) return;
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
61
|
-
const content = generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties);
|
|
75
|
+
const files = outputFiles(options, names, generatorOptions(scanned));
|
|
76
|
+
if (files.length > 0) {
|
|
62
77
|
writeQueue = writeQueue.then(async () => {
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
for (const [path, content] of files) {
|
|
79
|
+
if (myGeneration !== generation) return;
|
|
80
|
+
await writeFile(path, content, "utf8");
|
|
81
|
+
}
|
|
65
82
|
}).catch(console.error);
|
|
66
83
|
await writeQueue;
|
|
67
84
|
}
|
|
@@ -87,20 +104,20 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
87
104
|
},
|
|
88
105
|
async buildStart() {
|
|
89
106
|
cachedScan = scan();
|
|
90
|
-
const
|
|
91
|
-
const names = await resolveNames(
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
107
|
+
const scanned = await cachedScan;
|
|
108
|
+
const names = await resolveNames(scanned);
|
|
109
|
+
for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
|
|
110
|
+
await writeFile(path, content, "utf8");
|
|
111
|
+
}
|
|
95
112
|
},
|
|
96
113
|
resolveId(id) {
|
|
97
114
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
98
115
|
},
|
|
99
116
|
async load(id) {
|
|
100
117
|
if (id === RESOLVED_ID) {
|
|
101
|
-
const
|
|
102
|
-
const names = await applyPrune(
|
|
103
|
-
return generateJs(names,
|
|
118
|
+
const scanned = await (cachedScan ?? scan());
|
|
119
|
+
const names = await applyPrune(collectVarNames(scanned));
|
|
120
|
+
return generateJs(names, generatorOptions(scanned));
|
|
104
121
|
}
|
|
105
122
|
}
|
|
106
123
|
};
|