css-typed-vars 0.8.0 → 0.10.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 +17 -7
- package/dist/{chunk-32V3V6WT.js → chunk-NZUQZYNM.js} +14 -6
- package/dist/{chunk-YDXCJQPL.js → chunk-O7MEOCHS.js} +12 -7
- package/dist/{chunk-5HPTHW7S.js → chunk-XCAFJREQ.js} +53 -8
- package/dist/cli.cjs +196 -44
- package/dist/cli.js +155 -27
- package/dist/esbuild.cjs +62 -13
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +44 -15
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +4 -2
- package/dist/rollup.cjs +62 -13
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +62 -13
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +62 -13
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +62 -13
- package/dist/webpack.js +2 -2
- package/package.json +16 -2
package/README.md
CHANGED
|
@@ -45,6 +45,8 @@ Each generated key also carries a `/** @default ... */` comment with the propert
|
|
|
45
45
|
npm install -D css-typed-vars
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
+
Requires Node.js 20.19+ or 22.12+.
|
|
49
|
+
|
|
48
50
|
## Two approaches
|
|
49
51
|
|
|
50
52
|
| | CLI | Plugin |
|
|
@@ -128,6 +130,7 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
128
130
|
|
|
129
131
|
| Flag | Description |
|
|
130
132
|
|------|-------------|
|
|
133
|
+
| `--config` | Config file to use instead of looking one up in the working directory — see [Config file](#config-file) |
|
|
131
134
|
| `--input` | Glob pattern for CSS/SCSS/Less files (repeatable: `--input "src/**/*.css" --input "lib/**/*.css"`) |
|
|
132
135
|
| `--output` | Output file. `.ts` → TypeScript, `.js` → JavaScript + `.d.ts` alongside, `.json` → [design tokens](#design-tokens-dtcg) |
|
|
133
136
|
| `--exclude` | Glob pattern for files to exclude (repeatable: `--exclude "**/a/**" --exclude "**/b/**"`) |
|
|
@@ -143,15 +146,16 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
143
146
|
| `--refs-ignore` | Name or `--prefix-*` pattern for `--lint-refs` to skip (repeatable) |
|
|
144
147
|
| `--usage` | Glob of source files to scan for `cssVars` usage, for `--prune`/`--prune-check` (repeatable) |
|
|
145
148
|
| `--usage-exclude` | Glob of source files to exclude from the usage scan (repeatable) |
|
|
146
|
-
| `--watch` | Watch
|
|
149
|
+
| `--watch` | Watch the `input` files (and, with `--prune`, the `--usage` files) and regenerate on change |
|
|
147
150
|
| `--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`. |
|
|
148
151
|
| `--version`, `-v` | Print the version number and exit |
|
|
152
|
+
| `--help`, `-h` | Print all options and exit. An unknown option or a stray argument (such as an unquoted glob the shell expanded) is an error |
|
|
149
153
|
|
|
150
154
|
### Pruning unused variables
|
|
151
155
|
|
|
152
|
-
`--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
|
|
156
|
+
`--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 limitations worth knowing:
|
|
153
157
|
|
|
154
|
-
-
|
|
158
|
+
- 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`).
|
|
155
159
|
- A match inside a comment or string still counts as "used".
|
|
156
160
|
|
|
157
161
|
When in doubt, use `--prune-check` in CI to review what it would remove before turning on `--prune` for real.
|
|
@@ -185,13 +189,19 @@ The CLI looks for a config file in the current working directory (in order):
|
|
|
185
189
|
|
|
186
190
|
1. `css-typed-vars.config.js`
|
|
187
191
|
2. `css-typed-vars.config.mjs`
|
|
188
|
-
3. `css-typed-vars.config.
|
|
192
|
+
3. `css-typed-vars.config.cjs`
|
|
193
|
+
4. `css-typed-vars.config.ts` / `.mts` — needs a Node.js version that strips types natively (22.18+ or 23.6+)
|
|
194
|
+
5. `css-typed-vars.config.json`
|
|
195
|
+
|
|
196
|
+
Or point to one explicitly with `--config <file>`.
|
|
189
197
|
|
|
190
|
-
All options are supported in the config file:
|
|
198
|
+
All options are supported in the config file. Wrap it in `defineConfig` for type checking and editor autocompletion (it also accepts an array, see below):
|
|
191
199
|
|
|
192
200
|
```js
|
|
193
201
|
// css-typed-vars.config.js
|
|
194
|
-
|
|
202
|
+
import { defineConfig } from 'css-typed-vars';
|
|
203
|
+
|
|
204
|
+
export default defineConfig({
|
|
195
205
|
input: 'src/styles/**/*.{css,scss}',
|
|
196
206
|
output: 'src/cssVars.ts',
|
|
197
207
|
exclude: ['**/vendor/**', '**/node_modules/**'],
|
|
@@ -204,7 +214,7 @@ export default {
|
|
|
204
214
|
refsIgnore: ['--tw-*'],
|
|
205
215
|
prune: false,
|
|
206
216
|
usage: 'src/**/*.{ts,tsx}',
|
|
207
|
-
};
|
|
217
|
+
});
|
|
208
218
|
```
|
|
209
219
|
|
|
210
220
|
#### Multiple targets
|
|
@@ -2,19 +2,22 @@ import {
|
|
|
2
2
|
__dirname,
|
|
3
3
|
assertPruneCompatible,
|
|
4
4
|
collectVarNames,
|
|
5
|
+
createFileMatcher,
|
|
5
6
|
generateDeclaration,
|
|
6
7
|
generateJs,
|
|
7
8
|
generateTokens,
|
|
9
|
+
globBaseDirs,
|
|
8
10
|
partitionUnused,
|
|
9
11
|
scanCss,
|
|
10
12
|
scanUsage,
|
|
11
|
-
warnOnCollisions
|
|
12
|
-
|
|
13
|
+
warnOnCollisions,
|
|
14
|
+
writeOutput
|
|
15
|
+
} from "./chunk-XCAFJREQ.js";
|
|
13
16
|
|
|
14
17
|
// src/unplugin.ts
|
|
15
18
|
import { createUnplugin } from "unplugin";
|
|
16
|
-
import { writeFile } from "fs/promises";
|
|
17
19
|
import { join, resolve } from "path";
|
|
20
|
+
var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
18
21
|
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
19
22
|
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
20
23
|
function getDtsPath(options) {
|
|
@@ -77,7 +80,7 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
77
80
|
writeQueue = writeQueue.then(async () => {
|
|
78
81
|
for (const [path, content] of files) {
|
|
79
82
|
if (myGeneration !== generation) return;
|
|
80
|
-
await
|
|
83
|
+
await writeOutput(path, content);
|
|
81
84
|
}
|
|
82
85
|
}).catch(console.error);
|
|
83
86
|
await writeQueue;
|
|
@@ -89,8 +92,12 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
89
92
|
server.ws.send({ type: "full-reload" });
|
|
90
93
|
}
|
|
91
94
|
};
|
|
95
|
+
const isInput = createFileMatcher(options.input, options.exclude);
|
|
96
|
+
const isUsage = options.prune ? createFileMatcher(options.usage, options.usageExclude) : () => false;
|
|
97
|
+
const outputs = new Set([getDtsPath(options), options.tokens && resolve(process.cwd(), options.tokens)].filter((path) => !!path).map((path) => resolve(path)));
|
|
92
98
|
const handle = (file) => {
|
|
93
|
-
if (
|
|
99
|
+
if (outputs.has(resolve(file))) return;
|
|
100
|
+
if (!isInput(file) && !isUsage(file)) return;
|
|
94
101
|
if (debounceTimer) clearTimeout(debounceTimer);
|
|
95
102
|
debounceTimer = setTimeout(() => {
|
|
96
103
|
debounceTimer = null;
|
|
@@ -100,6 +107,7 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
100
107
|
server.watcher.on("change", handle);
|
|
101
108
|
server.watcher.on("add", handle);
|
|
102
109
|
server.watcher.on("unlink", handle);
|
|
110
|
+
server.watcher.add(globBaseDirs([...toArray(options.input), ...options.prune ? toArray(options.usage) : []]));
|
|
103
111
|
}
|
|
104
112
|
},
|
|
105
113
|
async buildStart() {
|
|
@@ -107,7 +115,7 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
107
115
|
const scanned = await cachedScan;
|
|
108
116
|
const names = await resolveNames(scanned);
|
|
109
117
|
for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
|
|
110
|
-
await
|
|
118
|
+
await writeOutput(path, content);
|
|
111
119
|
}
|
|
112
120
|
},
|
|
113
121
|
resolveId(id) {
|
|
@@ -9,12 +9,16 @@ import {
|
|
|
9
9
|
scanCss,
|
|
10
10
|
scanReferences,
|
|
11
11
|
scanUsage,
|
|
12
|
-
warnOnCollisions
|
|
13
|
-
|
|
12
|
+
warnOnCollisions,
|
|
13
|
+
writeOutput
|
|
14
|
+
} from "./chunk-XCAFJREQ.js";
|
|
14
15
|
|
|
15
16
|
// src/index.ts
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
17
|
+
import { readFile } from "fs/promises";
|
|
18
|
+
import { resolve } from "path";
|
|
19
|
+
function defineConfig(config) {
|
|
20
|
+
return config;
|
|
21
|
+
}
|
|
18
22
|
async function findUnusedVars(options) {
|
|
19
23
|
if (!options.usage) throw new Error('css-typed-vars: findUnusedVars requires "usage" (glob(s) of source files to scan).');
|
|
20
24
|
const [scanned, usage] = await Promise.all([
|
|
@@ -74,11 +78,11 @@ async function computeOutputs(options) {
|
|
|
74
78
|
}
|
|
75
79
|
async function generate(options) {
|
|
76
80
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
77
|
-
await
|
|
78
|
-
await writeFile(outPath, content, "utf8");
|
|
81
|
+
let written = await writeOutput(outPath, content);
|
|
79
82
|
if (dtsPath && dtsContent !== void 0) {
|
|
80
|
-
await
|
|
83
|
+
if (await writeOutput(dtsPath, dtsContent)) written = true;
|
|
81
84
|
}
|
|
85
|
+
return written;
|
|
82
86
|
}
|
|
83
87
|
async function checkGenerated(options) {
|
|
84
88
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
@@ -92,6 +96,7 @@ async function checkGenerated(options) {
|
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
export {
|
|
99
|
+
defineConfig,
|
|
95
100
|
findUnusedVars,
|
|
96
101
|
findUndefinedRefs,
|
|
97
102
|
generate,
|
|
@@ -5,6 +5,17 @@ var getFilename = () => fileURLToPath(import.meta.url);
|
|
|
5
5
|
var getDirname = () => path.dirname(getFilename());
|
|
6
6
|
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
7
|
|
|
8
|
+
// src/write.ts
|
|
9
|
+
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
10
|
+
import { dirname } from "path";
|
|
11
|
+
async function writeOutput(path2, content) {
|
|
12
|
+
const current = await readFile(path2, "utf8").catch(() => null);
|
|
13
|
+
if (current === content) return false;
|
|
14
|
+
await mkdir(dirname(path2), { recursive: true });
|
|
15
|
+
await writeFile(path2, content, "utf8");
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
8
19
|
// src/parser.ts
|
|
9
20
|
function stepQuote(text, i, quote) {
|
|
10
21
|
const char = text[i];
|
|
@@ -313,11 +324,31 @@ function parseDeclaredNames(css) {
|
|
|
313
324
|
}
|
|
314
325
|
|
|
315
326
|
// src/scanner.ts
|
|
316
|
-
import { readFile } from "fs/promises";
|
|
327
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
328
|
+
import { dirname as dirname2, relative, resolve } from "path";
|
|
317
329
|
import fg from "fast-glob";
|
|
330
|
+
import picomatch from "picomatch";
|
|
318
331
|
function normalizePatterns(patterns) {
|
|
319
332
|
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
320
333
|
}
|
|
334
|
+
function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
335
|
+
const compile = (globs) => picomatch(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
|
|
336
|
+
const include = compile(patterns);
|
|
337
|
+
const ignore = exclude ? compile(exclude) : () => false;
|
|
338
|
+
return (file) => {
|
|
339
|
+
const absolute = resolve(cwd, file).replace(/\\/g, "/");
|
|
340
|
+
const fromCwd = relative(cwd, absolute).replace(/\\/g, "/");
|
|
341
|
+
const matches = (test) => test(fromCwd) || test(absolute);
|
|
342
|
+
return matches(include) && !matches(ignore);
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function globBaseDirs(patterns, cwd = process.cwd()) {
|
|
346
|
+
const dirs = normalizePatterns(patterns).map((pattern) => {
|
|
347
|
+
const { base, isGlob } = picomatch.scan(pattern);
|
|
348
|
+
return resolve(cwd, isGlob ? base || "." : dirname2(pattern));
|
|
349
|
+
});
|
|
350
|
+
return [...new Set(dirs)];
|
|
351
|
+
}
|
|
321
352
|
function collectVarNames(scanned) {
|
|
322
353
|
const names = new Set(scanned.declarations.keys());
|
|
323
354
|
for (const values of scanned.themes.values()) {
|
|
@@ -333,7 +364,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
333
364
|
files.map(async (file) => {
|
|
334
365
|
let css;
|
|
335
366
|
try {
|
|
336
|
-
css = await
|
|
367
|
+
css = await readFile2(file, "utf8");
|
|
337
368
|
} catch (err) {
|
|
338
369
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
339
370
|
return null;
|
|
@@ -364,6 +395,17 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
364
395
|
return [...declarations.keys()].sort();
|
|
365
396
|
}
|
|
366
397
|
var ROOT_RE = /\bcssVars\b/g;
|
|
398
|
+
var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
|
|
399
|
+
var ALIAS_RE = /(?:^|,)\s*(?:type\s+)?cssVars\s+as\s+([A-Za-z_$][\w$]*)\s*(?=,|$)/g;
|
|
400
|
+
function rootPattern(text) {
|
|
401
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
402
|
+
for (const [, specifiers] of text.matchAll(IMPORT_RE)) {
|
|
403
|
+
for (const [, alias] of specifiers.matchAll(ALIAS_RE)) aliases.add(alias);
|
|
404
|
+
}
|
|
405
|
+
if (aliases.size === 0) return ROOT_RE;
|
|
406
|
+
const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
|
|
407
|
+
return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
|
|
408
|
+
}
|
|
367
409
|
var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
|
|
368
410
|
function readAccess(text, from) {
|
|
369
411
|
ACCESS_RE.lastIndex = from;
|
|
@@ -371,7 +413,7 @@ function readAccess(text, from) {
|
|
|
371
413
|
return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
|
|
372
414
|
}
|
|
373
415
|
function extractUsage(text, into) {
|
|
374
|
-
for (const root of text.matchAll(
|
|
416
|
+
for (const root of text.matchAll(rootPattern(text))) {
|
|
375
417
|
const first = readAccess(text, root.index + root[0].length);
|
|
376
418
|
if (!first) continue;
|
|
377
419
|
into.keys.add(first.key);
|
|
@@ -388,7 +430,7 @@ async function scanUsage(patterns, exclude) {
|
|
|
388
430
|
const texts = await Promise.all(
|
|
389
431
|
files.map(async (file) => {
|
|
390
432
|
try {
|
|
391
|
-
return await
|
|
433
|
+
return await readFile2(file, "utf8");
|
|
392
434
|
} catch (err) {
|
|
393
435
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
394
436
|
return "";
|
|
@@ -405,7 +447,7 @@ async function scanReferences(patterns, exclude) {
|
|
|
405
447
|
const perFile = await Promise.all(
|
|
406
448
|
files.map(async (file) => {
|
|
407
449
|
try {
|
|
408
|
-
return { file, css: await
|
|
450
|
+
return { file, css: await readFile2(file, "utf8") };
|
|
409
451
|
} catch (err) {
|
|
410
452
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
411
453
|
return null;
|
|
@@ -748,19 +790,19 @@ function generateTokens(varNames, options = {}) {
|
|
|
748
790
|
return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
|
|
749
791
|
});
|
|
750
792
|
const paths = new Map(tokens.map(({ meta, path: path2 }) => [meta.name, path2]));
|
|
751
|
-
const
|
|
793
|
+
const resolve2 = (value) => {
|
|
752
794
|
const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
|
|
753
795
|
return target ? `{${target.join(".")}}` : value;
|
|
754
796
|
};
|
|
755
797
|
const root = {};
|
|
756
798
|
for (const { meta, value, path: path2 } of tokens) {
|
|
757
|
-
const token = { $value:
|
|
799
|
+
const token = { $value: resolve2(value) };
|
|
758
800
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
759
801
|
if (type) token.$type = type;
|
|
760
802
|
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
761
803
|
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
762
804
|
if (meta.themes) {
|
|
763
|
-
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme,
|
|
805
|
+
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
|
|
764
806
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
765
807
|
}
|
|
766
808
|
const [key, leaf] = path2;
|
|
@@ -796,12 +838,15 @@ function partitionUnused(names, usage, options) {
|
|
|
796
838
|
|
|
797
839
|
export {
|
|
798
840
|
__dirname,
|
|
841
|
+
writeOutput,
|
|
799
842
|
parseVarNames,
|
|
800
843
|
parseVarDeclarations,
|
|
801
844
|
parseThemedDeclarations,
|
|
802
845
|
parsePropertyRules,
|
|
803
846
|
parseVarReferences,
|
|
804
847
|
parseDeclaredNames,
|
|
848
|
+
createFileMatcher,
|
|
849
|
+
globBaseDirs,
|
|
805
850
|
collectVarNames,
|
|
806
851
|
scanCss,
|
|
807
852
|
scanVarDeclarations,
|