css-typed-vars 0.5.0 → 0.7.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 +152 -5
- package/dist/chunk-6YWE4A6S.js +691 -0
- package/dist/chunk-HBUSLYYB.js +127 -0
- package/dist/chunk-PLO2JWR7.js +88 -0
- package/dist/cli.cjs +480 -57
- package/dist/cli.js +51 -9
- package/dist/esbuild.cjs +433 -60
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/generator-C1iVNsry.d.cts +49 -0
- package/dist/generator-C1iVNsry.d.ts +49 -0
- package/dist/index.cjs +452 -47
- package/dist/index.d.cts +27 -6
- package/dist/index.d.ts +27 -6
- package/dist/index.js +14 -2
- package/dist/rollup.cjs +433 -60
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +433 -60
- package/dist/unplugin.d.cts +17 -1
- package/dist/unplugin.d.ts +17 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +433 -60
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +433 -60
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +6 -2
- package/dist/chunk-H3T6RMQQ.js +0 -51
- package/dist/chunk-JDZAH2YQ.js +0 -320
- package/dist/chunk-YBZGHG2G.js +0 -91
- package/dist/generator-t6h8Ic7V.d.cts +0 -6
- package/dist/generator-t6h8Ic7V.d.ts +0 -6
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<h1>css-typed-vars</h1>
|
|
4
4
|
<p>Generate TypeScript typed constants from CSS custom properties</p>
|
|
5
|
+
<p><a href="https://stanislavkozachenko.github.io/css-typed-vars/">📖 Full documentation</a></p>
|
|
5
6
|
|
|
6
7
|
<p>
|
|
7
8
|
<a href="https://github.com/StanislavKozachenko/css-typed-vars/actions"><img src="https://github.com/StanislavKozachenko/css-typed-vars/actions/workflows/ci.yml/badge.svg" alt="CI" /></a>
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
- [Usage](#usage)
|
|
28
29
|
- [Programmatic API](#programmatic-api)
|
|
29
30
|
- [Supported formats](#supported-formats)
|
|
31
|
+
- [@property syntax typing](#property-syntax-typing)
|
|
30
32
|
- [Contributing](#contributing)
|
|
31
33
|
|
|
32
34
|
---
|
|
@@ -35,7 +37,7 @@
|
|
|
35
37
|
|
|
36
38
|
`css-typed-vars` scans your CSS files and gives you typed constants. Use `cssVars.colorPrimary` instead — TypeScript catches missing variables at compile time.
|
|
37
39
|
|
|
38
|
-
Each generated key also carries a `/** @default ... */` comment with the property's declared CSS value, so your editor shows it on hover and autocomplete.
|
|
40
|
+
Each generated key also carries a `/** @default ... */` comment with the property's declared CSS value, so your editor shows it on hover and autocomplete. If the variable is registered with [`@property`](#property-syntax-typing), its `syntax` descriptor shows up too, and an enum-like syntax (e.g. `"light | dark | system"`) gets its own exported TS union type.
|
|
39
41
|
|
|
40
42
|
## Installation
|
|
41
43
|
|
|
@@ -116,22 +118,42 @@ npx css-typed-vars --input "src/styles/**/*.{css,scss}" --output src/cssVars.ts
|
|
|
116
118
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --exclude "**/vendor/**"
|
|
117
119
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prefix theme --naming snake
|
|
118
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
|
|
119
122
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --selector ".dark" --selector "[data-theme='dark']"
|
|
120
123
|
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --check # for CI: exits 1 if out of date
|
|
124
|
+
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --group # nest keys by first name segment
|
|
125
|
+
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune --usage "src/**/*.{ts,tsx}" # drop unused vars from the output
|
|
126
|
+
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check --usage "src/**/*.{ts,tsx}" # for CI: exits 1 if any var is unused
|
|
121
127
|
```
|
|
122
128
|
|
|
123
129
|
| Flag | Description |
|
|
124
130
|
|------|-------------|
|
|
125
131
|
| `--input` | Glob pattern for CSS/SCSS/Less files (repeatable: `--input "src/**/*.css" --input "lib/**/*.css"`) |
|
|
126
|
-
| `--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) |
|
|
127
133
|
| `--exclude` | Glob pattern for files to exclude (repeatable: `--exclude "**/a/**" --exclude "**/b/**"`) |
|
|
128
134
|
| `--prefix` | Prefix for generated keys: `--prefix theme` → `themeColorPrimary` |
|
|
129
135
|
| `--naming` | Key naming: `camelCase` (default), `snake`, `kebab`, `constant`, `pascal` |
|
|
130
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) |
|
|
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`. |
|
|
139
|
+
| `--helpers` | Also emit the `CssVarRef` type and typed `getCssVar(ref, el?)`/`setCssVar(ref, value, el?)` runtime 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>`. |
|
|
141
|
+
| `--prune-check` | Like `--prune`, but only reports unused vars without writing; exits 1 if any are found. Useful in CI. |
|
|
142
|
+
| `--usage` | Glob of source files to scan for `cssVars` usage, for `--prune`/`--prune-check` (repeatable) |
|
|
143
|
+
| `--usage-exclude` | Glob of source files to exclude from the usage scan (repeatable) |
|
|
131
144
|
| `--watch` | Watch for file changes and regenerate |
|
|
132
145
|
| `--check` | Check whether the output is up to date without writing; exits 1 if it would differ. Useful in CI. Can't be combined with `--watch`. |
|
|
133
146
|
| `--version`, `-v` | Print the version number and exit |
|
|
134
147
|
|
|
148
|
+
### Pruning unused variables
|
|
149
|
+
|
|
150
|
+
`--prune`/`--prune-check` scan your source files for `cssVars.<key>` (dot access) or `cssVars['<key>']`/`cssVars["<key>"]` (bracket access) usage and compare that against the generated keys. This is a textual scan, not a full parser, so it has two limitations worth knowing:
|
|
151
|
+
|
|
152
|
+
- It won't see through a renamed import (`import { cssVars as vars } from './cssVars'` — usages via `vars.foo` aren't detected).
|
|
153
|
+
- A match inside a comment or string still counts as "used".
|
|
154
|
+
|
|
155
|
+
When in doubt, use `--prune-check` in CI to review what it would remove before turning on `--prune` for real.
|
|
156
|
+
|
|
135
157
|
CLI flags override values from the config file.
|
|
136
158
|
|
|
137
159
|
### Config file
|
|
@@ -153,6 +175,11 @@ export default {
|
|
|
153
175
|
prefix: 'theme',
|
|
154
176
|
naming: 'snake', // 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal'
|
|
155
177
|
selectors: ['.dark', '[data-theme="dark"]'],
|
|
178
|
+
themes: { dark: '[data-theme="dark"]' },
|
|
179
|
+
group: false,
|
|
180
|
+
helpers: false,
|
|
181
|
+
prune: false,
|
|
182
|
+
usage: 'src/**/*.{ts,tsx}',
|
|
156
183
|
};
|
|
157
184
|
```
|
|
158
185
|
|
|
@@ -251,7 +278,14 @@ Turbopack does not yet have a public plugin API for virtual modules. Use the CLI
|
|
|
251
278
|
| `prefix` | `string` | — | Prefix for generated keys: `'theme'` → `themeColorPrimary` |
|
|
252
279
|
| `naming` | `'camelCase' \| 'snake' \| 'kebab' \| 'constant' \| 'pascal'` | `'camelCase'` | Key naming convention |
|
|
253
280
|
| `selectors` | `string[]` | — | Extra CSS selectors to scan, e.g. `['.dark', '[data-theme="dark"]']` |
|
|
281
|
+
| `themes` | `Record<string, string>` | — | Theme name → selector or `@media` prelude; values become `@theme` JSDoc tags — see [Theme values](#theme-values) |
|
|
282
|
+
| `group` | `boolean` | `false` | Nest keys by each variable's first hyphen segment, e.g. `cssVars.color.primary`. Can't be combined with `prefix` |
|
|
283
|
+
| `helpers` | `boolean` | `false` | Also export the `CssVarRef` type and `getCssVar`/`setCssVar` runtime helpers from `css-typed-vars/vars` |
|
|
284
|
+
| `prune` | `boolean` | `false` | Drop vars with no detected `cssVars.<key>` (or `cssVars.<group>.<key>` with `group`) usage. Requires `usage` |
|
|
285
|
+
| `usage` | `string \| string[]` | — | Glob(s) of source files to scan for `cssVars` usage, for `prune` |
|
|
286
|
+
| `usageExclude` | `string \| string[]` | — | Glob(s) of source files to exclude from the usage scan |
|
|
254
287
|
| `dts` | `string \| false` | inside `node_modules` | Path to write type declarations. `false` to skip |
|
|
288
|
+
| `tokens` | `string` | — | Also write a [design tokens (DTCG)](#design-tokens-dtcg) JSON file to this path |
|
|
255
289
|
|
|
256
290
|
---
|
|
257
291
|
|
|
@@ -294,6 +328,18 @@ const Button = styled.button`
|
|
|
294
328
|
<div className={`text-[${cssVars.colorPrimary}]`} />
|
|
295
329
|
```
|
|
296
330
|
|
|
331
|
+
**Reading and writing at runtime** (`helpers: true` / `--helpers`):
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
import { cssVars, getCssVar, setCssVar } from './cssVars';
|
|
335
|
+
|
|
336
|
+
getCssVar(cssVars.colorPrimary); // computed value on <html>, e.g. '#3b82f6'
|
|
337
|
+
setCssVar(cssVars.colorPrimary, '#ef4444'); // inline style on <html>
|
|
338
|
+
setCssVar(cssVars.spacingMd, '12px', cardEl); // or on a specific element
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
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`. They use `getComputedStyle`/`style.setProperty`, so call them in the browser only, and the TS output needs the `DOM` lib in your `tsconfig`.
|
|
342
|
+
|
|
297
343
|
---
|
|
298
344
|
|
|
299
345
|
## Programmatic API
|
|
@@ -303,20 +349,27 @@ import { generate } from 'css-typed-vars';
|
|
|
303
349
|
|
|
304
350
|
await generate({
|
|
305
351
|
input: 'src/styles/**/*.{css,scss}',
|
|
306
|
-
output: 'src/cssVars.ts', // or 'src/cssVars.js' →
|
|
352
|
+
output: 'src/cssVars.ts', // or 'src/cssVars.js' → .js + .d.ts, or 'tokens.json' → design tokens
|
|
307
353
|
exclude: ['**/vendor/**'],
|
|
308
354
|
prefix: 'theme',
|
|
309
355
|
naming: 'snake', // 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal'
|
|
310
356
|
selectors: ['.dark', '[data-theme="dark"]'],
|
|
357
|
+
themes: { dark: '[data-theme="dark"]' }, // per-theme values as @theme JSDoc tags
|
|
358
|
+
group: false, // nest keys by first hyphen segment, e.g. cssVars.color.primary
|
|
359
|
+
helpers: false, // also emit getCssVar/setCssVar runtime helpers
|
|
360
|
+
prune: false, // drop vars with no detected cssVars.<key> usage (requires `usage`)
|
|
361
|
+
usage: 'src/**/*.{ts,tsx}',
|
|
311
362
|
});
|
|
312
363
|
```
|
|
313
364
|
|
|
314
365
|
`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.
|
|
315
366
|
|
|
367
|
+
`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.
|
|
368
|
+
|
|
316
369
|
Lower-level exports:
|
|
317
370
|
|
|
318
371
|
```ts
|
|
319
|
-
import { parseVarNames, parseVarDeclarations, generateCode, scanVarNames, scanVarDeclarations } from 'css-typed-vars';
|
|
372
|
+
import { parseVarNames, parseVarDeclarations, parsePropertyRules, parseThemedDeclarations, generateCode, generateTokens, scanVarNames, scanVarDeclarations, scanCss, scanUsedKeys } from 'css-typed-vars';
|
|
320
373
|
```
|
|
321
374
|
|
|
322
375
|
## Supported formats
|
|
@@ -327,7 +380,101 @@ import { parseVarNames, parseVarDeclarations, generateCode, scanVarNames, scanVa
|
|
|
327
380
|
| SCSS | `.scss` |
|
|
328
381
|
| Less | `.less` |
|
|
329
382
|
|
|
330
|
-
By default, variables are scanned from `:root {}` blocks (including attribute selectors like `:root[data-theme="dark"]`). Use the `selectors` option to also pick up variables from other selectors such as `.dark` or `[data-theme="dark"]`.
|
|
383
|
+
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"]`.
|
|
384
|
+
|
|
385
|
+
## Theme values
|
|
386
|
+
|
|
387
|
+
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:
|
|
388
|
+
|
|
389
|
+
```js
|
|
390
|
+
// css-typed-vars.config.js
|
|
391
|
+
export default {
|
|
392
|
+
input: 'src/**/*.css',
|
|
393
|
+
output: 'src/cssVars.ts',
|
|
394
|
+
themes: {
|
|
395
|
+
dark: '[data-theme="dark"]', // a selector...
|
|
396
|
+
contrast: '@media (prefers-contrast: more)', // ...or an @media prelude (its :root/@theme blocks are read)
|
|
397
|
+
},
|
|
398
|
+
};
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
```css
|
|
402
|
+
:root { --color-bg: #ffffff; }
|
|
403
|
+
[data-theme="dark"] { --color-bg: #0b0b0f; --glow: 0 0 8px #7c3aed; }
|
|
404
|
+
@media (prefers-contrast: more) { :root { --color-bg: #000000; } }
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
export const cssVars = {
|
|
409
|
+
/** @default #ffffff */
|
|
410
|
+
/** @theme dark #0b0b0f */
|
|
411
|
+
/** @theme contrast #000000 */
|
|
412
|
+
colorBg: 'var(--color-bg)',
|
|
413
|
+
/** @theme dark 0 0 8px #7c3aed */
|
|
414
|
+
glow: 'var(--glow)',
|
|
415
|
+
} as const;
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
- Theme blocks are kept out of the default value, so `@default` always shows the base (`:root`/`@theme`/`selectors`) value.
|
|
419
|
+
- A variable declared only inside a theme (`--glow` above) is still generated, with just its theme tags.
|
|
420
|
+
- An `@media` theme matches by its prelude, ignoring whitespace and case. Other `@media` blocks are read as before.
|
|
421
|
+
- On the CLI, pass `--theme <name>=<selector>` (repeatable): `--theme dark='[data-theme="dark"]'`.
|
|
422
|
+
|
|
423
|
+
## Design tokens (DTCG)
|
|
424
|
+
|
|
425
|
+
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:
|
|
426
|
+
|
|
427
|
+
```sh
|
|
428
|
+
npx css-typed-vars --input "src/**/*.css" --output tokens/tokens.json --group
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
```json
|
|
432
|
+
{
|
|
433
|
+
"color": {
|
|
434
|
+
"link": { "$value": "{color.primary}" },
|
|
435
|
+
"primary": { "$value": "#3b82f6", "$type": "color" }
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
- Keys follow `naming`/`prefix`/`group`, like the generated object.
|
|
441
|
+
- `$value` is the declared value (or the `@property` `initial-value`); `$type` is inferred from the `@property` syntax (`<color>`, `<length>`, `<number>`, `<time>`, ...).
|
|
442
|
+
- A value that's exactly `var(--other)` becomes an alias (`{color.primary}`) when `--other` is in the file.
|
|
443
|
+
- [Theme values](#theme-values) go under `$extensions["css-typed-vars"].themes`.
|
|
444
|
+
- The plugin can write the file too: `cssTypedVars({ input, tokens: 'tokens/tokens.json' })`.
|
|
445
|
+
|
|
446
|
+
See the [Design tokens guide](https://stanislavkozachenko.github.io/css-typed-vars/guide/design-tokens) for details.
|
|
447
|
+
|
|
448
|
+
## @property syntax typing
|
|
449
|
+
|
|
450
|
+
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:
|
|
451
|
+
|
|
452
|
+
```css
|
|
453
|
+
@property --theme-mode {
|
|
454
|
+
syntax: "light | dark | system";
|
|
455
|
+
inherits: true;
|
|
456
|
+
initial-value: light;
|
|
457
|
+
}
|
|
458
|
+
:root {
|
|
459
|
+
--theme-mode: light;
|
|
460
|
+
}
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
```ts
|
|
464
|
+
export const cssVars = {
|
|
465
|
+
/** @default light */
|
|
466
|
+
/** @syntax light | dark | system */
|
|
467
|
+
themeMode: 'var(--theme-mode)',
|
|
468
|
+
} as const;
|
|
469
|
+
|
|
470
|
+
export type CssVarName = keyof typeof cssVars;
|
|
471
|
+
|
|
472
|
+
export type ThemeModeSyntax = 'light' | 'dark' | 'system';
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
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)`.
|
|
476
|
+
|
|
477
|
+
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.
|
|
331
478
|
|
|
332
479
|
## Contributing
|
|
333
480
|
|