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/dist/cli.js
CHANGED
|
@@ -4,13 +4,13 @@ import {
|
|
|
4
4
|
findUndefinedRefs,
|
|
5
5
|
findUnusedVars,
|
|
6
6
|
generate
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-TB5PSGEF.js";
|
|
8
8
|
import {
|
|
9
9
|
VALID_NAMINGS
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-DHBPPGOE.js";
|
|
11
11
|
|
|
12
12
|
// src/cli.ts
|
|
13
|
-
import { readFile } from "fs/promises";
|
|
13
|
+
import { access, readFile } from "fs/promises";
|
|
14
14
|
import { relative, resolve } from "path";
|
|
15
15
|
import { pathToFileURL } from "url";
|
|
16
16
|
import { watch } from "chokidar";
|
|
@@ -48,33 +48,130 @@ var OPTION_FLAGS = [
|
|
|
48
48
|
"--helpers",
|
|
49
49
|
"--refs-ignore"
|
|
50
50
|
];
|
|
51
|
-
var USAGE = "Usage: css-typed-vars --input <glob> --output <file> [
|
|
51
|
+
var USAGE = "Usage: css-typed-vars --input <glob> --output <file> [options]";
|
|
52
|
+
var VALUE_FLAGS = [
|
|
53
|
+
"--input",
|
|
54
|
+
"--output",
|
|
55
|
+
"--exclude",
|
|
56
|
+
"--prefix",
|
|
57
|
+
"--naming",
|
|
58
|
+
"--selector",
|
|
59
|
+
"--theme",
|
|
60
|
+
"--usage",
|
|
61
|
+
"--usage-exclude",
|
|
62
|
+
"--refs-ignore",
|
|
63
|
+
"--config"
|
|
64
|
+
];
|
|
65
|
+
var BOOLEAN_FLAGS = [
|
|
66
|
+
"--group",
|
|
67
|
+
"--prune",
|
|
68
|
+
"--helpers",
|
|
69
|
+
"--watch",
|
|
70
|
+
"--check",
|
|
71
|
+
"--prune-check",
|
|
72
|
+
"--lint-refs",
|
|
73
|
+
"--version",
|
|
74
|
+
"-v",
|
|
75
|
+
"--help",
|
|
76
|
+
"-h"
|
|
77
|
+
];
|
|
78
|
+
var HELP = `${USAGE}
|
|
79
|
+
|
|
80
|
+
Generates typed constants for the CSS custom properties found in <glob>.
|
|
81
|
+
Options can also be set in css-typed-vars.config.{js,mjs,cjs,ts,mts,json}; flags take precedence.
|
|
82
|
+
|
|
83
|
+
Options:
|
|
84
|
+
--config <file> Config file to use instead of looking one up
|
|
85
|
+
--input <glob> CSS files to scan (repeatable)
|
|
86
|
+
--output <file> .ts, .js/.mjs/.cjs (plus a .d.ts), or .json (design tokens)
|
|
87
|
+
--exclude <glob> Files to skip (repeatable)
|
|
88
|
+
--prefix <prefix> Prefix every generated key
|
|
89
|
+
--naming <convention> camelCase (default), snake, kebab, constant or pascal
|
|
90
|
+
--selector <selector> Also scan this selector's blocks, not just :root/@theme (repeatable)
|
|
91
|
+
--theme <name>=<sel> Theme block, emitted as @theme JSDoc tags (repeatable)
|
|
92
|
+
--group Nest keys by the first hyphen segment (cssVars.color.primary)
|
|
93
|
+
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback
|
|
94
|
+
--prune Drop vars with no cssVars.<key> usage (needs --usage)
|
|
95
|
+
--usage <glob> Source files to scan for cssVars usage (repeatable)
|
|
96
|
+
--usage-exclude <glob> Source files to skip in the usage scan (repeatable)
|
|
97
|
+
|
|
98
|
+
Modes:
|
|
99
|
+
--watch Regenerate when input (or, with --prune, usage) files change
|
|
100
|
+
--check Exit 1 if the output is out of date, without writing
|
|
101
|
+
--prune-check Exit 1 if any var has no cssVars.<key> usage (needs --usage)
|
|
102
|
+
--lint-refs Exit 1 on var() references to undeclared variables
|
|
103
|
+
--refs-ignore <name> Name or --prefix-* pattern for --lint-refs to skip (repeatable)
|
|
104
|
+
--version, -v Print the version
|
|
105
|
+
--help, -h Print this help
|
|
106
|
+
|
|
107
|
+
Docs: https://stanislavkozachenko.github.io/css-typed-vars/`;
|
|
108
|
+
function editDistance(a, b) {
|
|
109
|
+
let row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
110
|
+
for (let i = 1; i <= a.length; i++) {
|
|
111
|
+
const next = [i];
|
|
112
|
+
for (let j = 1; j <= b.length; j++) {
|
|
113
|
+
next[j] = Math.min(row[j] + 1, next[j - 1] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
114
|
+
}
|
|
115
|
+
row = next;
|
|
116
|
+
}
|
|
117
|
+
return row[b.length];
|
|
118
|
+
}
|
|
119
|
+
function validateArgs() {
|
|
120
|
+
for (let i = 0; i < args.length; i++) {
|
|
121
|
+
const arg = args[i];
|
|
122
|
+
if (VALUE_FLAGS.includes(arg)) {
|
|
123
|
+
if (i + 1 >= args.length) throw new CliError(`css-typed-vars: ${arg} requires a value.`);
|
|
124
|
+
i++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (BOOLEAN_FLAGS.includes(arg)) continue;
|
|
128
|
+
if (arg.startsWith("-")) {
|
|
129
|
+
const known = [...VALUE_FLAGS, ...BOOLEAN_FLAGS].filter((flag) => flag.startsWith("--"));
|
|
130
|
+
const closest = known.reduce((best, flag) => editDistance(arg, flag) < editDistance(arg, best) ? flag : best);
|
|
131
|
+
const hint = editDistance(arg, closest) <= 2 ? ` Did you mean ${closest}?` : "";
|
|
132
|
+
throw new CliError(`css-typed-vars: unknown option "${arg}".${hint} Run with --help to see all options.`);
|
|
133
|
+
}
|
|
134
|
+
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".`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
52
137
|
var CliError = class extends Error {
|
|
53
138
|
};
|
|
139
|
+
var CONFIG_FILES = [
|
|
140
|
+
"css-typed-vars.config.js",
|
|
141
|
+
"css-typed-vars.config.mjs",
|
|
142
|
+
"css-typed-vars.config.cjs",
|
|
143
|
+
"css-typed-vars.config.ts",
|
|
144
|
+
"css-typed-vars.config.mts",
|
|
145
|
+
"css-typed-vars.config.json"
|
|
146
|
+
];
|
|
147
|
+
async function importConfig(path) {
|
|
148
|
+
if (/\.json$/i.test(path)) return JSON.parse(await readFile(path, "utf8"));
|
|
149
|
+
const mod = await import(pathToFileURL(path).href);
|
|
150
|
+
return mod.default ?? {};
|
|
151
|
+
}
|
|
54
152
|
async function loadConfig() {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
153
|
+
const explicit = getArg("--config");
|
|
154
|
+
if (explicit !== void 0) {
|
|
155
|
+
const path = resolve(process.cwd(), explicit);
|
|
156
|
+
if (!await exists(path)) throw new CliError(`css-typed-vars: config file "${explicit}" not found.`);
|
|
157
|
+
try {
|
|
158
|
+
return await importConfig(path);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
throw new CliError(`css-typed-vars: failed to load config "${explicit}": ${err.message}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (const file of CONFIG_FILES) {
|
|
61
164
|
const path = resolve(process.cwd(), file);
|
|
165
|
+
if (!await exists(path)) continue;
|
|
62
166
|
try {
|
|
63
|
-
|
|
64
|
-
const content = await readFile(path, "utf8");
|
|
65
|
-
return JSON.parse(content);
|
|
66
|
-
} else {
|
|
67
|
-
const mod = await import(pathToFileURL(path).href);
|
|
68
|
-
return mod.default ?? {};
|
|
69
|
-
}
|
|
167
|
+
return await importConfig(path);
|
|
70
168
|
} catch (err) {
|
|
71
|
-
const code = err.code;
|
|
72
|
-
if (code === "ENOENT" || code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") continue;
|
|
73
169
|
console.warn(`css-typed-vars: failed to load config "${file}": ${err.message}`);
|
|
74
170
|
}
|
|
75
171
|
}
|
|
76
172
|
return {};
|
|
77
173
|
}
|
|
174
|
+
var exists = (path) => access(path).then(() => true, () => false);
|
|
78
175
|
function applyFlags(config) {
|
|
79
176
|
const inputArgs = getArgs("--input");
|
|
80
177
|
const excludeArgs = getArgs("--exclude");
|
|
@@ -134,7 +231,7 @@ function validateTarget({ options, tag }, single) {
|
|
|
134
231
|
}
|
|
135
232
|
if (!input || !output) {
|
|
136
233
|
throw new CliError(single ? `${USAGE}
|
|
137
|
-
Or add a css-typed-vars.config.js file with input and output fields.` : `${tag}: every target needs "input" and "output".`);
|
|
234
|
+
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".`);
|
|
138
235
|
}
|
|
139
236
|
if (group && prefix) throw new CliError(`${tag}: --group cannot be combined with --prefix.`);
|
|
140
237
|
if ((prune || pruneCheckMode) && !usage) {
|
|
@@ -176,20 +273,51 @@ async function check({ options, tag }) {
|
|
|
176
273
|
return true;
|
|
177
274
|
}
|
|
178
275
|
async function run({ options }) {
|
|
179
|
-
await generate({ ...options, input: options.input, output: options.output });
|
|
180
|
-
console.log(
|
|
276
|
+
const written = await generate({ ...options, input: options.input, output: options.output });
|
|
277
|
+
console.log(`${written ? "Generated" : "Up to date"} \u2192 ${options.output}`);
|
|
181
278
|
}
|
|
279
|
+
var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
182
280
|
function watchTarget(target) {
|
|
183
|
-
const { input, exclude } = target.options;
|
|
184
|
-
const
|
|
281
|
+
const { input, exclude, output, prune, usage, usageExclude } = target.options;
|
|
282
|
+
const jsExt = /\.(m|c)?js$/i.exec(output);
|
|
283
|
+
const outputs = new Set([output, ...jsExt ? [output.slice(0, -jsExt[0].length) + ".d.ts"] : []].map((path) => resolve(path)));
|
|
284
|
+
let timer = null;
|
|
285
|
+
let running = false;
|
|
286
|
+
let pending = false;
|
|
287
|
+
const regenerate = async () => {
|
|
288
|
+
if (running) {
|
|
289
|
+
pending = true;
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
running = true;
|
|
293
|
+
await run(target).catch(console.error);
|
|
294
|
+
running = false;
|
|
295
|
+
if (pending) {
|
|
296
|
+
pending = false;
|
|
297
|
+
await regenerate();
|
|
298
|
+
}
|
|
299
|
+
};
|
|
185
300
|
const makeHandler = (label) => (file) => {
|
|
301
|
+
if (outputs.has(resolve(file))) return;
|
|
186
302
|
console.log(`${label}: ${file}`);
|
|
187
|
-
|
|
303
|
+
if (timer) clearTimeout(timer);
|
|
304
|
+
timer = setTimeout(() => {
|
|
305
|
+
timer = null;
|
|
306
|
+
void regenerate();
|
|
307
|
+
}, 100);
|
|
308
|
+
};
|
|
309
|
+
const subscribe = (patterns, ignored) => {
|
|
310
|
+
watch(patterns, { ignoreInitial: true, ...ignored.length > 0 ? { ignored } : {} }).on("change", makeHandler("Changed")).on("add", makeHandler("Added")).on("unlink", makeHandler("Removed"));
|
|
188
311
|
};
|
|
189
|
-
|
|
190
|
-
|
|
312
|
+
subscribe(toArray(input), toArray(exclude));
|
|
313
|
+
if (prune) subscribe(toArray(usage), toArray(usageExclude));
|
|
191
314
|
}
|
|
192
315
|
async function main() {
|
|
316
|
+
validateArgs();
|
|
317
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
318
|
+
console.log(HELP);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
193
321
|
if (args.includes("--version") || args.includes("-v")) {
|
|
194
322
|
const pkg = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
|
|
195
323
|
console.log(pkg.version);
|
package/dist/esbuild.cjs
CHANGED
|
@@ -36,12 +36,26 @@ module.exports = __toCommonJS(esbuild_exports);
|
|
|
36
36
|
|
|
37
37
|
// src/unplugin.ts
|
|
38
38
|
var import_unplugin = require("unplugin");
|
|
39
|
-
|
|
39
|
+
|
|
40
|
+
// src/write.ts
|
|
41
|
+
var import_promises = require("fs/promises");
|
|
40
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
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/unplugin.ts
|
|
52
|
+
var import_node_path3 = require("path");
|
|
41
53
|
|
|
42
54
|
// src/scanner.ts
|
|
43
|
-
var
|
|
55
|
+
var import_promises2 = require("fs/promises");
|
|
56
|
+
var import_node_path2 = require("path");
|
|
44
57
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
58
|
+
var import_picomatch = __toESM(require("picomatch"), 1);
|
|
45
59
|
|
|
46
60
|
// src/parser.ts
|
|
47
61
|
function stepQuote(text, i, quote) {
|
|
@@ -323,6 +337,17 @@ function toRawPosition(stripped, strippedPos) {
|
|
|
323
337
|
function normalizePatterns(patterns) {
|
|
324
338
|
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
325
339
|
}
|
|
340
|
+
function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
341
|
+
const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
|
|
342
|
+
const include = compile(patterns);
|
|
343
|
+
const ignore = exclude ? compile(exclude) : () => false;
|
|
344
|
+
return (file) => {
|
|
345
|
+
const absolute = (0, import_node_path2.resolve)(cwd, file).replace(/\\/g, "/");
|
|
346
|
+
const fromCwd = (0, import_node_path2.relative)(cwd, absolute).replace(/\\/g, "/");
|
|
347
|
+
const matches = (test) => test(fromCwd) || test(absolute);
|
|
348
|
+
return matches(include) && !matches(ignore);
|
|
349
|
+
};
|
|
350
|
+
}
|
|
326
351
|
function collectVarNames(scanned) {
|
|
327
352
|
const names = new Set(scanned.declarations.keys());
|
|
328
353
|
for (const values of scanned.themes.values()) {
|
|
@@ -338,7 +363,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
338
363
|
files.map(async (file) => {
|
|
339
364
|
let css;
|
|
340
365
|
try {
|
|
341
|
-
css = await (0,
|
|
366
|
+
css = await (0, import_promises2.readFile)(file, "utf8");
|
|
342
367
|
} catch (err) {
|
|
343
368
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
344
369
|
return null;
|
|
@@ -386,7 +411,7 @@ async function scanUsage(patterns, exclude) {
|
|
|
386
411
|
const texts = await Promise.all(
|
|
387
412
|
files.map(async (file) => {
|
|
388
413
|
try {
|
|
389
|
-
return await (0,
|
|
414
|
+
return await (0, import_promises2.readFile)(file, "utf8");
|
|
390
415
|
} catch (err) {
|
|
391
416
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
392
417
|
return "";
|
|
@@ -690,19 +715,19 @@ function generateTokens(varNames, options = {}) {
|
|
|
690
715
|
return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
|
|
691
716
|
});
|
|
692
717
|
const paths = new Map(tokens.map(({ meta, path }) => [meta.name, path]));
|
|
693
|
-
const
|
|
718
|
+
const resolve3 = (value) => {
|
|
694
719
|
const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
|
|
695
720
|
return target ? `{${target.join(".")}}` : value;
|
|
696
721
|
};
|
|
697
722
|
const root = {};
|
|
698
723
|
for (const { meta, value, path } of tokens) {
|
|
699
|
-
const token = { $value:
|
|
724
|
+
const token = { $value: resolve3(value) };
|
|
700
725
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
701
726
|
if (type) token.$type = type;
|
|
702
727
|
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
703
728
|
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
704
729
|
if (meta.themes) {
|
|
705
|
-
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme,
|
|
730
|
+
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve3(themeValue)]));
|
|
706
731
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
707
732
|
}
|
|
708
733
|
const [key, leaf] = path;
|
|
@@ -741,13 +766,13 @@ var VIRTUAL_ID = "css-typed-vars/vars";
|
|
|
741
766
|
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
742
767
|
function getDtsPath(options) {
|
|
743
768
|
if (options.dts === false) return null;
|
|
744
|
-
return options.dts ? (0,
|
|
769
|
+
return options.dts ? (0, import_node_path3.resolve)(process.cwd(), options.dts) : (0, import_node_path3.join)(__dirname, "generated.d.ts");
|
|
745
770
|
}
|
|
746
771
|
function outputFiles(options, names, generatorOptions) {
|
|
747
772
|
const files = [];
|
|
748
773
|
const dtsPath = getDtsPath(options);
|
|
749
774
|
if (dtsPath) files.push([dtsPath, generateDeclaration(names, generatorOptions)]);
|
|
750
|
-
if (options.tokens) files.push([(0,
|
|
775
|
+
if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
|
|
751
776
|
return files;
|
|
752
777
|
}
|
|
753
778
|
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
@@ -799,7 +824,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
799
824
|
writeQueue = writeQueue.then(async () => {
|
|
800
825
|
for (const [path, content] of files) {
|
|
801
826
|
if (myGeneration !== generation) return;
|
|
802
|
-
await (
|
|
827
|
+
await writeOutput(path, content);
|
|
803
828
|
}
|
|
804
829
|
}).catch(console.error);
|
|
805
830
|
await writeQueue;
|
|
@@ -811,8 +836,12 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
811
836
|
server.ws.send({ type: "full-reload" });
|
|
812
837
|
}
|
|
813
838
|
};
|
|
839
|
+
const isInput = createFileMatcher(options.input, options.exclude);
|
|
840
|
+
const isUsage = options.prune ? createFileMatcher(options.usage, options.usageExclude) : () => false;
|
|
841
|
+
const outputs = new Set([getDtsPath(options), options.tokens && (0, import_node_path3.resolve)(process.cwd(), options.tokens)].filter((path) => !!path).map((path) => (0, import_node_path3.resolve)(path)));
|
|
814
842
|
const handle = (file) => {
|
|
815
|
-
if (
|
|
843
|
+
if (outputs.has((0, import_node_path3.resolve)(file))) return;
|
|
844
|
+
if (!isInput(file) && !isUsage(file)) return;
|
|
816
845
|
if (debounceTimer) clearTimeout(debounceTimer);
|
|
817
846
|
debounceTimer = setTimeout(() => {
|
|
818
847
|
debounceTimer = null;
|
|
@@ -829,7 +858,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
829
858
|
const scanned = await cachedScan;
|
|
830
859
|
const names = await resolveNames(scanned);
|
|
831
860
|
for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
|
|
832
|
-
await (
|
|
861
|
+
await writeOutput(path, content);
|
|
833
862
|
}
|
|
834
863
|
},
|
|
835
864
|
resolveId(id) {
|
package/dist/esbuild.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
checkGenerated: () => checkGenerated,
|
|
34
|
+
defineConfig: () => defineConfig,
|
|
34
35
|
findUndefinedRefs: () => findUndefinedRefs,
|
|
35
36
|
findUnusedVars: () => findUnusedVars,
|
|
36
37
|
generate: () => generate,
|
|
@@ -51,12 +52,25 @@ __export(src_exports, {
|
|
|
51
52
|
scanVarNames: () => scanVarNames
|
|
52
53
|
});
|
|
53
54
|
module.exports = __toCommonJS(src_exports);
|
|
54
|
-
var
|
|
55
|
+
var import_promises3 = require("fs/promises");
|
|
56
|
+
var import_node_path3 = require("path");
|
|
57
|
+
|
|
58
|
+
// src/write.ts
|
|
59
|
+
var import_promises = require("fs/promises");
|
|
55
60
|
var import_node_path = require("path");
|
|
61
|
+
async function writeOutput(path, content) {
|
|
62
|
+
const current = await (0, import_promises.readFile)(path, "utf8").catch(() => null);
|
|
63
|
+
if (current === content) return false;
|
|
64
|
+
await (0, import_promises.mkdir)((0, import_node_path.dirname)(path), { recursive: true });
|
|
65
|
+
await (0, import_promises.writeFile)(path, content, "utf8");
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
56
68
|
|
|
57
69
|
// src/scanner.ts
|
|
58
|
-
var
|
|
70
|
+
var import_promises2 = require("fs/promises");
|
|
71
|
+
var import_node_path2 = require("path");
|
|
59
72
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
73
|
+
var import_picomatch = __toESM(require("picomatch"), 1);
|
|
60
74
|
|
|
61
75
|
// src/parser.ts
|
|
62
76
|
function stepQuote(text, i, quote) {
|
|
@@ -384,7 +398,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
384
398
|
files.map(async (file) => {
|
|
385
399
|
let css;
|
|
386
400
|
try {
|
|
387
|
-
css = await (0,
|
|
401
|
+
css = await (0, import_promises2.readFile)(file, "utf8");
|
|
388
402
|
} catch (err) {
|
|
389
403
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
390
404
|
return null;
|
|
@@ -439,7 +453,7 @@ async function scanUsage(patterns, exclude) {
|
|
|
439
453
|
const texts = await Promise.all(
|
|
440
454
|
files.map(async (file) => {
|
|
441
455
|
try {
|
|
442
|
-
return await (0,
|
|
456
|
+
return await (0, import_promises2.readFile)(file, "utf8");
|
|
443
457
|
} catch (err) {
|
|
444
458
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
445
459
|
return "";
|
|
@@ -456,7 +470,7 @@ async function scanReferences(patterns, exclude) {
|
|
|
456
470
|
const perFile = await Promise.all(
|
|
457
471
|
files.map(async (file) => {
|
|
458
472
|
try {
|
|
459
|
-
return { file, css: await (0,
|
|
473
|
+
return { file, css: await (0, import_promises2.readFile)(file, "utf8") };
|
|
460
474
|
} catch (err) {
|
|
461
475
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
462
476
|
return null;
|
|
@@ -799,19 +813,19 @@ function generateTokens(varNames, options = {}) {
|
|
|
799
813
|
return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
|
|
800
814
|
});
|
|
801
815
|
const paths = new Map(tokens.map(({ meta, path }) => [meta.name, path]));
|
|
802
|
-
const
|
|
816
|
+
const resolve3 = (value) => {
|
|
803
817
|
const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
|
|
804
818
|
return target ? `{${target.join(".")}}` : value;
|
|
805
819
|
};
|
|
806
820
|
const root = {};
|
|
807
821
|
for (const { meta, value, path } of tokens) {
|
|
808
|
-
const token = { $value:
|
|
822
|
+
const token = { $value: resolve3(value) };
|
|
809
823
|
const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
|
|
810
824
|
if (type) token.$type = type;
|
|
811
825
|
if (meta.doc?.description) token.$description = meta.doc.description;
|
|
812
826
|
if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
|
|
813
827
|
if (meta.themes) {
|
|
814
|
-
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme,
|
|
828
|
+
const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve3(themeValue)]));
|
|
815
829
|
token.$extensions = { "css-typed-vars": { themes } };
|
|
816
830
|
}
|
|
817
831
|
const [key, leaf] = path;
|
|
@@ -846,6 +860,9 @@ function partitionUnused(names, usage, options) {
|
|
|
846
860
|
}
|
|
847
861
|
|
|
848
862
|
// src/index.ts
|
|
863
|
+
function defineConfig(config) {
|
|
864
|
+
return config;
|
|
865
|
+
}
|
|
849
866
|
async function findUnusedVars(options) {
|
|
850
867
|
if (!options.usage) throw new Error('css-typed-vars: findUnusedVars requires "usage" (glob(s) of source files to scan).');
|
|
851
868
|
const [scanned, usage] = await Promise.all([
|
|
@@ -880,7 +897,7 @@ async function computeOutputs(options) {
|
|
|
880
897
|
}
|
|
881
898
|
names = used;
|
|
882
899
|
}
|
|
883
|
-
const outPath = (0,
|
|
900
|
+
const outPath = (0, import_node_path3.resolve)(options.output);
|
|
884
901
|
const generatorOptions = {
|
|
885
902
|
prefix: options.prefix,
|
|
886
903
|
naming: options.naming,
|
|
@@ -905,18 +922,18 @@ async function computeOutputs(options) {
|
|
|
905
922
|
}
|
|
906
923
|
async function generate(options) {
|
|
907
924
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
908
|
-
|
|
909
|
-
await (0, import_promises2.writeFile)(outPath, content, "utf8");
|
|
925
|
+
let written = await writeOutput(outPath, content);
|
|
910
926
|
if (dtsPath && dtsContent !== void 0) {
|
|
911
|
-
|
|
927
|
+
if (await writeOutput(dtsPath, dtsContent)) written = true;
|
|
912
928
|
}
|
|
929
|
+
return written;
|
|
913
930
|
}
|
|
914
931
|
async function checkGenerated(options) {
|
|
915
932
|
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
916
|
-
const current = await (0,
|
|
933
|
+
const current = await (0, import_promises3.readFile)(outPath, "utf8").catch(() => null);
|
|
917
934
|
if (current !== content) return false;
|
|
918
935
|
if (dtsPath && dtsContent !== void 0) {
|
|
919
|
-
const currentDts = await (0,
|
|
936
|
+
const currentDts = await (0, import_promises3.readFile)(dtsPath, "utf8").catch(() => null);
|
|
920
937
|
if (currentDts !== dtsContent) return false;
|
|
921
938
|
}
|
|
922
939
|
return true;
|
|
@@ -924,6 +941,7 @@ async function checkGenerated(options) {
|
|
|
924
941
|
// Annotate the CommonJS export names for ESM import in node:
|
|
925
942
|
0 && (module.exports = {
|
|
926
943
|
checkGenerated,
|
|
944
|
+
defineConfig,
|
|
927
945
|
findUndefinedRefs,
|
|
928
946
|
findUnusedVars,
|
|
929
947
|
generate,
|
package/dist/index.d.cts
CHANGED
|
@@ -46,9 +46,17 @@ interface GenerateOptions {
|
|
|
46
46
|
/** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
|
|
47
47
|
refsIgnore?: string[];
|
|
48
48
|
}
|
|
49
|
+
/** A CLI config file's options — like `GenerateOptions`, but anything may come from CLI flags instead. */
|
|
50
|
+
type Config = Partial<GenerateOptions>;
|
|
51
|
+
/**
|
|
52
|
+
* Types a config file's export (one target, or an array of targets):
|
|
53
|
+
* `export default defineConfig({ input: 'src/styles/*.css', output: 'src/cssVars.ts' })`.
|
|
54
|
+
*/
|
|
55
|
+
declare function defineConfig(config: Config): Config;
|
|
56
|
+
declare function defineConfig(config: Config[]): Config[];
|
|
49
57
|
declare function findUnusedVars(options: Pick<GenerateOptions, 'input' | 'exclude' | 'selectors' | 'themes' | 'prefix' | 'naming' | 'group' | 'usage' | 'usageExclude'>): Promise<string[]>;
|
|
50
58
|
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore'>): Promise<FileReference[]>;
|
|
51
|
-
declare function generate(options: GenerateOptions): Promise<
|
|
59
|
+
declare function generate(options: GenerateOptions): Promise<boolean>;
|
|
52
60
|
declare function checkGenerated(options: GenerateOptions): Promise<boolean>;
|
|
53
61
|
|
|
54
|
-
export { type FileReference, type GenerateOptions, NamingConvention, PropertyRule, VarDoc, VarReference, checkGenerated, findUndefinedRefs, findUnusedVars, generate, scanCss, scanReferences, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
|
62
|
+
export { type Config, type FileReference, type GenerateOptions, NamingConvention, PropertyRule, VarDoc, VarReference, checkGenerated, defineConfig, findUndefinedRefs, findUnusedVars, generate, scanCss, scanReferences, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
package/dist/index.d.ts
CHANGED
|
@@ -46,9 +46,17 @@ interface GenerateOptions {
|
|
|
46
46
|
/** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
|
|
47
47
|
refsIgnore?: string[];
|
|
48
48
|
}
|
|
49
|
+
/** A CLI config file's options — like `GenerateOptions`, but anything may come from CLI flags instead. */
|
|
50
|
+
type Config = Partial<GenerateOptions>;
|
|
51
|
+
/**
|
|
52
|
+
* Types a config file's export (one target, or an array of targets):
|
|
53
|
+
* `export default defineConfig({ input: 'src/styles/*.css', output: 'src/cssVars.ts' })`.
|
|
54
|
+
*/
|
|
55
|
+
declare function defineConfig(config: Config): Config;
|
|
56
|
+
declare function defineConfig(config: Config[]): Config[];
|
|
49
57
|
declare function findUnusedVars(options: Pick<GenerateOptions, 'input' | 'exclude' | 'selectors' | 'themes' | 'prefix' | 'naming' | 'group' | 'usage' | 'usageExclude'>): Promise<string[]>;
|
|
50
58
|
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore'>): Promise<FileReference[]>;
|
|
51
|
-
declare function generate(options: GenerateOptions): Promise<
|
|
59
|
+
declare function generate(options: GenerateOptions): Promise<boolean>;
|
|
52
60
|
declare function checkGenerated(options: GenerateOptions): Promise<boolean>;
|
|
53
61
|
|
|
54
|
-
export { type FileReference, type GenerateOptions, NamingConvention, PropertyRule, VarDoc, VarReference, checkGenerated, findUndefinedRefs, findUnusedVars, generate, scanCss, scanReferences, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
|
62
|
+
export { type Config, type FileReference, type GenerateOptions, NamingConvention, PropertyRule, VarDoc, VarReference, checkGenerated, defineConfig, findUndefinedRefs, findUnusedVars, generate, scanCss, scanReferences, scanUsedKeys, scanVarDeclarations, scanVarNames };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
checkGenerated,
|
|
3
|
+
defineConfig,
|
|
3
4
|
findUndefinedRefs,
|
|
4
5
|
findUnusedVars,
|
|
5
6
|
generate
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-TB5PSGEF.js";
|
|
7
8
|
import {
|
|
8
9
|
generateCode,
|
|
9
10
|
generateDeclaration,
|
|
@@ -20,9 +21,10 @@ import {
|
|
|
20
21
|
scanUsedKeys,
|
|
21
22
|
scanVarDeclarations,
|
|
22
23
|
scanVarNames
|
|
23
|
-
} from "./chunk-
|
|
24
|
+
} from "./chunk-DHBPPGOE.js";
|
|
24
25
|
export {
|
|
25
26
|
checkGenerated,
|
|
27
|
+
defineConfig,
|
|
26
28
|
findUndefinedRefs,
|
|
27
29
|
findUnusedVars,
|
|
28
30
|
generate,
|