css-typed-vars 0.13.1 → 0.14.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 +6 -6
- package/dist/{chunk-3QCGO2EN.js → chunk-7J7UZYHB.js} +1 -1
- package/dist/{chunk-L2T4LBKN.js → chunk-BPZRXE6W.js} +105 -15
- package/dist/{chunk-DIGUOQES.js → chunk-VWEQJVXA.js} +1 -1
- package/dist/cli.cjs +109 -14
- package/dist/cli.js +8 -3
- package/dist/esbuild.cjs +103 -13
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/{generator-BokM5zux.d.ts → generator-TJNk6ET_.d.cts} +5 -0
- package/dist/{generator-BokM5zux.d.cts → generator-TJNk6ET_.d.ts} +5 -0
- package/dist/index.cjs +97 -12
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/rollup.cjs +103 -13
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +103 -13
- package/dist/unplugin.d.cts +1 -1
- package/dist/unplugin.d.ts +1 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +103 -13
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +103 -13
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
|
|
38
38
|
`css-typed-vars` scans your CSS files and gives you typed constants. Use `cssVars.colorPrimary` instead — TypeScript catches missing variables at compile time.
|
|
39
39
|
|
|
40
|
-
Each generated key also carries a `/** @default ... */` comment with the property's declared CSS value, so your editor shows it on hover and autocomplete. If the variable is registered with [`@property`](#property-syntax-typing), its `syntax` descriptor shows up too, and an enum-like syntax (e.g. `"light | dark | system"`) gets its own exported TS union type.
|
|
40
|
+
Each generated key also carries a `/** @default ... */` comment with the property's declared CSS value, so your editor shows it on hover and autocomplete. A value written over several lines (a font stack, a list of shadows) is joined into one. If the variable is registered with [`@property`](#property-syntax-typing), its `syntax` descriptor shows up too, and an enum-like syntax (e.g. `"light | dark | system"`) gets its own exported TS union type.
|
|
41
41
|
|
|
42
42
|
## Installation
|
|
43
43
|
|
|
@@ -133,9 +133,9 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prune-check
|
|
|
133
133
|
| `--config` | Config file to use instead of looking one up in the working directory — see [Config file](#config-file) |
|
|
134
134
|
| `--input` | Glob pattern for CSS/SCSS/Less files (repeatable: `--input "src/**/*.css" --input "lib/**/*.css"`) |
|
|
135
135
|
| `--output` | Output file. `.ts` → TypeScript, `.js` → JavaScript + `.d.ts` alongside, `.json` → [design tokens](#design-tokens-dtcg) |
|
|
136
|
-
| `--exclude` | Glob pattern for files to exclude (repeatable: `--exclude "**/a/**" --exclude "**/b/**"`) |
|
|
136
|
+
| `--exclude` | Glob pattern for files to exclude (repeatable: `--exclude "**/a/**" --exclude "**/b/**"`). A negated `--input` glob excludes too: `--input "!**/vendor/**"` |
|
|
137
137
|
| `--prefix` | Prefix for generated keys: `--prefix theme` → `themeColorPrimary` |
|
|
138
|
-
| `--naming` | Key naming: `camelCase` (default), `snake`, `kebab`, `constant`, `pascal` |
|
|
138
|
+
| `--naming` | Key naming: `camelCase` (default), `snake`, `kebab`, `constant`, `pascal`. Another spelling of one (`snake_case`, `PascalCase`) is rejected with a hint at the value to use |
|
|
139
139
|
| `--selector` | Extra CSS selector to scan for variables (repeatable: `--selector ".dark" --selector "[data-theme='dark']"`) |
|
|
140
140
|
| `--theme` | Named theme block, kept out of the default value and emitted as `@theme <name> <value>` JSDoc tags (repeatable: `--theme dark='[data-theme="dark"]' --theme contrast='@media (prefers-contrast: more)'`) — see [Theme values](#theme-values) |
|
|
141
141
|
| `--group` | Nest keys under an object named after each variable's first hyphen segment: `--color-primary`/`--color-secondary` → `cssVars.color.primary`/`cssVars.color.secondary`. A variable with no hyphen stays top-level. Can't be combined with `--prefix`. |
|
|
@@ -362,7 +362,7 @@ Turbopack does not yet have a public plugin API for virtual modules. Use the CLI
|
|
|
362
362
|
| Option | Type | Default | Description |
|
|
363
363
|
|--------|------|---------|-------------|
|
|
364
364
|
| `input` | `string \| string[]` | — | Glob pattern for CSS/SCSS/Less files |
|
|
365
|
-
| `exclude` | `string \| string[]` | — | Glob pattern(s) for files to exclude |
|
|
365
|
+
| `exclude` | `string \| string[]` | — | Glob pattern(s) for files to exclude. A negated `input` glob (`'!**/vendor/**'`) excludes too |
|
|
366
366
|
| `prefix` | `string` | — | Prefix for generated keys: `'theme'` → `themeColorPrimary` |
|
|
367
367
|
| `naming` | `'camelCase' \| 'snake' \| 'kebab' \| 'constant' \| 'pascal'` | `'camelCase'` | Key naming convention |
|
|
368
368
|
| `selectors` | `string[]` | — | Extra CSS selectors to scan, e.g. `['.dark', '[data-theme="dark"]']` |
|
|
@@ -497,7 +497,7 @@ By default, variables are scanned from `:root {}` blocks (including attribute se
|
|
|
497
497
|
|
|
498
498
|
When a variable is declared in several files, the last file read wins. Files are read in `input` order (a file matched by several globs counts at the last one), and within a glob a directory's files come before its subdirectories', alphabetically — so `styles/themes/dark.css` overrides `styles/tokens.css`.
|
|
499
499
|
|
|
500
|
-
By default, a variable redeclared in another block (`:root[data-theme="dark"]`, or a `selectors` entry) simply overwrites its `@default` value — last one wins. The `themes` option names those blocks instead, so each theme's value is kept separately:
|
|
500
|
+
By default, a variable redeclared in another block (`:root[data-theme="dark"]`, or a `selectors` entry) simply overwrites its `@default` value — last one wins. A conditional declaration is the exception: one inside an `@media`, `@supports` or `@container` rule, or in a rule nested in the block (`:root { .compact & { --gap: 2px; } }`), doesn't replace an unconditional one, in the same file or another — it's only used when the variable has no other value. The `themes` option names those blocks instead, so each theme's value is kept separately:
|
|
501
501
|
|
|
502
502
|
```js
|
|
503
503
|
// css-typed-vars.config.js
|
|
@@ -532,7 +532,7 @@ export const cssVars = {
|
|
|
532
532
|
|
|
533
533
|
- Theme blocks are kept out of the default value, so `@default` always shows the base (`:root`/`@theme`/`selectors`) value.
|
|
534
534
|
- A variable declared only inside a theme (`--glow` above) is still generated, with just its theme tags.
|
|
535
|
-
- An `@media` theme matches by its prelude, ignoring whitespace and case. Other `@media` blocks are read as before.
|
|
535
|
+
- An `@media` theme matches by its prelude, ignoring whitespace and case, whether it wraps the `:root`/`@theme` block or is nested in it (`:root { @media (prefers-color-scheme: dark) { --bg: #000; } }`). Other `@media` blocks are read as before.
|
|
536
536
|
- On the CLI, pass `--theme <name>=<selector>` (repeatable): `--theme dark='[data-theme="dark"]'`.
|
|
537
537
|
|
|
538
538
|
## Doc comments
|
|
@@ -142,7 +142,18 @@ function extractValue(text, start) {
|
|
|
142
142
|
else if (char === open[open.length - 1]) open.pop();
|
|
143
143
|
else if (char === "}" || char === ";" && open.length === 0) break;
|
|
144
144
|
}
|
|
145
|
-
return text.slice(start, i).trim();
|
|
145
|
+
return joinLines(text.slice(start, i).trim());
|
|
146
|
+
}
|
|
147
|
+
function joinLines(value) {
|
|
148
|
+
if (!/[\n\r]/.test(value)) return value;
|
|
149
|
+
const masked = maskQuotedContent(value);
|
|
150
|
+
let result = "";
|
|
151
|
+
let last = 0;
|
|
152
|
+
for (const match of masked.matchAll(/\s*[\n\r]\s*/g)) {
|
|
153
|
+
result += value.slice(last, match.index) + " ";
|
|
154
|
+
last = match.index + match[0].length;
|
|
155
|
+
}
|
|
156
|
+
return result + value.slice(last);
|
|
146
157
|
}
|
|
147
158
|
var IMPORTANT = /\s*!\s*important$/i;
|
|
148
159
|
var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
|
|
@@ -154,6 +165,55 @@ function forEachProperty(block, onProperty, offset = 0) {
|
|
|
154
165
|
onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
|
|
155
166
|
}
|
|
156
167
|
}
|
|
168
|
+
var inRanges = (ranges, position) => ranges.some(([start, end]) => position >= start && position < end);
|
|
169
|
+
function nestedRanges(block) {
|
|
170
|
+
const masked = maskQuotedContent(block);
|
|
171
|
+
const ranges = [];
|
|
172
|
+
const statement = /\s*(--[\w-]+\s*:)?/y;
|
|
173
|
+
let i = 0;
|
|
174
|
+
while (i < block.length) {
|
|
175
|
+
statement.lastIndex = i;
|
|
176
|
+
const match = statement.exec(masked);
|
|
177
|
+
i = statement.lastIndex;
|
|
178
|
+
if (match[1]) {
|
|
179
|
+
const raw = block.slice(i);
|
|
180
|
+
i += raw.length - raw.trimStart().length + extractValue(block, i).length;
|
|
181
|
+
while (i < block.length && block[i] !== ";" && block[i] !== "}") i++;
|
|
182
|
+
i++;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
for (let depth = 0; i < block.length; i++) {
|
|
186
|
+
const char = masked[i];
|
|
187
|
+
if (char === "(") depth++;
|
|
188
|
+
else if (char === ")") depth = Math.max(0, depth - 1);
|
|
189
|
+
else if (depth === 0 && (char === ";" || char === "{" || char === "}")) break;
|
|
190
|
+
}
|
|
191
|
+
if (masked[i] === "{") {
|
|
192
|
+
const body = extractBlock(block, i + 1);
|
|
193
|
+
ranges.push([i + 1, i + 1 + body.length]);
|
|
194
|
+
i += body.length + 1;
|
|
195
|
+
}
|
|
196
|
+
i++;
|
|
197
|
+
}
|
|
198
|
+
return ranges;
|
|
199
|
+
}
|
|
200
|
+
function forEachOwnProperty(block, onProperty, offset = 0) {
|
|
201
|
+
const nested = nestedRanges(block);
|
|
202
|
+
forEachProperty(block, (name, value, position) => {
|
|
203
|
+
if (!inRanges(nested, position - offset)) onProperty(name, value, position);
|
|
204
|
+
}, offset);
|
|
205
|
+
}
|
|
206
|
+
function conditionalRanges(stripped) {
|
|
207
|
+
const masked = maskQuotedContent(stripped);
|
|
208
|
+
const ranges = [];
|
|
209
|
+
const openRegex = /@(?:media|supports|container)\b[^{;]*\{/gi;
|
|
210
|
+
let match;
|
|
211
|
+
while (match = openRegex.exec(masked)) {
|
|
212
|
+
const start = match.index + match[0].length;
|
|
213
|
+
ranges.push([start, start + extractBlock(stripped, start).length]);
|
|
214
|
+
}
|
|
215
|
+
return ranges;
|
|
216
|
+
}
|
|
157
217
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
158
218
|
for (const sel of selectors) {
|
|
159
219
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -170,19 +230,35 @@ function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
|
170
230
|
}
|
|
171
231
|
var BASE_SELECTORS = [":root", "@theme"];
|
|
172
232
|
function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
|
|
233
|
+
const conditional = conditionalRanges(stripped);
|
|
173
234
|
forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
|
|
174
|
-
|
|
175
|
-
forEachProperty(block,
|
|
235
|
+
const nested = nestedRanges(block);
|
|
236
|
+
forEachProperty(block, (name, value, position) => {
|
|
237
|
+
if (inRanges(skip, position)) return;
|
|
238
|
+
onProperty(name, value, position, inRanges(conditional, position) || inRanges(nested, position - bodyStart));
|
|
239
|
+
}, bodyStart);
|
|
176
240
|
});
|
|
177
241
|
}
|
|
242
|
+
function makeDeclarationCollector() {
|
|
243
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
244
|
+
const conditional = /* @__PURE__ */ new Set();
|
|
245
|
+
const add = (name, value, isConditional) => {
|
|
246
|
+
if (isConditional && declarations.has(name) && !conditional.has(name)) return false;
|
|
247
|
+
declarations.set(name, value);
|
|
248
|
+
if (isConditional) conditional.add(name);
|
|
249
|
+
else conditional.delete(name);
|
|
250
|
+
return true;
|
|
251
|
+
};
|
|
252
|
+
return { declarations, conditional, add };
|
|
253
|
+
}
|
|
178
254
|
function parseVarNames(css, selectors) {
|
|
179
255
|
const names = /* @__PURE__ */ new Set();
|
|
180
256
|
scanDeclarations(css, selectors, (name) => names.add(name));
|
|
181
257
|
return [...names];
|
|
182
258
|
}
|
|
183
259
|
function parseVarDeclarations(css, selectors) {
|
|
184
|
-
const declarations
|
|
185
|
-
scanDeclarations(css, selectors, (name, value) =>
|
|
260
|
+
const { declarations, add } = makeDeclarationCollector();
|
|
261
|
+
scanDeclarations(css, selectors, (name, value, _position, conditional) => add(name, value, conditional));
|
|
186
262
|
return declarations;
|
|
187
263
|
}
|
|
188
264
|
var normalizePrelude = (prelude) => prelude.replace(/\s+/g, "").toLowerCase();
|
|
@@ -240,15 +316,17 @@ function parseThemedDeclarations(css, selectors, themes) {
|
|
|
240
316
|
for (const [themeName, themeSelector] of Object.entries(themes)) {
|
|
241
317
|
const values = /* @__PURE__ */ new Map();
|
|
242
318
|
themeValues.set(themeName, values);
|
|
243
|
-
const
|
|
319
|
+
const onProperty = (name, value, position) => {
|
|
244
320
|
values.set(name, value);
|
|
245
321
|
const doc = docAt(position);
|
|
246
322
|
if (doc) themeDocs.set(name, doc);
|
|
247
|
-
}
|
|
323
|
+
};
|
|
324
|
+
const collect = (block, offset) => forEachProperty(block, onProperty, offset);
|
|
248
325
|
const selector = themeSelector.trim();
|
|
249
326
|
if (/^@media\b/i.test(selector)) {
|
|
250
327
|
forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
|
|
251
328
|
skip.push(range);
|
|
329
|
+
forEachOwnProperty(mediaBlock, onProperty, range[0]);
|
|
252
330
|
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
|
|
253
331
|
});
|
|
254
332
|
} else {
|
|
@@ -258,17 +336,17 @@ function parseThemedDeclarations(css, selectors, themes) {
|
|
|
258
336
|
});
|
|
259
337
|
}
|
|
260
338
|
}
|
|
261
|
-
const declarations
|
|
339
|
+
const { declarations, conditional, add } = makeDeclarationCollector();
|
|
262
340
|
const docs = /* @__PURE__ */ new Map();
|
|
263
|
-
scanDeclarations(css, selectors, (name, value, position) => {
|
|
264
|
-
|
|
341
|
+
scanDeclarations(css, selectors, (name, value, position, isConditional) => {
|
|
342
|
+
if (!add(name, value, isConditional)) return;
|
|
265
343
|
const doc = docAt(position);
|
|
266
344
|
if (doc) docs.set(name, doc);
|
|
267
345
|
}, skip, stripped);
|
|
268
346
|
for (const [name, doc] of themeDocs) {
|
|
269
347
|
if (!docs.has(name)) docs.set(name, doc);
|
|
270
348
|
}
|
|
271
|
-
return { declarations, themes: themeValues, docs };
|
|
349
|
+
return { declarations, conditional, themes: themeValues, docs };
|
|
272
350
|
}
|
|
273
351
|
var PROPERTY_DESCRIPTORS = [
|
|
274
352
|
["syntax", "syntax"],
|
|
@@ -395,9 +473,13 @@ async function findFiles(patterns, exclude) {
|
|
|
395
473
|
return [...new Set(files.reverse())].reverse();
|
|
396
474
|
}
|
|
397
475
|
function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
398
|
-
const compile = (globs) => picomatch(
|
|
399
|
-
const
|
|
400
|
-
const
|
|
476
|
+
const compile = (globs) => picomatch(globs.map((p) => p.replace(/^\.\//, "")));
|
|
477
|
+
const normalized = normalizePatterns(patterns);
|
|
478
|
+
const include = compile(normalized.filter((p) => !p.startsWith("!")));
|
|
479
|
+
const ignore = compile([
|
|
480
|
+
...exclude ? normalizePatterns(exclude) : [],
|
|
481
|
+
...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
|
|
482
|
+
]);
|
|
401
483
|
return (file) => {
|
|
402
484
|
const absolute = resolve(cwd, file).replace(/\\/g, "/");
|
|
403
485
|
const fromCwd = relative(cwd, absolute).replace(/\\/g, "/");
|
|
@@ -408,6 +490,7 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
|
408
490
|
function globWatchRoots(patterns, cwd = process.cwd()) {
|
|
409
491
|
const roots = /* @__PURE__ */ new Map();
|
|
410
492
|
for (const pattern of normalizePatterns(patterns)) {
|
|
493
|
+
if (pattern.startsWith("!")) continue;
|
|
411
494
|
const { base, glob: glob2, isGlob } = picomatch.scan(pattern);
|
|
412
495
|
const dir = resolve(cwd, isGlob ? base || "." : dirname2(pattern));
|
|
413
496
|
const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
|
|
@@ -442,12 +525,19 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
442
525
|
})
|
|
443
526
|
);
|
|
444
527
|
const declarations = /* @__PURE__ */ new Map();
|
|
528
|
+
const conditional = /* @__PURE__ */ new Set();
|
|
445
529
|
const properties = /* @__PURE__ */ new Map();
|
|
446
530
|
const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
447
531
|
const docs = /* @__PURE__ */ new Map();
|
|
448
532
|
for (const result of perFile) {
|
|
449
533
|
if (!result) continue;
|
|
450
|
-
for (const [name, value] of result.declarations)
|
|
534
|
+
for (const [name, value] of result.declarations) {
|
|
535
|
+
const isConditional = result.conditional.has(name);
|
|
536
|
+
if (isConditional && declarations.has(name) && !conditional.has(name)) continue;
|
|
537
|
+
declarations.set(name, value);
|
|
538
|
+
if (isConditional) conditional.add(name);
|
|
539
|
+
else conditional.delete(name);
|
|
540
|
+
}
|
|
451
541
|
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
452
542
|
for (const [name, doc] of result.docs) docs.set(name, doc);
|
|
453
543
|
for (const [theme, values] of result.themes) {
|
package/dist/cli.cjs
CHANGED
|
@@ -165,7 +165,18 @@ function extractValue(text, start) {
|
|
|
165
165
|
else if (char === open[open.length - 1]) open.pop();
|
|
166
166
|
else if (char === "}" || char === ";" && open.length === 0) break;
|
|
167
167
|
}
|
|
168
|
-
return text.slice(start, i).trim();
|
|
168
|
+
return joinLines(text.slice(start, i).trim());
|
|
169
|
+
}
|
|
170
|
+
function joinLines(value) {
|
|
171
|
+
if (!/[\n\r]/.test(value)) return value;
|
|
172
|
+
const masked = maskQuotedContent(value);
|
|
173
|
+
let result = "";
|
|
174
|
+
let last = 0;
|
|
175
|
+
for (const match of masked.matchAll(/\s*[\n\r]\s*/g)) {
|
|
176
|
+
result += value.slice(last, match.index) + " ";
|
|
177
|
+
last = match.index + match[0].length;
|
|
178
|
+
}
|
|
179
|
+
return result + value.slice(last);
|
|
169
180
|
}
|
|
170
181
|
var IMPORTANT = /\s*!\s*important$/i;
|
|
171
182
|
var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
|
|
@@ -177,6 +188,55 @@ function forEachProperty(block, onProperty, offset = 0) {
|
|
|
177
188
|
onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
|
|
178
189
|
}
|
|
179
190
|
}
|
|
191
|
+
var inRanges = (ranges, position) => ranges.some(([start, end]) => position >= start && position < end);
|
|
192
|
+
function nestedRanges(block) {
|
|
193
|
+
const masked = maskQuotedContent(block);
|
|
194
|
+
const ranges = [];
|
|
195
|
+
const statement = /\s*(--[\w-]+\s*:)?/y;
|
|
196
|
+
let i = 0;
|
|
197
|
+
while (i < block.length) {
|
|
198
|
+
statement.lastIndex = i;
|
|
199
|
+
const match = statement.exec(masked);
|
|
200
|
+
i = statement.lastIndex;
|
|
201
|
+
if (match[1]) {
|
|
202
|
+
const raw = block.slice(i);
|
|
203
|
+
i += raw.length - raw.trimStart().length + extractValue(block, i).length;
|
|
204
|
+
while (i < block.length && block[i] !== ";" && block[i] !== "}") i++;
|
|
205
|
+
i++;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
for (let depth = 0; i < block.length; i++) {
|
|
209
|
+
const char = masked[i];
|
|
210
|
+
if (char === "(") depth++;
|
|
211
|
+
else if (char === ")") depth = Math.max(0, depth - 1);
|
|
212
|
+
else if (depth === 0 && (char === ";" || char === "{" || char === "}")) break;
|
|
213
|
+
}
|
|
214
|
+
if (masked[i] === "{") {
|
|
215
|
+
const body = extractBlock(block, i + 1);
|
|
216
|
+
ranges.push([i + 1, i + 1 + body.length]);
|
|
217
|
+
i += body.length + 1;
|
|
218
|
+
}
|
|
219
|
+
i++;
|
|
220
|
+
}
|
|
221
|
+
return ranges;
|
|
222
|
+
}
|
|
223
|
+
function forEachOwnProperty(block, onProperty, offset = 0) {
|
|
224
|
+
const nested = nestedRanges(block);
|
|
225
|
+
forEachProperty(block, (name, value, position) => {
|
|
226
|
+
if (!inRanges(nested, position - offset)) onProperty(name, value, position);
|
|
227
|
+
}, offset);
|
|
228
|
+
}
|
|
229
|
+
function conditionalRanges(stripped) {
|
|
230
|
+
const masked = maskQuotedContent(stripped);
|
|
231
|
+
const ranges = [];
|
|
232
|
+
const openRegex = /@(?:media|supports|container)\b[^{;]*\{/gi;
|
|
233
|
+
let match;
|
|
234
|
+
while (match = openRegex.exec(masked)) {
|
|
235
|
+
const start = match.index + match[0].length;
|
|
236
|
+
ranges.push([start, start + extractBlock(stripped, start).length]);
|
|
237
|
+
}
|
|
238
|
+
return ranges;
|
|
239
|
+
}
|
|
180
240
|
function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
181
241
|
for (const sel of selectors) {
|
|
182
242
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -193,11 +253,27 @@ function forEachSelectorBlock(stripped, selectors, onBlock) {
|
|
|
193
253
|
}
|
|
194
254
|
var BASE_SELECTORS = [":root", "@theme"];
|
|
195
255
|
function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
|
|
256
|
+
const conditional = conditionalRanges(stripped);
|
|
196
257
|
forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
|
|
197
|
-
|
|
198
|
-
forEachProperty(block,
|
|
258
|
+
const nested = nestedRanges(block);
|
|
259
|
+
forEachProperty(block, (name, value, position) => {
|
|
260
|
+
if (inRanges(skip, position)) return;
|
|
261
|
+
onProperty(name, value, position, inRanges(conditional, position) || inRanges(nested, position - bodyStart));
|
|
262
|
+
}, bodyStart);
|
|
199
263
|
});
|
|
200
264
|
}
|
|
265
|
+
function makeDeclarationCollector() {
|
|
266
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
267
|
+
const conditional = /* @__PURE__ */ new Set();
|
|
268
|
+
const add = (name, value, isConditional) => {
|
|
269
|
+
if (isConditional && declarations.has(name) && !conditional.has(name)) return false;
|
|
270
|
+
declarations.set(name, value);
|
|
271
|
+
if (isConditional) conditional.add(name);
|
|
272
|
+
else conditional.delete(name);
|
|
273
|
+
return true;
|
|
274
|
+
};
|
|
275
|
+
return { declarations, conditional, add };
|
|
276
|
+
}
|
|
201
277
|
var normalizePrelude = (prelude) => prelude.replace(/\s+/g, "").toLowerCase();
|
|
202
278
|
function forEachMediaBlock(stripped, media, onBlock) {
|
|
203
279
|
const wanted = normalizePrelude(media.replace(/^@media/i, ""));
|
|
@@ -253,15 +329,17 @@ function parseThemedDeclarations(css, selectors, themes) {
|
|
|
253
329
|
for (const [themeName, themeSelector] of Object.entries(themes)) {
|
|
254
330
|
const values = /* @__PURE__ */ new Map();
|
|
255
331
|
themeValues.set(themeName, values);
|
|
256
|
-
const
|
|
332
|
+
const onProperty = (name, value, position) => {
|
|
257
333
|
values.set(name, value);
|
|
258
334
|
const doc = docAt(position);
|
|
259
335
|
if (doc) themeDocs.set(name, doc);
|
|
260
|
-
}
|
|
336
|
+
};
|
|
337
|
+
const collect = (block, offset) => forEachProperty(block, onProperty, offset);
|
|
261
338
|
const selector = themeSelector.trim();
|
|
262
339
|
if (/^@media\b/i.test(selector)) {
|
|
263
340
|
forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
|
|
264
341
|
skip.push(range);
|
|
342
|
+
forEachOwnProperty(mediaBlock, onProperty, range[0]);
|
|
265
343
|
forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
|
|
266
344
|
});
|
|
267
345
|
} else {
|
|
@@ -271,17 +349,17 @@ function parseThemedDeclarations(css, selectors, themes) {
|
|
|
271
349
|
});
|
|
272
350
|
}
|
|
273
351
|
}
|
|
274
|
-
const declarations
|
|
352
|
+
const { declarations, conditional, add } = makeDeclarationCollector();
|
|
275
353
|
const docs = /* @__PURE__ */ new Map();
|
|
276
|
-
scanDeclarations(css, selectors, (name, value, position) => {
|
|
277
|
-
|
|
354
|
+
scanDeclarations(css, selectors, (name, value, position, isConditional) => {
|
|
355
|
+
if (!add(name, value, isConditional)) return;
|
|
278
356
|
const doc = docAt(position);
|
|
279
357
|
if (doc) docs.set(name, doc);
|
|
280
358
|
}, skip, stripped);
|
|
281
359
|
for (const [name, doc] of themeDocs) {
|
|
282
360
|
if (!docs.has(name)) docs.set(name, doc);
|
|
283
361
|
}
|
|
284
|
-
return { declarations, themes: themeValues, docs };
|
|
362
|
+
return { declarations, conditional, themes: themeValues, docs };
|
|
285
363
|
}
|
|
286
364
|
var PROPERTY_DESCRIPTORS = [
|
|
287
365
|
["syntax", "syntax"],
|
|
@@ -404,9 +482,13 @@ async function findFiles(patterns, exclude) {
|
|
|
404
482
|
return [...new Set(files.reverse())].reverse();
|
|
405
483
|
}
|
|
406
484
|
function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
407
|
-
const compile = (globs) => (0, import_picomatch.default)(
|
|
408
|
-
const
|
|
409
|
-
const
|
|
485
|
+
const compile = (globs) => (0, import_picomatch.default)(globs.map((p) => p.replace(/^\.\//, "")));
|
|
486
|
+
const normalized = normalizePatterns(patterns);
|
|
487
|
+
const include = compile(normalized.filter((p) => !p.startsWith("!")));
|
|
488
|
+
const ignore = compile([
|
|
489
|
+
...exclude ? normalizePatterns(exclude) : [],
|
|
490
|
+
...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
|
|
491
|
+
]);
|
|
410
492
|
return (file) => {
|
|
411
493
|
const absolute = (0, import_node_path.resolve)(cwd, file).replace(/\\/g, "/");
|
|
412
494
|
const fromCwd = (0, import_node_path.relative)(cwd, absolute).replace(/\\/g, "/");
|
|
@@ -417,6 +499,7 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
|
|
|
417
499
|
function globWatchRoots(patterns, cwd = process.cwd()) {
|
|
418
500
|
const roots = /* @__PURE__ */ new Map();
|
|
419
501
|
for (const pattern of normalizePatterns(patterns)) {
|
|
502
|
+
if (pattern.startsWith("!")) continue;
|
|
420
503
|
const { base, glob: glob2, isGlob } = import_picomatch.default.scan(pattern);
|
|
421
504
|
const dir = (0, import_node_path.resolve)(cwd, isGlob ? base || "." : (0, import_node_path.dirname)(pattern));
|
|
422
505
|
const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
|
|
@@ -448,12 +531,19 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
|
|
|
448
531
|
})
|
|
449
532
|
);
|
|
450
533
|
const declarations = /* @__PURE__ */ new Map();
|
|
534
|
+
const conditional = /* @__PURE__ */ new Set();
|
|
451
535
|
const properties = /* @__PURE__ */ new Map();
|
|
452
536
|
const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
453
537
|
const docs = /* @__PURE__ */ new Map();
|
|
454
538
|
for (const result of perFile) {
|
|
455
539
|
if (!result) continue;
|
|
456
|
-
for (const [name, value] of result.declarations)
|
|
540
|
+
for (const [name, value] of result.declarations) {
|
|
541
|
+
const isConditional = result.conditional.has(name);
|
|
542
|
+
if (isConditional && declarations.has(name) && !conditional.has(name)) continue;
|
|
543
|
+
declarations.set(name, value);
|
|
544
|
+
if (isConditional) conditional.add(name);
|
|
545
|
+
else conditional.delete(name);
|
|
546
|
+
}
|
|
457
547
|
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
458
548
|
for (const [name, doc] of result.docs) docs.set(name, doc);
|
|
459
549
|
for (const [theme, values] of result.themes) {
|
|
@@ -1162,6 +1252,11 @@ function suggest(value, candidates) {
|
|
|
1162
1252
|
const closest = candidates.reduce((best, candidate) => editDistance(value, candidate) < editDistance(value, best) ? candidate : best);
|
|
1163
1253
|
return editDistance(value, closest) <= 2 ? ` Did you mean ${closest}?` : "";
|
|
1164
1254
|
}
|
|
1255
|
+
function suggestNaming(value) {
|
|
1256
|
+
const bare = value.toLowerCase().replace(/[-_ ]?case$/, "");
|
|
1257
|
+
const match = VALID_NAMINGS.find((naming) => naming.toLowerCase().replace(/case$/, "") === bare);
|
|
1258
|
+
return match ? ` Did you mean ${match}?` : suggest(value, VALID_NAMINGS);
|
|
1259
|
+
}
|
|
1165
1260
|
function validateArgs() {
|
|
1166
1261
|
for (let i = 0; i < args.length; i++) {
|
|
1167
1262
|
const arg = args[i];
|
|
@@ -1281,7 +1376,7 @@ function validateModes() {
|
|
|
1281
1376
|
function validateTarget({ options, tag }, single) {
|
|
1282
1377
|
const { input, output, naming, group, prefix, prune, usage } = options;
|
|
1283
1378
|
if (naming !== void 0 && !VALID_NAMINGS.includes(naming)) {
|
|
1284
|
-
throw new CliError(`${tag}: invalid --naming value "${naming}"
|
|
1379
|
+
throw new CliError(`${tag}: invalid --naming value "${naming}".${suggestNaming(String(naming))} Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
1285
1380
|
}
|
|
1286
1381
|
if (lintRefsMode) {
|
|
1287
1382
|
if (!input) throw new CliError(`${tag}: --lint-refs requires --input <glob> (or "input" in the config file).`);
|
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-VWEQJVXA.js";
|
|
8
8
|
import {
|
|
9
9
|
VALID_NAMINGS,
|
|
10
10
|
createFileMatcher,
|
|
11
11
|
globWatchRoots
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-BPZRXE6W.js";
|
|
13
13
|
|
|
14
14
|
// src/cli.ts
|
|
15
15
|
import { access, readFile } from "fs/promises";
|
|
@@ -145,6 +145,11 @@ function suggest(value, candidates) {
|
|
|
145
145
|
const closest = candidates.reduce((best, candidate) => editDistance(value, candidate) < editDistance(value, best) ? candidate : best);
|
|
146
146
|
return editDistance(value, closest) <= 2 ? ` Did you mean ${closest}?` : "";
|
|
147
147
|
}
|
|
148
|
+
function suggestNaming(value) {
|
|
149
|
+
const bare = value.toLowerCase().replace(/[-_ ]?case$/, "");
|
|
150
|
+
const match = VALID_NAMINGS.find((naming) => naming.toLowerCase().replace(/case$/, "") === bare);
|
|
151
|
+
return match ? ` Did you mean ${match}?` : suggest(value, VALID_NAMINGS);
|
|
152
|
+
}
|
|
148
153
|
function validateArgs() {
|
|
149
154
|
for (let i = 0; i < args.length; i++) {
|
|
150
155
|
const arg = args[i];
|
|
@@ -264,7 +269,7 @@ function validateModes() {
|
|
|
264
269
|
function validateTarget({ options, tag }, single) {
|
|
265
270
|
const { input, output, naming, group, prefix, prune, usage } = options;
|
|
266
271
|
if (naming !== void 0 && !VALID_NAMINGS.includes(naming)) {
|
|
267
|
-
throw new CliError(`${tag}: invalid --naming value "${naming}"
|
|
272
|
+
throw new CliError(`${tag}: invalid --naming value "${naming}".${suggestNaming(String(naming))} Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
268
273
|
}
|
|
269
274
|
if (lintRefsMode) {
|
|
270
275
|
if (!input) throw new CliError(`${tag}: --lint-refs requires --input <glob> (or "input" in the config file).`);
|