css-typed-vars 0.11.0 → 0.13.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 +37 -4
- package/dist/{chunk-P3HJQZ6Y.js → chunk-FWKCSH2F.js} +7 -3
- package/dist/{chunk-CBN3J54B.js → chunk-JGJ53FND.js} +1 -1
- package/dist/{chunk-TR53YUPE.js → chunk-XSFYQPH6.js} +88 -19
- package/dist/cli.cjs +112 -27
- package/dist/cli.js +21 -9
- package/dist/esbuild.cjs +52 -12
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/{generator-DJ9uOYBP.d.ts → generator-BokM5zux.d.cts} +1 -1
- package/dist/{generator-DJ9uOYBP.d.cts → generator-BokM5zux.d.ts} +1 -1
- package/dist/index.cjs +91 -18
- package/dist/index.d.cts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +2 -2
- package/dist/rollup.cjs +52 -12
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +52 -12
- package/dist/unplugin.d.cts +2 -2
- package/dist/unplugin.d.ts +2 -2
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +52 -12
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +52 -12
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +4 -4
- package/schema.json +5 -1
package/dist/cli.js
CHANGED
|
@@ -4,12 +4,12 @@ import {
|
|
|
4
4
|
findUndefinedRefs,
|
|
5
5
|
findUnusedVars,
|
|
6
6
|
generate
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-FWKCSH2F.js";
|
|
8
8
|
import {
|
|
9
9
|
VALID_NAMINGS,
|
|
10
10
|
createFileMatcher,
|
|
11
11
|
globWatchRoots
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-XSFYQPH6.js";
|
|
13
13
|
|
|
14
14
|
// src/cli.ts
|
|
15
15
|
import { access, readFile } from "fs/promises";
|
|
@@ -31,7 +31,8 @@ var CONFIG_KEYS = [
|
|
|
31
31
|
"usage",
|
|
32
32
|
"usageExclude",
|
|
33
33
|
"helpers",
|
|
34
|
-
"refsIgnore"
|
|
34
|
+
"refsIgnore",
|
|
35
|
+
"refsSource"
|
|
35
36
|
];
|
|
36
37
|
|
|
37
38
|
// src/cli.ts
|
|
@@ -67,7 +68,8 @@ var OPTION_FLAGS = [
|
|
|
67
68
|
"--usage",
|
|
68
69
|
"--usage-exclude",
|
|
69
70
|
"--helpers",
|
|
70
|
-
"--refs-ignore"
|
|
71
|
+
"--refs-ignore",
|
|
72
|
+
"--refs-source"
|
|
71
73
|
];
|
|
72
74
|
var USAGE = "Usage: css-typed-vars --input <glob> --output <file> [options]";
|
|
73
75
|
var VALUE_FLAGS = [
|
|
@@ -81,6 +83,7 @@ var VALUE_FLAGS = [
|
|
|
81
83
|
"--usage",
|
|
82
84
|
"--usage-exclude",
|
|
83
85
|
"--refs-ignore",
|
|
86
|
+
"--refs-source",
|
|
84
87
|
"--config"
|
|
85
88
|
];
|
|
86
89
|
var BOOLEAN_FLAGS = [
|
|
@@ -111,7 +114,7 @@ Options:
|
|
|
111
114
|
--selector <selector> Also scan this selector's blocks, not just :root/@theme (repeatable)
|
|
112
115
|
--theme <name>=<sel> Theme block, emitted as @theme JSDoc tags (repeatable)
|
|
113
116
|
--group Nest keys by the first hyphen segment (cssVars.color.primary)
|
|
114
|
-
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback
|
|
117
|
+
--helpers Also emit CssVarRef and getCssVar/setCssVar/withFallback/varName
|
|
115
118
|
--prune Drop vars with no cssVars.<key> usage (needs --usage)
|
|
116
119
|
--usage <glob> Source files to scan for cssVars usage (repeatable)
|
|
117
120
|
--usage-exclude <glob> Source files to skip in the usage scan (repeatable)
|
|
@@ -122,6 +125,7 @@ Modes:
|
|
|
122
125
|
--prune-check Exit 1 if any var has no cssVars.<key> usage (needs --usage)
|
|
123
126
|
--lint-refs Exit 1 on var() references to undeclared variables
|
|
124
127
|
--refs-ignore <name> Name or --prefix-* pattern for --lint-refs to skip (repeatable)
|
|
128
|
+
--refs-source <glob> Source files whose var() references --lint-refs checks too (repeatable)
|
|
125
129
|
--version, -v Print the version
|
|
126
130
|
--help, -h Print this help
|
|
127
131
|
|
|
@@ -215,6 +219,7 @@ function applyFlags(config) {
|
|
|
215
219
|
const usageArgs = getArgs("--usage");
|
|
216
220
|
const usageExcludeArgs = getArgs("--usage-exclude");
|
|
217
221
|
const refsIgnoreArgs = getArgs("--refs-ignore");
|
|
222
|
+
const refsSourceArgs = getArgs("--refs-source");
|
|
218
223
|
return {
|
|
219
224
|
...config,
|
|
220
225
|
input: inputArgs.length > 0 ? inputArgs : config.input,
|
|
@@ -229,7 +234,8 @@ function applyFlags(config) {
|
|
|
229
234
|
usage: usageArgs.length > 0 ? usageArgs : config.usage,
|
|
230
235
|
usageExclude: usageExcludeArgs.length > 0 ? usageExcludeArgs : config.usageExclude,
|
|
231
236
|
helpers: args.includes("--helpers") || config.helpers,
|
|
232
|
-
refsIgnore: refsIgnoreArgs.length > 0 ? refsIgnoreArgs : config.refsIgnore
|
|
237
|
+
refsIgnore: refsIgnoreArgs.length > 0 ? refsIgnoreArgs : config.refsIgnore,
|
|
238
|
+
refsSource: refsSourceArgs.length > 0 ? refsSourceArgs : config.refsSource
|
|
233
239
|
};
|
|
234
240
|
}
|
|
235
241
|
function loadTargets(config) {
|
|
@@ -276,8 +282,15 @@ Or add a css-typed-vars.config.js file with input and output fields. Run with --
|
|
|
276
282
|
throw new CliError(`${tag}: --prune/--prune-check requires --usage <glob> (source files to scan for cssVars usage).`);
|
|
277
283
|
}
|
|
278
284
|
}
|
|
285
|
+
function outputPaths(output) {
|
|
286
|
+
if (!output) return /* @__PURE__ */ new Set();
|
|
287
|
+
const jsExt = /\.(m|c)?js$/i.exec(output);
|
|
288
|
+
return new Set([output, ...jsExt ? [output.slice(0, -jsExt[0].length) + ".d.ts"] : []].map((path) => resolve(path)));
|
|
289
|
+
}
|
|
279
290
|
async function lintRefs({ options, tag }) {
|
|
280
|
-
const
|
|
291
|
+
const { input, exclude, refsIgnore, refsSource } = options;
|
|
292
|
+
const outputs = outputPaths(options.output);
|
|
293
|
+
const refs = (await findUndefinedRefs({ input, exclude, refsIgnore, refsSource })).filter((ref) => !outputs.has(resolve(ref.file)));
|
|
281
294
|
const format = (ref) => ` ${relative(process.cwd(), ref.file).replace(/\\/g, "/")}:${ref.line}:${ref.column} var(${ref.name}${ref.hasFallback ? ", \u2026" : ""})`;
|
|
282
295
|
const errors = refs.filter((ref) => !ref.hasFallback);
|
|
283
296
|
const warnings = refs.filter((ref) => ref.hasFallback);
|
|
@@ -317,8 +330,7 @@ async function run({ options }) {
|
|
|
317
330
|
var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
318
331
|
function watchTarget(target) {
|
|
319
332
|
const { input, exclude, output, prune, usage, usageExclude } = target.options;
|
|
320
|
-
const
|
|
321
|
-
const outputs = new Set([output, ...jsExt ? [output.slice(0, -jsExt[0].length) + ".d.ts"] : []].map((path) => resolve(path)));
|
|
333
|
+
const outputs = outputPaths(output);
|
|
322
334
|
let timer = null;
|
|
323
335
|
let running = false;
|
|
324
336
|
let pending = false;
|
package/dist/esbuild.cjs
CHANGED
|
@@ -54,8 +54,8 @@ var import_node_path3 = require("path");
|
|
|
54
54
|
// src/scanner.ts
|
|
55
55
|
var import_promises2 = require("fs/promises");
|
|
56
56
|
var import_node_path2 = require("path");
|
|
57
|
-
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
58
57
|
var import_picomatch = __toESM(require("picomatch"), 1);
|
|
58
|
+
var import_tinyglobby = require("tinyglobby");
|
|
59
59
|
|
|
60
60
|
// src/parser.ts
|
|
61
61
|
function stepQuote(text, i, quote) {
|
|
@@ -162,9 +162,11 @@ function maskQuotedContent(text) {
|
|
|
162
162
|
}
|
|
163
163
|
return result;
|
|
164
164
|
}
|
|
165
|
+
var CLOSERS = { "(": ")", "[": "]", "{": "}" };
|
|
165
166
|
function extractValue(text, start) {
|
|
166
167
|
let i = start;
|
|
167
168
|
let quote = null;
|
|
169
|
+
const open = [];
|
|
168
170
|
for (; i < text.length; i++) {
|
|
169
171
|
const char = text[i];
|
|
170
172
|
if (quote) {
|
|
@@ -177,16 +179,19 @@ function extractValue(text, start) {
|
|
|
177
179
|
quote = char;
|
|
178
180
|
continue;
|
|
179
181
|
}
|
|
180
|
-
if (char
|
|
182
|
+
if (char in CLOSERS) open.push(CLOSERS[char]);
|
|
183
|
+
else if (char === open[open.length - 1]) open.pop();
|
|
184
|
+
else if (char === "}" || char === ";" && open.length === 0) break;
|
|
181
185
|
}
|
|
182
186
|
return text.slice(start, i).trim();
|
|
183
187
|
}
|
|
188
|
+
var IMPORTANT = /\s*!\s*important$/i;
|
|
184
189
|
function forEachProperty(block, onProperty, offset = 0) {
|
|
185
190
|
const masked = maskQuotedContent(block);
|
|
186
191
|
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
187
192
|
const name = prop[0];
|
|
188
193
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
189
|
-
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
194
|
+
onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
|
|
190
195
|
}
|
|
191
196
|
}
|
|
192
197
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -337,6 +342,39 @@ function toRawPosition(stripped, strippedPos) {
|
|
|
337
342
|
function normalizePatterns(patterns) {
|
|
338
343
|
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
339
344
|
}
|
|
345
|
+
function comparePaths(a, b) {
|
|
346
|
+
const as = a.split("/");
|
|
347
|
+
const bs = b.split("/");
|
|
348
|
+
for (let i = 0; i < Math.min(as.length, bs.length); i++) {
|
|
349
|
+
if (as[i] === bs[i]) continue;
|
|
350
|
+
const aIsFile = i === as.length - 1;
|
|
351
|
+
const bIsFile = i === bs.length - 1;
|
|
352
|
+
if (aIsFile !== bIsFile) return aIsFile ? -1 : 1;
|
|
353
|
+
return as[i] < bs[i] ? -1 : 1;
|
|
354
|
+
}
|
|
355
|
+
return as.length - bs.length;
|
|
356
|
+
}
|
|
357
|
+
async function globFiles(pattern, ignore) {
|
|
358
|
+
if (!/^(?:[A-Za-z]:)?\//.test(pattern)) {
|
|
359
|
+
return (0, import_tinyglobby.glob)(pattern, { absolute: true, ignore, expandDirectories: false });
|
|
360
|
+
}
|
|
361
|
+
const { base, glob: rest, isGlob } = import_picomatch.default.scan(pattern);
|
|
362
|
+
const [cwd, relativeGlob] = isGlob ? [base, rest] : [import_node_path2.posix.dirname(pattern), import_node_path2.posix.basename(pattern)];
|
|
363
|
+
const matches = await (0, import_tinyglobby.glob)(relativeGlob, { cwd, absolute: true, expandDirectories: false });
|
|
364
|
+
if (ignore.length === 0) return matches;
|
|
365
|
+
const isIgnored = (0, import_picomatch.default)(ignore, { dot: true });
|
|
366
|
+
return matches.filter((file) => !isIgnored(file));
|
|
367
|
+
}
|
|
368
|
+
async function findFiles(patterns, exclude) {
|
|
369
|
+
const normalized = normalizePatterns(patterns);
|
|
370
|
+
const ignore = [
|
|
371
|
+
...exclude ? normalizePatterns(exclude) : [],
|
|
372
|
+
...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
|
|
373
|
+
];
|
|
374
|
+
const perGlob = await Promise.all(normalized.filter((p) => !p.startsWith("!")).map((p) => globFiles(p, ignore)));
|
|
375
|
+
const files = perGlob.flatMap((matches) => matches.sort(comparePaths));
|
|
376
|
+
return [...new Set(files.reverse())].reverse();
|
|
377
|
+
}
|
|
340
378
|
function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
341
379
|
const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
|
|
342
380
|
const include = compile(patterns);
|
|
@@ -351,9 +389,9 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
|
351
389
|
function globWatchRoots(patterns, cwd = process.cwd()) {
|
|
352
390
|
const roots = /* @__PURE__ */ new Map();
|
|
353
391
|
for (const pattern of normalizePatterns(patterns)) {
|
|
354
|
-
const { base, glob, isGlob } = import_picomatch.default.scan(pattern);
|
|
392
|
+
const { base, glob: glob2, isGlob } = import_picomatch.default.scan(pattern);
|
|
355
393
|
const dir = (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
|
|
356
|
-
const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(
|
|
394
|
+
const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
|
|
357
395
|
const previous = roots.get(dir);
|
|
358
396
|
const unlimited = roots.has(dir) && previous === void 0 || depth === void 0;
|
|
359
397
|
roots.set(dir, unlimited ? void 0 : Math.max(previous ?? 0, depth));
|
|
@@ -371,9 +409,7 @@ function collectVarNames(scanned) {
|
|
|
371
409
|
return [...names].sort();
|
|
372
410
|
}
|
|
373
411
|
async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
374
|
-
const
|
|
375
|
-
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
376
|
-
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
412
|
+
const files = await findFiles(patterns, exclude);
|
|
377
413
|
const perFile = await Promise.all(
|
|
378
414
|
files.map(async (file) => {
|
|
379
415
|
let css;
|
|
@@ -430,9 +466,7 @@ function extractUsage(text, into) {
|
|
|
430
466
|
}
|
|
431
467
|
}
|
|
432
468
|
async function scanUsage(patterns, exclude) {
|
|
433
|
-
const
|
|
434
|
-
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
435
|
-
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
469
|
+
const files = await findFiles(patterns, exclude);
|
|
436
470
|
const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set(), files };
|
|
437
471
|
const texts = await Promise.all(
|
|
438
472
|
files.map(async (file) => {
|
|
@@ -690,13 +724,19 @@ var HELPERS_JS = [
|
|
|
690
724
|
"",
|
|
691
725
|
"export function withFallback(ref, fallback) {",
|
|
692
726
|
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
727
|
+
"}",
|
|
728
|
+
"",
|
|
729
|
+
"export function varName(ref) {",
|
|
730
|
+
" return ref.slice(4, -1);",
|
|
693
731
|
"}"
|
|
694
732
|
];
|
|
695
733
|
var HELPERS_DTS = [
|
|
696
734
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
697
735
|
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
698
736
|
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
699
|
-
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
737
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
|
|
738
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
739
|
+
"export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
|
|
700
740
|
];
|
|
701
741
|
function generateJs(varNames, ...args) {
|
|
702
742
|
const options = normalizeOptions(args);
|
package/dist/esbuild.d.cts
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
|
@@ -41,7 +41,7 @@ interface GeneratorOptions {
|
|
|
41
41
|
group?: boolean;
|
|
42
42
|
/** `@property` rules per var name, emitted as `@syntax` JSDoc tags and enum union types. */
|
|
43
43
|
properties?: Map<string, PropertyRule>;
|
|
44
|
-
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`
|
|
44
|
+
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
|
|
45
45
|
helpers?: boolean;
|
|
46
46
|
/** Per theme name, the values declared for it, emitted as `@theme <name> <value>` JSDoc tags. */
|
|
47
47
|
themes?: Map<string, Map<string, string>>;
|
|
@@ -41,7 +41,7 @@ interface GeneratorOptions {
|
|
|
41
41
|
group?: boolean;
|
|
42
42
|
/** `@property` rules per var name, emitted as `@syntax` JSDoc tags and enum union types. */
|
|
43
43
|
properties?: Map<string, PropertyRule>;
|
|
44
|
-
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`
|
|
44
|
+
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
|
|
45
45
|
helpers?: boolean;
|
|
46
46
|
/** Per theme name, the values declared for it, emitted as `@theme <name> <value>` JSDoc tags. */
|
|
47
47
|
themes?: Map<string, Map<string, string>>;
|
package/dist/index.cjs
CHANGED
|
@@ -69,8 +69,8 @@ async function writeOutput(path, content) {
|
|
|
69
69
|
// src/scanner.ts
|
|
70
70
|
var import_promises2 = require("fs/promises");
|
|
71
71
|
var import_node_path2 = require("path");
|
|
72
|
-
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
73
72
|
var import_picomatch = __toESM(require("picomatch"), 1);
|
|
73
|
+
var import_tinyglobby = require("tinyglobby");
|
|
74
74
|
|
|
75
75
|
// src/parser.ts
|
|
76
76
|
function stepQuote(text, i, quote) {
|
|
@@ -177,9 +177,11 @@ function maskQuotedContent(text) {
|
|
|
177
177
|
}
|
|
178
178
|
return result;
|
|
179
179
|
}
|
|
180
|
+
var CLOSERS = { "(": ")", "[": "]", "{": "}" };
|
|
180
181
|
function extractValue(text, start) {
|
|
181
182
|
let i = start;
|
|
182
183
|
let quote = null;
|
|
184
|
+
const open = [];
|
|
183
185
|
for (; i < text.length; i++) {
|
|
184
186
|
const char = text[i];
|
|
185
187
|
if (quote) {
|
|
@@ -192,16 +194,19 @@ function extractValue(text, start) {
|
|
|
192
194
|
quote = char;
|
|
193
195
|
continue;
|
|
194
196
|
}
|
|
195
|
-
if (char
|
|
197
|
+
if (char in CLOSERS) open.push(CLOSERS[char]);
|
|
198
|
+
else if (char === open[open.length - 1]) open.pop();
|
|
199
|
+
else if (char === "}" || char === ";" && open.length === 0) break;
|
|
196
200
|
}
|
|
197
201
|
return text.slice(start, i).trim();
|
|
198
202
|
}
|
|
203
|
+
var IMPORTANT = /\s*!\s*important$/i;
|
|
199
204
|
function forEachProperty(block, onProperty, offset = 0) {
|
|
200
205
|
const masked = maskQuotedContent(block);
|
|
201
206
|
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
202
207
|
const name = prop[0];
|
|
203
208
|
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
204
|
-
onProperty(name, extractValue(block, colonIndex + 1), offset + prop.index);
|
|
209
|
+
onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
|
|
205
210
|
}
|
|
206
211
|
}
|
|
207
212
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
@@ -371,6 +376,29 @@ function parseVarReferences(css) {
|
|
|
371
376
|
}
|
|
372
377
|
return refs;
|
|
373
378
|
}
|
|
379
|
+
var SOURCE_REF_RE = /\bvar\(\s*(--[\w-]+)\s*(,)?|(?<=[\w-]-)\((?:[\w-]+:)?(--[\w-]+)\)/g;
|
|
380
|
+
function parseSourceVarReferences(text) {
|
|
381
|
+
const lineStarts = [0];
|
|
382
|
+
for (let i = 0; i < text.length; i++) if (text[i] === "\n") lineStarts.push(i + 1);
|
|
383
|
+
const refs = [];
|
|
384
|
+
for (const match of text.matchAll(SOURCE_REF_RE)) {
|
|
385
|
+
let line = lineStarts.length - 1;
|
|
386
|
+
while (lineStarts[line] > match.index) line--;
|
|
387
|
+
refs.push({
|
|
388
|
+
name: match[1] ?? match[3],
|
|
389
|
+
hasFallback: match[2] === ",",
|
|
390
|
+
line: line + 1,
|
|
391
|
+
column: match.index - lineStarts[line] + 1
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
return refs;
|
|
395
|
+
}
|
|
396
|
+
function parseSourceDeclaredNames(text) {
|
|
397
|
+
const names = /* @__PURE__ */ new Set();
|
|
398
|
+
for (const match of text.matchAll(/(?<![\w-])(--[\w-]+)['"`]?\s*:/g)) names.add(match[1]);
|
|
399
|
+
for (const match of text.matchAll(/\bsetProperty\(\s*['"`](--[\w-]+)['"`]/g)) names.add(match[1]);
|
|
400
|
+
return names;
|
|
401
|
+
}
|
|
374
402
|
function parseDeclaredNames(css) {
|
|
375
403
|
const masked = maskQuotedContent(stripComments(css));
|
|
376
404
|
const names = /* @__PURE__ */ new Set();
|
|
@@ -383,6 +411,39 @@ function parseDeclaredNames(css) {
|
|
|
383
411
|
function normalizePatterns(patterns) {
|
|
384
412
|
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
385
413
|
}
|
|
414
|
+
function comparePaths(a, b) {
|
|
415
|
+
const as = a.split("/");
|
|
416
|
+
const bs = b.split("/");
|
|
417
|
+
for (let i = 0; i < Math.min(as.length, bs.length); i++) {
|
|
418
|
+
if (as[i] === bs[i]) continue;
|
|
419
|
+
const aIsFile = i === as.length - 1;
|
|
420
|
+
const bIsFile = i === bs.length - 1;
|
|
421
|
+
if (aIsFile !== bIsFile) return aIsFile ? -1 : 1;
|
|
422
|
+
return as[i] < bs[i] ? -1 : 1;
|
|
423
|
+
}
|
|
424
|
+
return as.length - bs.length;
|
|
425
|
+
}
|
|
426
|
+
async function globFiles(pattern, ignore) {
|
|
427
|
+
if (!/^(?:[A-Za-z]:)?\//.test(pattern)) {
|
|
428
|
+
return (0, import_tinyglobby.glob)(pattern, { absolute: true, ignore, expandDirectories: false });
|
|
429
|
+
}
|
|
430
|
+
const { base, glob: rest, isGlob } = import_picomatch.default.scan(pattern);
|
|
431
|
+
const [cwd, relativeGlob] = isGlob ? [base, rest] : [import_node_path2.posix.dirname(pattern), import_node_path2.posix.basename(pattern)];
|
|
432
|
+
const matches = await (0, import_tinyglobby.glob)(relativeGlob, { cwd, absolute: true, expandDirectories: false });
|
|
433
|
+
if (ignore.length === 0) return matches;
|
|
434
|
+
const isIgnored = (0, import_picomatch.default)(ignore, { dot: true });
|
|
435
|
+
return matches.filter((file) => !isIgnored(file));
|
|
436
|
+
}
|
|
437
|
+
async function findFiles(patterns, exclude) {
|
|
438
|
+
const normalized = normalizePatterns(patterns);
|
|
439
|
+
const ignore = [
|
|
440
|
+
...exclude ? normalizePatterns(exclude) : [],
|
|
441
|
+
...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
|
|
442
|
+
];
|
|
443
|
+
const perGlob = await Promise.all(normalized.filter((p) => !p.startsWith("!")).map((p) => globFiles(p, ignore)));
|
|
444
|
+
const files = perGlob.flatMap((matches) => matches.sort(comparePaths));
|
|
445
|
+
return [...new Set(files.reverse())].reverse();
|
|
446
|
+
}
|
|
386
447
|
function collectVarNames(scanned) {
|
|
387
448
|
const names = new Set(scanned.declarations.keys());
|
|
388
449
|
for (const values of scanned.themes.values()) {
|
|
@@ -391,9 +452,7 @@ function collectVarNames(scanned) {
|
|
|
391
452
|
return [...names].sort();
|
|
392
453
|
}
|
|
393
454
|
async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
394
|
-
const
|
|
395
|
-
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
396
|
-
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
455
|
+
const files = await findFiles(patterns, exclude);
|
|
397
456
|
const perFile = await Promise.all(
|
|
398
457
|
files.map(async (file) => {
|
|
399
458
|
let css;
|
|
@@ -457,9 +516,7 @@ function extractUsage(text, into) {
|
|
|
457
516
|
}
|
|
458
517
|
}
|
|
459
518
|
async function scanUsage(patterns, exclude) {
|
|
460
|
-
const
|
|
461
|
-
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
462
|
-
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
519
|
+
const files = await findFiles(patterns, exclude);
|
|
463
520
|
const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set(), files };
|
|
464
521
|
const texts = await Promise.all(
|
|
465
522
|
files.map(async (file) => {
|
|
@@ -474,10 +531,10 @@ async function scanUsage(patterns, exclude) {
|
|
|
474
531
|
for (const text of texts) extractUsage(text, usage);
|
|
475
532
|
return usage;
|
|
476
533
|
}
|
|
477
|
-
async function scanReferences(patterns, exclude) {
|
|
478
|
-
const
|
|
479
|
-
const
|
|
480
|
-
const files = (await (
|
|
534
|
+
async function scanReferences(patterns, exclude, source = false) {
|
|
535
|
+
const parseReferences = source ? parseSourceVarReferences : parseVarReferences;
|
|
536
|
+
const parseDeclared = source ? parseSourceDeclaredNames : parseDeclaredNames;
|
|
537
|
+
const files = (await findFiles(patterns, exclude)).sort();
|
|
481
538
|
const perFile = await Promise.all(
|
|
482
539
|
files.map(async (file) => {
|
|
483
540
|
try {
|
|
@@ -492,8 +549,8 @@ async function scanReferences(patterns, exclude) {
|
|
|
492
549
|
const declared = /* @__PURE__ */ new Set();
|
|
493
550
|
for (const result of perFile) {
|
|
494
551
|
if (!result) continue;
|
|
495
|
-
for (const ref of
|
|
496
|
-
for (const name of
|
|
552
|
+
for (const ref of parseReferences(result.css)) references.push({ ...ref, file: result.file });
|
|
553
|
+
for (const name of parseDeclared(result.css)) declared.add(name);
|
|
497
554
|
}
|
|
498
555
|
return { references, declared };
|
|
499
556
|
}
|
|
@@ -745,6 +802,12 @@ var HELPERS_TS = [
|
|
|
745
802
|
"",
|
|
746
803
|
"export function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F> {",
|
|
747
804
|
" return `${ref.slice(0, -1)}, ${fallback})` as CssVarWithFallback<R, F>;",
|
|
805
|
+
"}",
|
|
806
|
+
"",
|
|
807
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
808
|
+
"",
|
|
809
|
+
"export function varName<R extends CssVarRef>(ref: R): CssVarProperty<R> {",
|
|
810
|
+
" return ref.slice(4, -1) as CssVarProperty<R>;",
|
|
748
811
|
"}"
|
|
749
812
|
];
|
|
750
813
|
var HELPERS_JS = [
|
|
@@ -758,13 +821,19 @@ var HELPERS_JS = [
|
|
|
758
821
|
"",
|
|
759
822
|
"export function withFallback(ref, fallback) {",
|
|
760
823
|
" return `${ref.slice(0, -1)}, ${fallback})`;",
|
|
824
|
+
"}",
|
|
825
|
+
"",
|
|
826
|
+
"export function varName(ref) {",
|
|
827
|
+
" return ref.slice(4, -1);",
|
|
761
828
|
"}"
|
|
762
829
|
];
|
|
763
830
|
var HELPERS_DTS = [
|
|
764
831
|
"export declare function getCssVar(ref: CssVarRef, el?: Element): string;",
|
|
765
832
|
"export declare function setCssVar(ref: CssVarRef, value: string, el?: HTMLElement): void;",
|
|
766
833
|
"export type CssVarWithFallback<R extends CssVarRef, F extends string | number> = R extends `var(${infer N})` ? `var(${N}, ${F})` : never;",
|
|
767
|
-
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;"
|
|
834
|
+
"export declare function withFallback<R extends CssVarRef, F extends string | number>(ref: R, fallback: F): CssVarWithFallback<R, F>;",
|
|
835
|
+
"export type CssVarProperty<R extends CssVarRef> = R extends `var(${infer N})` ? N : never;",
|
|
836
|
+
"export declare function varName<R extends CssVarRef>(ref: R): CssVarProperty<R>;"
|
|
768
837
|
];
|
|
769
838
|
function generateCode(varNames, ...args) {
|
|
770
839
|
const options = normalizeOptions(args);
|
|
@@ -887,9 +956,13 @@ function matchesIgnore(name, patterns) {
|
|
|
887
956
|
return patterns.some((pattern) => pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
|
|
888
957
|
}
|
|
889
958
|
async function findUndefinedRefs(options) {
|
|
890
|
-
const
|
|
959
|
+
const [css, source] = await Promise.all([
|
|
960
|
+
scanReferences(options.input, options.exclude),
|
|
961
|
+
options.refsSource ? scanReferences(options.refsSource, void 0, true) : null
|
|
962
|
+
]);
|
|
963
|
+
const declared = /* @__PURE__ */ new Set([...css.declared, ...source?.declared ?? []]);
|
|
891
964
|
const ignore = options.refsIgnore ?? [];
|
|
892
|
-
return references.filter((ref) => !declared.has(ref.name) && !matchesIgnore(ref.name, ignore));
|
|
965
|
+
return [...css.references, ...source?.references ?? []].filter((ref) => !declared.has(ref.name) && !matchesIgnore(ref.name, ignore));
|
|
893
966
|
}
|
|
894
967
|
async function computeOutputs(options) {
|
|
895
968
|
assertPruneCompatible(options);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-
|
|
2
|
-
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode, b as generateDeclaration, c as generateJs, d as generateTokens, p as parseDeclaredNames, e as parsePropertyRules, f as parseThemedDeclarations, h as parseVarDeclarations, i as parseVarNames, j as parseVarReferences } from './generator-
|
|
1
|
+
import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-BokM5zux.cjs';
|
|
2
|
+
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode, b as generateDeclaration, c as generateJs, d as generateTokens, p as parseDeclaredNames, e as parsePropertyRules, f as parseThemedDeclarations, h as parseVarDeclarations, i as parseVarNames, j as parseVarReferences } from './generator-BokM5zux.cjs';
|
|
3
3
|
|
|
4
4
|
interface ScannedCss {
|
|
5
5
|
declarations: Map<string, string>;
|
|
@@ -17,7 +17,7 @@ declare function scanVarNames(patterns: string | string[], exclude?: string | st
|
|
|
17
17
|
interface FileReference extends VarReference {
|
|
18
18
|
file: string;
|
|
19
19
|
}
|
|
20
|
-
declare function scanReferences(patterns: string | string[], exclude?: string | string[]): Promise<{
|
|
20
|
+
declare function scanReferences(patterns: string | string[], exclude?: string | string[], source?: boolean): Promise<{
|
|
21
21
|
references: FileReference[];
|
|
22
22
|
declared: Set<string>;
|
|
23
23
|
}>;
|
|
@@ -43,10 +43,15 @@ interface GenerateOptions {
|
|
|
43
43
|
/** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true, or when calling `findUnusedVars`. */
|
|
44
44
|
usage?: string | string[];
|
|
45
45
|
usageExclude?: string | string[];
|
|
46
|
-
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`
|
|
46
|
+
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
|
|
47
47
|
helpers?: boolean;
|
|
48
48
|
/** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
|
|
49
49
|
refsIgnore?: string[];
|
|
50
|
+
/**
|
|
51
|
+
* Glob(s) of source files (TS/JSX, Vue, HTML, ...) whose `var(--name)` references
|
|
52
|
+
* (and Tailwind `bg-(--name)` shorthands) `findUndefinedRefs` also checks.
|
|
53
|
+
*/
|
|
54
|
+
refsSource?: string | string[];
|
|
50
55
|
}
|
|
51
56
|
/** A CLI config file's options — like `GenerateOptions`, but anything may come from CLI flags instead. */
|
|
52
57
|
type Config = Partial<GenerateOptions>;
|
|
@@ -57,7 +62,7 @@ type Config = Partial<GenerateOptions>;
|
|
|
57
62
|
declare function defineConfig(config: Config): Config;
|
|
58
63
|
declare function defineConfig(config: Config[]): Config[];
|
|
59
64
|
declare function findUnusedVars(options: Pick<GenerateOptions, 'input' | 'exclude' | 'selectors' | 'themes' | 'prefix' | 'naming' | 'group' | 'usage' | 'usageExclude'>): Promise<string[]>;
|
|
60
|
-
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore'>): Promise<FileReference[]>;
|
|
65
|
+
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore' | 'refsSource'>): Promise<FileReference[]>;
|
|
61
66
|
declare function generate(options: GenerateOptions): Promise<boolean>;
|
|
62
67
|
declare function checkGenerated(options: GenerateOptions): Promise<boolean>;
|
|
63
68
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-
|
|
2
|
-
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode, b as generateDeclaration, c as generateJs, d as generateTokens, p as parseDeclaredNames, e as parsePropertyRules, f as parseThemedDeclarations, h as parseVarDeclarations, i as parseVarNames, j as parseVarReferences } from './generator-
|
|
1
|
+
import { V as VarReference, P as PropertyRule, a as VarDoc, N as NamingConvention } from './generator-BokM5zux.js';
|
|
2
|
+
export { G as GeneratorOptions, T as ThemedDeclarations, g as generateCode, b as generateDeclaration, c as generateJs, d as generateTokens, p as parseDeclaredNames, e as parsePropertyRules, f as parseThemedDeclarations, h as parseVarDeclarations, i as parseVarNames, j as parseVarReferences } from './generator-BokM5zux.js';
|
|
3
3
|
|
|
4
4
|
interface ScannedCss {
|
|
5
5
|
declarations: Map<string, string>;
|
|
@@ -17,7 +17,7 @@ declare function scanVarNames(patterns: string | string[], exclude?: string | st
|
|
|
17
17
|
interface FileReference extends VarReference {
|
|
18
18
|
file: string;
|
|
19
19
|
}
|
|
20
|
-
declare function scanReferences(patterns: string | string[], exclude?: string | string[]): Promise<{
|
|
20
|
+
declare function scanReferences(patterns: string | string[], exclude?: string | string[], source?: boolean): Promise<{
|
|
21
21
|
references: FileReference[];
|
|
22
22
|
declared: Set<string>;
|
|
23
23
|
}>;
|
|
@@ -43,10 +43,15 @@ interface GenerateOptions {
|
|
|
43
43
|
/** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true, or when calling `findUnusedVars`. */
|
|
44
44
|
usage?: string | string[];
|
|
45
45
|
usageExclude?: string | string[];
|
|
46
|
-
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`
|
|
46
|
+
/** Also emit the `CssVarRef` type and `getCssVar`/`setCssVar`/`withFallback`/`varName` helpers. */
|
|
47
47
|
helpers?: boolean;
|
|
48
48
|
/** Names (or `--prefix-*` patterns) that `findUndefinedRefs` shouldn't report, e.g. vars set from JS. */
|
|
49
49
|
refsIgnore?: string[];
|
|
50
|
+
/**
|
|
51
|
+
* Glob(s) of source files (TS/JSX, Vue, HTML, ...) whose `var(--name)` references
|
|
52
|
+
* (and Tailwind `bg-(--name)` shorthands) `findUndefinedRefs` also checks.
|
|
53
|
+
*/
|
|
54
|
+
refsSource?: string | string[];
|
|
50
55
|
}
|
|
51
56
|
/** A CLI config file's options — like `GenerateOptions`, but anything may come from CLI flags instead. */
|
|
52
57
|
type Config = Partial<GenerateOptions>;
|
|
@@ -57,7 +62,7 @@ type Config = Partial<GenerateOptions>;
|
|
|
57
62
|
declare function defineConfig(config: Config): Config;
|
|
58
63
|
declare function defineConfig(config: Config[]): Config[];
|
|
59
64
|
declare function findUnusedVars(options: Pick<GenerateOptions, 'input' | 'exclude' | 'selectors' | 'themes' | 'prefix' | 'naming' | 'group' | 'usage' | 'usageExclude'>): Promise<string[]>;
|
|
60
|
-
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore'>): Promise<FileReference[]>;
|
|
65
|
+
declare function findUndefinedRefs(options: Pick<GenerateOptions, 'input' | 'exclude' | 'refsIgnore' | 'refsSource'>): Promise<FileReference[]>;
|
|
61
66
|
declare function generate(options: GenerateOptions): Promise<boolean>;
|
|
62
67
|
declare function checkGenerated(options: GenerateOptions): Promise<boolean>;
|
|
63
68
|
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
findUndefinedRefs,
|
|
5
5
|
findUnusedVars,
|
|
6
6
|
generate
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-FWKCSH2F.js";
|
|
8
8
|
import {
|
|
9
9
|
generateCode,
|
|
10
10
|
generateDeclaration,
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
scanUsedKeys,
|
|
22
22
|
scanVarDeclarations,
|
|
23
23
|
scanVarNames
|
|
24
|
-
} from "./chunk-
|
|
24
|
+
} from "./chunk-XSFYQPH6.js";
|
|
25
25
|
export {
|
|
26
26
|
checkGenerated,
|
|
27
27
|
defineConfig,
|