css-typed-vars 0.8.0 → 0.9.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 +13 -5
- package/dist/{chunk-32V3V6WT.js → chunk-66NEMXYL.js} +11 -6
- package/dist/{chunk-5HPTHW7S.js → chunk-DHBPPGOE.js} +33 -7
- package/dist/{chunk-YDXCJQPL.js → chunk-TB5PSGEF.js} +12 -7
- package/dist/cli.cjs +184 -43
- package/dist/cli.js +155 -27
- package/dist/esbuild.cjs +41 -12
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +32 -14
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +4 -2
- package/dist/rollup.cjs +41 -12
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +41 -12
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +41 -12
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +41 -12
- package/dist/webpack.js +2 -2
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -128,6 +128,7 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
128
128
|
|
|
129
129
|
| Flag | Description |
|
|
130
130
|
|------|-------------|
|
|
131
|
+
| `--config` | Config file to use instead of looking one up in the working directory — see [Config file](#config-file) |
|
|
131
132
|
| `--input` | Glob pattern for CSS/SCSS/Less files (repeatable: `--input "src/**/*.css" --input "lib/**/*.css"`) |
|
|
132
133
|
| `--output` | Output file. `.ts` → TypeScript, `.js` → JavaScript + `.d.ts` alongside, `.json` → [design tokens](#design-tokens-dtcg) |
|
|
133
134
|
| `--exclude` | Glob pattern for files to exclude (repeatable: `--exclude "**/a/**" --exclude "**/b/**"`) |
|
|
@@ -143,9 +144,10 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
143
144
|
| `--refs-ignore` | Name or `--prefix-*` pattern for `--lint-refs` to skip (repeatable) |
|
|
144
145
|
| `--usage` | Glob of source files to scan for `cssVars` usage, for `--prune`/`--prune-check` (repeatable) |
|
|
145
146
|
| `--usage-exclude` | Glob of source files to exclude from the usage scan (repeatable) |
|
|
146
|
-
| `--watch` | Watch
|
|
147
|
+
| `--watch` | Watch the `input` files (and, with `--prune`, the `--usage` files) and regenerate on change |
|
|
147
148
|
| `--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
149
|
| `--version`, `-v` | Print the version number and exit |
|
|
150
|
+
| `--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
151
|
|
|
150
152
|
### Pruning unused variables
|
|
151
153
|
|
|
@@ -185,13 +187,19 @@ The CLI looks for a config file in the current working directory (in order):
|
|
|
185
187
|
|
|
186
188
|
1. `css-typed-vars.config.js`
|
|
187
189
|
2. `css-typed-vars.config.mjs`
|
|
188
|
-
3. `css-typed-vars.config.
|
|
190
|
+
3. `css-typed-vars.config.cjs`
|
|
191
|
+
4. `css-typed-vars.config.ts` / `.mts` — needs a Node.js version that strips types natively (22.18+ or 23.6+)
|
|
192
|
+
5. `css-typed-vars.config.json`
|
|
189
193
|
|
|
190
|
-
|
|
194
|
+
Or point to one explicitly with `--config <file>`.
|
|
195
|
+
|
|
196
|
+
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
197
|
|
|
192
198
|
```js
|
|
193
199
|
// css-typed-vars.config.js
|
|
194
|
-
|
|
200
|
+
import { defineConfig } from 'css-typed-vars';
|
|
201
|
+
|
|
202
|
+
export default defineConfig({
|
|
195
203
|
input: 'src/styles/**/*.{css,scss}',
|
|
196
204
|
output: 'src/cssVars.ts',
|
|
197
205
|
exclude: ['**/vendor/**', '**/node_modules/**'],
|
|
@@ -204,7 +212,7 @@ export default {
|
|
|
204
212
|
refsIgnore: ['--tw-*'],
|
|
205
213
|
prune: false,
|
|
206
214
|
usage: 'src/**/*.{ts,tsx}',
|
|
207
|
-
};
|
|
215
|
+
});
|
|
208
216
|
```
|
|
209
217
|
|
|
210
218
|
#### Multiple targets
|
|
@@ -2,18 +2,19 @@ import {
|
|
|
2
2
|
__dirname,
|
|
3
3
|
assertPruneCompatible,
|
|
4
4
|
collectVarNames,
|
|
5
|
+
createFileMatcher,
|
|
5
6
|
generateDeclaration,
|
|
6
7
|
generateJs,
|
|
7
8
|
generateTokens,
|
|
8
9
|
partitionUnused,
|
|
9
10
|
scanCss,
|
|
10
11
|
scanUsage,
|
|
11
|
-
warnOnCollisions
|
|
12
|
-
|
|
12
|
+
warnOnCollisions,
|
|
13
|
+
writeOutput
|
|
14
|
+
} from "./chunk-DHBPPGOE.js";
|
|
13
15
|
|
|
14
16
|
// src/unplugin.ts
|
|
15
17
|
import { createUnplugin } from "unplugin";
|
|
16
|
-
import { writeFile } from "fs/promises";
|
|
17
18
|
import { join, resolve } from "path";
|
|
18
19
|
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
19
20
|
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
@@ -77,7 +78,7 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
77
78
|
writeQueue = writeQueue.then(async () => {
|
|
78
79
|
for (const [path, content] of files) {
|
|
79
80
|
if (myGeneration !== generation) return;
|
|
80
|
-
await
|
|
81
|
+
await writeOutput(path, content);
|
|
81
82
|
}
|
|
82
83
|
}).catch(console.error);
|
|
83
84
|
await writeQueue;
|
|
@@ -89,8 +90,12 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
89
90
|
server.ws.send({ type: "full-reload" });
|
|
90
91
|
}
|
|
91
92
|
};
|
|
93
|
+
const isInput = createFileMatcher(options.input, options.exclude);
|
|
94
|
+
const isUsage = options.prune ? createFileMatcher(options.usage, options.usageExclude) : () => false;
|
|
95
|
+
const outputs = new Set([getDtsPath(options), options.tokens && resolve(process.cwd(), options.tokens)].filter((path) => !!path).map((path) => resolve(path)));
|
|
92
96
|
const handle = (file) => {
|
|
93
|
-
if (
|
|
97
|
+
if (outputs.has(resolve(file))) return;
|
|
98
|
+
if (!isInput(file) && !isUsage(file)) return;
|
|
94
99
|
if (debounceTimer) clearTimeout(debounceTimer);
|
|
95
100
|
debounceTimer = setTimeout(() => {
|
|
96
101
|
debounceTimer = null;
|
|
@@ -107,7 +112,7 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
107
112
|
const scanned = await cachedScan;
|
|
108
113
|
const names = await resolveNames(scanned);
|
|
109
114
|
for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
|
|
110
|
-
await
|
|
115
|
+
await writeOutput(path, content);
|
|
111
116
|
}
|
|
112
117
|
},
|
|
113
118
|
resolveId(id) {
|
|
@@ -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,24 @@ 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 { 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
|
+
}
|
|
321
345
|
function collectVarNames(scanned) {
|
|
322
346
|
const names = new Set(scanned.declarations.keys());
|
|
323
347
|
for (const values of scanned.themes.values()) {
|
|
@@ -333,7 +357,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
333
357
|
files.map(async (file) => {
|
|
334
358
|
let css;
|
|
335
359
|
try {
|
|
336
|
-
css = await
|
|
360
|
+
css = await readFile2(file, "utf8");
|
|
337
361
|
} catch (err) {
|
|
338
362
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
339
363
|
return null;
|
|
@@ -388,7 +412,7 @@ async function scanUsage(patterns, exclude) {
|
|
|
388
412
|
const texts = await Promise.all(
|
|
389
413
|
files.map(async (file) => {
|
|
390
414
|
try {
|
|
391
|
-
return await
|
|
415
|
+
return await readFile2(file, "utf8");
|
|
392
416
|
} catch (err) {
|
|
393
417
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
394
418
|
return "";
|
|
@@ -405,7 +429,7 @@ async function scanReferences(patterns, exclude) {
|
|
|
405
429
|
const perFile = await Promise.all(
|
|
406
430
|
files.map(async (file) => {
|
|
407
431
|
try {
|
|
408
|
-
return { file, css: await
|
|
432
|
+
return { file, css: await readFile2(file, "utf8") };
|
|
409
433
|
} catch (err) {
|
|
410
434
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
411
435
|
return null;
|
|
@@ -748,19 +772,19 @@ function generateTokens(varNames, options = {}) {
|
|
|
748
772
|
return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
|
|
749
773
|
});
|
|
750
774
|
const paths = new Map(tokens.map(({ meta, path: path2 }) => [meta.name, path2]));
|
|
751
|
-
const
|
|
775
|
+
const resolve2 = (value) => {
|
|
752
776
|
const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
|
|
753
777
|
return target ? `{${target.join(".")}}` : value;
|
|
754
778
|
};
|
|
755
779
|
const root = {};
|
|
756
780
|
for (const { meta, value, path: path2 } of tokens) {
|
|
757
|
-
const token = { $value:
|
|
781
|
+
const token = { $value: resolve2(value) };
|
|
758
782
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
759
783
|
if (type) token.$type = type;
|
|
760
784
|
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
761
785
|
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
762
786
|
if (meta.themes) {
|
|
763
|
-
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme,
|
|
787
|
+
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
|
|
764
788
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
765
789
|
}
|
|
766
790
|
const [key, leaf] = path2;
|
|
@@ -796,12 +820,14 @@ function partitionUnused(names, usage, options) {
|
|
|
796
820
|
|
|
797
821
|
export {
|
|
798
822
|
__dirname,
|
|
823
|
+
writeOutput,
|
|
799
824
|
parseVarNames,
|
|
800
825
|
parseVarDeclarations,
|
|
801
826
|
parseThemedDeclarations,
|
|
802
827
|
parsePropertyRules,
|
|
803
828
|
parseVarReferences,
|
|
804
829
|
parseDeclaredNames,
|
|
830
|
+
createFileMatcher,
|
|
805
831
|
collectVarNames,
|
|
806
832
|
scanCss,
|
|
807
833
|
scanVarDeclarations,
|
|
@@ -9,12 +9,16 @@ import {
|
|
|
9
9
|
scanCss,
|
|
10
10
|
scanReferences,
|
|
11
11
|
scanUsage,
|
|
12
|
-
warnOnCollisions
|
|
13
|
-
|
|
12
|
+
warnOnCollisions,
|
|
13
|
+
writeOutput
|
|
14
|
+
} from "./chunk-DHBPPGOE.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,
|
package/dist/cli.cjs
CHANGED
|
@@ -28,18 +28,31 @@ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${_
|
|
|
28
28
|
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
29
29
|
|
|
30
30
|
// src/cli.ts
|
|
31
|
-
var
|
|
32
|
-
var
|
|
31
|
+
var import_promises4 = require("fs/promises");
|
|
32
|
+
var import_node_path4 = require("path");
|
|
33
33
|
var import_node_url = require("url");
|
|
34
34
|
var import_chokidar = require("chokidar");
|
|
35
35
|
|
|
36
36
|
// src/index.ts
|
|
37
|
-
var
|
|
37
|
+
var import_promises3 = require("fs/promises");
|
|
38
|
+
var import_node_path3 = require("path");
|
|
39
|
+
|
|
40
|
+
// src/write.ts
|
|
41
|
+
var import_promises = require("fs/promises");
|
|
38
42
|
var import_node_path = require("path");
|
|
43
|
+
async function writeOutput(path, content) {
|
|
44
|
+
const current = await (0, import_promises.readFile)(path, "utf8").catch(() => null);
|
|
45
|
+
if (current === content) return false;
|
|
46
|
+
await (0, import_promises.mkdir)((0, import_node_path.dirname)(path), { recursive: true });
|
|
47
|
+
await (0, import_promises.writeFile)(path, content, "utf8");
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
39
50
|
|
|
40
51
|
// src/scanner.ts
|
|
41
|
-
var
|
|
52
|
+
var import_promises2 = require("fs/promises");
|
|
53
|
+
var import_node_path2 = require("path");
|
|
42
54
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
55
|
+
var import_picomatch = __toESM(require("picomatch"), 1);
|
|
43
56
|
|
|
44
57
|
// src/parser.ts
|
|
45
58
|
function stepQuote(text, i, quote) {
|
|
@@ -357,7 +370,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
357
370
|
files.map(async (file) => {
|
|
358
371
|
let css;
|
|
359
372
|
try {
|
|
360
|
-
css = await (0,
|
|
373
|
+
css = await (0, import_promises2.readFile)(file, "utf8");
|
|
361
374
|
} catch (err) {
|
|
362
375
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
363
376
|
return null;
|
|
@@ -405,7 +418,7 @@ async function scanUsage(patterns, exclude) {
|
|
|
405
418
|
const texts = await Promise.all(
|
|
406
419
|
files.map(async (file) => {
|
|
407
420
|
try {
|
|
408
|
-
return await (0,
|
|
421
|
+
return await (0, import_promises2.readFile)(file, "utf8");
|
|
409
422
|
} catch (err) {
|
|
410
423
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
411
424
|
return "";
|
|
@@ -422,7 +435,7 @@ async function scanReferences(patterns, exclude) {
|
|
|
422
435
|
const perFile = await Promise.all(
|
|
423
436
|
files.map(async (file) => {
|
|
424
437
|
try {
|
|
425
|
-
return { file, css: await (0,
|
|
438
|
+
return { file, css: await (0, import_promises2.readFile)(file, "utf8") };
|
|
426
439
|
} catch (err) {
|
|
427
440
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
428
441
|
return null;
|
|
@@ -762,19 +775,19 @@ function generateTokens(varNames, options = {}) {
|
|
|
762
775
|
return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
|
|
763
776
|
});
|
|
764
777
|
const paths = new Map(tokens.map(({ meta, path }) => [meta.name, path]));
|
|
765
|
-
const
|
|
778
|
+
const resolve4 = (value) => {
|
|
766
779
|
const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
|
|
767
780
|
return target ? `{${target.join(".")}}` : value;
|
|
768
781
|
};
|
|
769
782
|
const root = {};
|
|
770
783
|
for (const { meta, value, path } of tokens) {
|
|
771
|
-
const token = { $value:
|
|
784
|
+
const token = { $value: resolve4(value) };
|
|
772
785
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
773
786
|
if (type) token.$type = type;
|
|
774
787
|
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
775
788
|
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
776
789
|
if (meta.themes) {
|
|
777
|
-
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme,
|
|
790
|
+
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve4(themeValue)]));
|
|
778
791
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
779
792
|
}
|
|
780
793
|
const [key, leaf] = path;
|
|
@@ -843,7 +856,7 @@ async function computeOutputs(options) {
|
|
|
843
856
|
}
|
|
844
857
|
names = used;
|
|
845
858
|
}
|
|
846
|
-
const outPath = (0,
|
|
859
|
+
const outPath = (0, import_node_path3.resolve)(options.output);
|
|
847
860
|
const generatorOptions = {
|
|
848
861
|
prefix: options.prefix,
|
|
849
862
|
naming: options.naming,
|
|
@@ -868,18 +881,18 @@ async function computeOutputs(options) {
|
|
|
868
881
|
}
|
|
869
882
|
async function generate(options) {
|
|
870
883
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
871
|
-
|
|
872
|
-
await (0, import_promises2.writeFile)(outPath, content, "utf8");
|
|
884
|
+
let written = await writeOutput(outPath, content);
|
|
873
885
|
if (dtsPath && dtsContent !== void 0) {
|
|
874
|
-
|
|
886
|
+
if (await writeOutput(dtsPath, dtsContent)) written = true;
|
|
875
887
|
}
|
|
888
|
+
return written;
|
|
876
889
|
}
|
|
877
890
|
async function checkGenerated(options) {
|
|
878
891
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
879
|
-
const current = await (0,
|
|
892
|
+
const current = await (0, import_promises3.readFile)(outPath, "utf8").catch(() => null);
|
|
880
893
|
if (current !== content) return false;
|
|
881
894
|
if (dtsPath && dtsContent !== void 0) {
|
|
882
|
-
const currentDts = await (0,
|
|
895
|
+
const currentDts = await (0, import_promises3.readFile)(dtsPath, "utf8").catch(() => null);
|
|
883
896
|
if (currentDts !== dtsContent) return false;
|
|
884
897
|
}
|
|
885
898
|
return true;
|
|
@@ -920,33 +933,130 @@ var OPTION_FLAGS = [
|
|
|
920
933
|
"--helpers",
|
|
921
934
|
"--refs-ignore"
|
|
922
935
|
];
|
|
923
|
-
var USAGE = "Usage: css-typed-vars --input <glob> --output <file> [
|
|
936
|
+
var USAGE = "Usage: css-typed-vars --input <glob> --output <file> [options]";
|
|
937
|
+
var VALUE_FLAGS = [
|
|
938
|
+
"--input",
|
|
939
|
+
"--output",
|
|
940
|
+
"--exclude",
|
|
941
|
+
"--prefix",
|
|
942
|
+
"--naming",
|
|
943
|
+
"--selector",
|
|
944
|
+
"--theme",
|
|
945
|
+
"--usage",
|
|
946
|
+
"--usage-exclude",
|
|
947
|
+
"--refs-ignore",
|
|
948
|
+
"--config"
|
|
949
|
+
];
|
|
950
|
+
var BOOLEAN_FLAGS = [
|
|
951
|
+
"--group",
|
|
952
|
+
"--prune",
|
|
953
|
+
"--helpers",
|
|
954
|
+
"--watch",
|
|
955
|
+
"--check",
|
|
956
|
+
"--prune-check",
|
|
957
|
+
"--lint-refs",
|
|
958
|
+
"--version",
|
|
959
|
+
"-v",
|
|
960
|
+
"--help",
|
|
961
|
+
"-h"
|
|
962
|
+
];
|
|
963
|
+
var HELP = `${USAGE}
|
|
964
|
+
|
|
965
|
+
Generates typed constants for the CSS custom properties found in <glob>.
|
|
966
|
+
Options can also be set in css-typed-vars.config.{js,mjs,cjs,ts,mts,json}; flags take precedence.
|
|
967
|
+
|
|
968
|
+
Options:
|
|
969
|
+
--config <file> Config file to use instead of looking one up
|
|
970
|
+
--input <glob> CSS files to scan (repeatable)
|
|
971
|
+
--output <file> .ts, .js/.mjs/.cjs (plus a .d.ts), or .json (design tokens)
|
|
972
|
+
--exclude <glob> Files to skip (repeatable)
|
|
973
|
+
--prefix <prefix> Prefix every generated key
|
|
974
|
+
--naming <convention> camelCase (default), snake, kebab, constant or pascal
|
|
975
|
+
--selector <selector> Also scan this selector's blocks, not just :root/@theme (repeatable)
|
|
976
|
+
--theme <name>=<sel> Theme block, emitted as @theme JSDoc tags (repeatable)
|
|
977
|
+
--group Nest keys by the first hyphen segment (cssVars.color.primary)
|
|
978
|
+
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback
|
|
979
|
+
--prune Drop vars with no cssVars.<key> usage (needs --usage)
|
|
980
|
+
--usage <glob> Source files to scan for cssVars usage (repeatable)
|
|
981
|
+
--usage-exclude <glob> Source files to skip in the usage scan (repeatable)
|
|
982
|
+
|
|
983
|
+
Modes:
|
|
984
|
+
--watch Regenerate when input (or, with --prune, usage) files change
|
|
985
|
+
--check Exit 1 if the output is out of date, without writing
|
|
986
|
+
--prune-check Exit 1 if any var has no cssVars.<key> usage (needs --usage)
|
|
987
|
+
--lint-refs Exit 1 on var() references to undeclared variables
|
|
988
|
+
--refs-ignore <name> Name or --prefix-* pattern for --lint-refs to skip (repeatable)
|
|
989
|
+
--version, -v Print the version
|
|
990
|
+
--help, -h Print this help
|
|
991
|
+
|
|
992
|
+
Docs: https://stanislavkozachenko.github.io/css-typed-vars/`;
|
|
993
|
+
function editDistance(a, b) {
|
|
994
|
+
let row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
995
|
+
for (let i = 1; i <= a.length; i++) {
|
|
996
|
+
const next = [i];
|
|
997
|
+
for (let j = 1; j <= b.length; j++) {
|
|
998
|
+
next[j] = Math.min(row[j] + 1, next[j - 1] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
999
|
+
}
|
|
1000
|
+
row = next;
|
|
1001
|
+
}
|
|
1002
|
+
return row[b.length];
|
|
1003
|
+
}
|
|
1004
|
+
function validateArgs() {
|
|
1005
|
+
for (let i = 0; i < args.length; i++) {
|
|
1006
|
+
const arg = args[i];
|
|
1007
|
+
if (VALUE_FLAGS.includes(arg)) {
|
|
1008
|
+
if (i + 1 >= args.length) throw new CliError(`css-typed-vars: ${arg} requires a value.`);
|
|
1009
|
+
i++;
|
|
1010
|
+
continue;
|
|
1011
|
+
}
|
|
1012
|
+
if (BOOLEAN_FLAGS.includes(arg)) continue;
|
|
1013
|
+
if (arg.startsWith("-")) {
|
|
1014
|
+
const known = [...VALUE_FLAGS, ...BOOLEAN_FLAGS].filter((flag) => flag.startsWith("--"));
|
|
1015
|
+
const closest = known.reduce((best, flag) => editDistance(arg, flag) < editDistance(arg, best) ? flag : best);
|
|
1016
|
+
const hint = editDistance(arg, closest) <= 2 ? ` Did you mean ${closest}?` : "";
|
|
1017
|
+
throw new CliError(`css-typed-vars: unknown option "${arg}".${hint} Run with --help to see all options.`);
|
|
1018
|
+
}
|
|
1019
|
+
throw new CliError(`css-typed-vars: unexpected argument "${arg}". If it's a glob, quote it so the shell doesn't expand it: --input "src/**/*.css".`);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
924
1022
|
var CliError = class extends Error {
|
|
925
1023
|
};
|
|
1024
|
+
var CONFIG_FILES = [
|
|
1025
|
+
"css-typed-vars.config.js",
|
|
1026
|
+
"css-typed-vars.config.mjs",
|
|
1027
|
+
"css-typed-vars.config.cjs",
|
|
1028
|
+
"css-typed-vars.config.ts",
|
|
1029
|
+
"css-typed-vars.config.mts",
|
|
1030
|
+
"css-typed-vars.config.json"
|
|
1031
|
+
];
|
|
1032
|
+
async function importConfig(path) {
|
|
1033
|
+
if (/\.json$/i.test(path)) return JSON.parse(await (0, import_promises4.readFile)(path, "utf8"));
|
|
1034
|
+
const mod = await import((0, import_node_url.pathToFileURL)(path).href);
|
|
1035
|
+
return mod.default ?? {};
|
|
1036
|
+
}
|
|
926
1037
|
async function loadConfig() {
|
|
927
|
-
const
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
];
|
|
932
|
-
for (const file of candidates) {
|
|
933
|
-
const path = (0, import_node_path2.resolve)(process.cwd(), file);
|
|
1038
|
+
const explicit = getArg("--config");
|
|
1039
|
+
if (explicit !== void 0) {
|
|
1040
|
+
const path = (0, import_node_path4.resolve)(process.cwd(), explicit);
|
|
1041
|
+
if (!await exists(path)) throw new CliError(`css-typed-vars: config file "${explicit}" not found.`);
|
|
934
1042
|
try {
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
1043
|
+
return await importConfig(path);
|
|
1044
|
+
} catch (err) {
|
|
1045
|
+
throw new CliError(`css-typed-vars: failed to load config "${explicit}": ${err.message}`);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
for (const file of CONFIG_FILES) {
|
|
1049
|
+
const path = (0, import_node_path4.resolve)(process.cwd(), file);
|
|
1050
|
+
if (!await exists(path)) continue;
|
|
1051
|
+
try {
|
|
1052
|
+
return await importConfig(path);
|
|
942
1053
|
} catch (err) {
|
|
943
|
-
const code = err.code;
|
|
944
|
-
if (code === "ENOENT" || code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") continue;
|
|
945
1054
|
console.warn(`css-typed-vars: failed to load config "${file}": ${err.message}`);
|
|
946
1055
|
}
|
|
947
1056
|
}
|
|
948
1057
|
return {};
|
|
949
1058
|
}
|
|
1059
|
+
var exists = (path) => (0, import_promises4.access)(path).then(() => true, () => false);
|
|
950
1060
|
function applyFlags(config) {
|
|
951
1061
|
const inputArgs = getArgs("--input");
|
|
952
1062
|
const excludeArgs = getArgs("--exclude");
|
|
@@ -1006,7 +1116,7 @@ function validateTarget({ options, tag }, single) {
|
|
|
1006
1116
|
}
|
|
1007
1117
|
if (!input || !output) {
|
|
1008
1118
|
throw new CliError(single ? `${USAGE}
|
|
1009
|
-
Or add a css-typed-vars.config.js file with input and output fields.` : `${tag}: every target needs "input" and "output".`);
|
|
1119
|
+
Or add a css-typed-vars.config.js file with input and output fields. Run with --help to see all options.` : `${tag}: every target needs "input" and "output".`);
|
|
1010
1120
|
}
|
|
1011
1121
|
if (group && prefix) throw new CliError(`${tag}: --group cannot be combined with --prefix.`);
|
|
1012
1122
|
if ((prune || pruneCheckMode) && !usage) {
|
|
@@ -1015,7 +1125,7 @@ Or add a css-typed-vars.config.js file with input and output fields.` : `${tag}:
|
|
|
1015
1125
|
}
|
|
1016
1126
|
async function lintRefs({ options, tag }) {
|
|
1017
1127
|
const refs = await findUndefinedRefs({ input: options.input, exclude: options.exclude, refsIgnore: options.refsIgnore });
|
|
1018
|
-
const format = (ref) => ` ${(0,
|
|
1128
|
+
const format = (ref) => ` ${(0, import_node_path4.relative)(process.cwd(), ref.file).replace(/\\/g, "/")}:${ref.line}:${ref.column} var(${ref.name}${ref.hasFallback ? ", \u2026" : ""})`;
|
|
1019
1129
|
const errors = refs.filter((ref) => !ref.hasFallback);
|
|
1020
1130
|
const warnings = refs.filter((ref) => ref.hasFallback);
|
|
1021
1131
|
if (warnings.length > 0) {
|
|
@@ -1048,22 +1158,53 @@ async function check({ options, tag }) {
|
|
|
1048
1158
|
return true;
|
|
1049
1159
|
}
|
|
1050
1160
|
async function run({ options }) {
|
|
1051
|
-
await generate({ ...options, input: options.input, output: options.output });
|
|
1052
|
-
console.log(
|
|
1161
|
+
const written = await generate({ ...options, input: options.input, output: options.output });
|
|
1162
|
+
console.log(`${written ? "Generated" : "Up to date"} \u2192 ${options.output}`);
|
|
1053
1163
|
}
|
|
1164
|
+
var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
1054
1165
|
function watchTarget(target) {
|
|
1055
|
-
const { input, exclude } = target.options;
|
|
1056
|
-
const
|
|
1166
|
+
const { input, exclude, output, prune, usage, usageExclude } = target.options;
|
|
1167
|
+
const jsExt = /\.(m|c)?js$/i.exec(output);
|
|
1168
|
+
const outputs = new Set([output, ...jsExt ? [output.slice(0, -jsExt[0].length) + ".d.ts"] : []].map((path) => (0, import_node_path4.resolve)(path)));
|
|
1169
|
+
let timer = null;
|
|
1170
|
+
let running = false;
|
|
1171
|
+
let pending = false;
|
|
1172
|
+
const regenerate = async () => {
|
|
1173
|
+
if (running) {
|
|
1174
|
+
pending = true;
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
running = true;
|
|
1178
|
+
await run(target).catch(console.error);
|
|
1179
|
+
running = false;
|
|
1180
|
+
if (pending) {
|
|
1181
|
+
pending = false;
|
|
1182
|
+
await regenerate();
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1057
1185
|
const makeHandler = (label) => (file) => {
|
|
1186
|
+
if (outputs.has((0, import_node_path4.resolve)(file))) return;
|
|
1058
1187
|
console.log(`${label}: ${file}`);
|
|
1059
|
-
|
|
1188
|
+
if (timer) clearTimeout(timer);
|
|
1189
|
+
timer = setTimeout(() => {
|
|
1190
|
+
timer = null;
|
|
1191
|
+
void regenerate();
|
|
1192
|
+
}, 100);
|
|
1060
1193
|
};
|
|
1061
|
-
const
|
|
1062
|
-
|
|
1194
|
+
const subscribe = (patterns, ignored) => {
|
|
1195
|
+
(0, import_chokidar.watch)(patterns, { ignoreInitial: true, ...ignored.length > 0 ? { ignored } : {} }).on("change", makeHandler("Changed")).on("add", makeHandler("Added")).on("unlink", makeHandler("Removed"));
|
|
1196
|
+
};
|
|
1197
|
+
subscribe(toArray(input), toArray(exclude));
|
|
1198
|
+
if (prune) subscribe(toArray(usage), toArray(usageExclude));
|
|
1063
1199
|
}
|
|
1064
1200
|
async function main() {
|
|
1201
|
+
validateArgs();
|
|
1202
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
1203
|
+
console.log(HELP);
|
|
1204
|
+
return;
|
|
1205
|
+
}
|
|
1065
1206
|
if (args.includes("--version") || args.includes("-v")) {
|
|
1066
|
-
const pkg = JSON.parse(await (0,
|
|
1207
|
+
const pkg = JSON.parse(await (0, import_promises4.readFile)(new URL("../package.json", importMetaUrl), "utf8"));
|
|
1067
1208
|
console.log(pkg.version);
|
|
1068
1209
|
process.exit(0);
|
|
1069
1210
|
}
|