css-typed-vars 0.13.0 → 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/dist/webpack.cjs CHANGED
@@ -183,17 +183,78 @@ function extractValue(text, start) {
183
183
  else if (char === open[open.length - 1]) open.pop();
184
184
  else if (char === "}" || char === ";" && open.length === 0) break;
185
185
  }
186
- return text.slice(start, i).trim();
186
+ return joinLines(text.slice(start, i).trim());
187
+ }
188
+ function joinLines(value) {
189
+ if (!/[\n\r]/.test(value)) return value;
190
+ const masked = maskQuotedContent(value);
191
+ let result = "";
192
+ let last = 0;
193
+ for (const match of masked.matchAll(/\s*[\n\r]\s*/g)) {
194
+ result += value.slice(last, match.index) + " ";
195
+ last = match.index + match[0].length;
196
+ }
197
+ return result + value.slice(last);
187
198
  }
188
199
  var IMPORTANT = /\s*!\s*important$/i;
200
+ var DECLARATION_NAME = /(?<=(?:^|[{;}])\s*)--[\w-]+(?=\s*:)/g;
189
201
  function forEachProperty(block, onProperty, offset = 0) {
190
202
  const masked = maskQuotedContent(block);
191
- for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
203
+ for (const prop of masked.matchAll(DECLARATION_NAME)) {
192
204
  const name = prop[0];
193
205
  const colonIndex = block.indexOf(":", prop.index + name.length);
194
206
  onProperty(name, extractValue(block, colonIndex + 1).replace(IMPORTANT, ""), offset + prop.index);
195
207
  }
196
208
  }
209
+ var inRanges = (ranges, position) => ranges.some(([start, end]) => position >= start && position < end);
210
+ function nestedRanges(block) {
211
+ const masked = maskQuotedContent(block);
212
+ const ranges = [];
213
+ const statement = /\s*(--[\w-]+\s*:)?/y;
214
+ let i = 0;
215
+ while (i < block.length) {
216
+ statement.lastIndex = i;
217
+ const match = statement.exec(masked);
218
+ i = statement.lastIndex;
219
+ if (match[1]) {
220
+ const raw = block.slice(i);
221
+ i += raw.length - raw.trimStart().length + extractValue(block, i).length;
222
+ while (i < block.length && block[i] !== ";" && block[i] !== "}") i++;
223
+ i++;
224
+ continue;
225
+ }
226
+ for (let depth = 0; i < block.length; i++) {
227
+ const char = masked[i];
228
+ if (char === "(") depth++;
229
+ else if (char === ")") depth = Math.max(0, depth - 1);
230
+ else if (depth === 0 && (char === ";" || char === "{" || char === "}")) break;
231
+ }
232
+ if (masked[i] === "{") {
233
+ const body = extractBlock(block, i + 1);
234
+ ranges.push([i + 1, i + 1 + body.length]);
235
+ i += body.length + 1;
236
+ }
237
+ i++;
238
+ }
239
+ return ranges;
240
+ }
241
+ function forEachOwnProperty(block, onProperty, offset = 0) {
242
+ const nested = nestedRanges(block);
243
+ forEachProperty(block, (name, value, position) => {
244
+ if (!inRanges(nested, position - offset)) onProperty(name, value, position);
245
+ }, offset);
246
+ }
247
+ function conditionalRanges(stripped) {
248
+ const masked = maskQuotedContent(stripped);
249
+ const ranges = [];
250
+ const openRegex = /@(?:media|supports|container)\b[^{;]*\{/gi;
251
+ let match;
252
+ while (match = openRegex.exec(masked)) {
253
+ const start = match.index + match[0].length;
254
+ ranges.push([start, start + extractBlock(stripped, start).length]);
255
+ }
256
+ return ranges;
257
+ }
197
258
  function forEachSelectorBlock(stripped, selectors, onBlock) {
198
259
  for (const sel of selectors) {
199
260
  const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -210,11 +271,27 @@ function forEachSelectorBlock(stripped, selectors, onBlock) {
210
271
  }
211
272
  var BASE_SELECTORS = [":root", "@theme"];
212
273
  function scanDeclarations(css, selectors, onProperty, skip = [], stripped = stripComments(css)) {
274
+ const conditional = conditionalRanges(stripped);
213
275
  forEachSelectorBlock(stripped, [...BASE_SELECTORS, ...selectors ?? []], (block, [bodyStart]) => {
214
- if (skip.some(([s, e]) => bodyStart >= s && bodyStart < e)) return;
215
- forEachProperty(block, onProperty, bodyStart);
276
+ const nested = nestedRanges(block);
277
+ forEachProperty(block, (name, value, position) => {
278
+ if (inRanges(skip, position)) return;
279
+ onProperty(name, value, position, inRanges(conditional, position) || inRanges(nested, position - bodyStart));
280
+ }, bodyStart);
216
281
  });
217
282
  }
283
+ function makeDeclarationCollector() {
284
+ const declarations = /* @__PURE__ */ new Map();
285
+ const conditional = /* @__PURE__ */ new Set();
286
+ const add = (name, value, isConditional) => {
287
+ if (isConditional && declarations.has(name) && !conditional.has(name)) return false;
288
+ declarations.set(name, value);
289
+ if (isConditional) conditional.add(name);
290
+ else conditional.delete(name);
291
+ return true;
292
+ };
293
+ return { declarations, conditional, add };
294
+ }
218
295
  var normalizePrelude = (prelude) => prelude.replace(/\s+/g, "").toLowerCase();
219
296
  function forEachMediaBlock(stripped, media, onBlock) {
220
297
  const wanted = normalizePrelude(media.replace(/^@media/i, ""));
@@ -270,15 +347,17 @@ function parseThemedDeclarations(css, selectors, themes) {
270
347
  for (const [themeName, themeSelector] of Object.entries(themes)) {
271
348
  const values = /* @__PURE__ */ new Map();
272
349
  themeValues.set(themeName, values);
273
- const collect = (block, offset) => forEachProperty(block, (name, value, position) => {
350
+ const onProperty = (name, value, position) => {
274
351
  values.set(name, value);
275
352
  const doc = docAt(position);
276
353
  if (doc) themeDocs.set(name, doc);
277
- }, offset);
354
+ };
355
+ const collect = (block, offset) => forEachProperty(block, onProperty, offset);
278
356
  const selector = themeSelector.trim();
279
357
  if (/^@media\b/i.test(selector)) {
280
358
  forEachMediaBlock(stripped, selector, (mediaBlock, range) => {
281
359
  skip.push(range);
360
+ forEachOwnProperty(mediaBlock, onProperty, range[0]);
282
361
  forEachSelectorBlock(mediaBlock, BASE_SELECTORS, (block, [innerStart]) => collect(block, range[0] + innerStart));
283
362
  });
284
363
  } else {
@@ -288,17 +367,17 @@ function parseThemedDeclarations(css, selectors, themes) {
288
367
  });
289
368
  }
290
369
  }
291
- const declarations = /* @__PURE__ */ new Map();
370
+ const { declarations, conditional, add } = makeDeclarationCollector();
292
371
  const docs = /* @__PURE__ */ new Map();
293
- scanDeclarations(css, selectors, (name, value, position) => {
294
- declarations.set(name, value);
372
+ scanDeclarations(css, selectors, (name, value, position, isConditional) => {
373
+ if (!add(name, value, isConditional)) return;
295
374
  const doc = docAt(position);
296
375
  if (doc) docs.set(name, doc);
297
376
  }, skip, stripped);
298
377
  for (const [name, doc] of themeDocs) {
299
378
  if (!docs.has(name)) docs.set(name, doc);
300
379
  }
301
- return { declarations, themes: themeValues, docs };
380
+ return { declarations, conditional, themes: themeValues, docs };
302
381
  }
303
382
  var PROPERTY_DESCRIPTORS = [
304
383
  ["syntax", "syntax"],
@@ -376,9 +455,13 @@ async function findFiles(patterns, exclude) {
376
455
  return [...new Set(files.reverse())].reverse();
377
456
  }
378
457
  function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
379
- const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
380
- const include = compile(patterns);
381
- const ignore = exclude ? compile(exclude) : () => false;
458
+ const compile = (globs) => (0, import_picomatch.default)(globs.map((p) => p.replace(/^\.\//, "")));
459
+ const normalized = normalizePatterns(patterns);
460
+ const include = compile(normalized.filter((p) => !p.startsWith("!")));
461
+ const ignore = compile([
462
+ ...exclude ? normalizePatterns(exclude) : [],
463
+ ...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
464
+ ]);
382
465
  return (file) => {
383
466
  const absolute = (0, import_node_path2.resolve)(cwd, file).replace(/\\/g, "/");
384
467
  const fromCwd = (0, import_node_path2.relative)(cwd, absolute).replace(/\\/g, "/");
@@ -389,6 +472,7 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
389
472
  function globWatchRoots(patterns, cwd = process.cwd()) {
390
473
  const roots = /* @__PURE__ */ new Map();
391
474
  for (const pattern of normalizePatterns(patterns)) {
475
+ if (pattern.startsWith("!")) continue;
392
476
  const { base, glob: glob2, isGlob } = import_picomatch.default.scan(pattern);
393
477
  const dir = (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
394
478
  const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
@@ -423,12 +507,19 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
423
507
  })
424
508
  );
425
509
  const declarations = /* @__PURE__ */ new Map();
510
+ const conditional = /* @__PURE__ */ new Set();
426
511
  const properties = /* @__PURE__ */ new Map();
427
512
  const themeValues = new Map(Object.keys(themes).map((theme) => [theme, /* @__PURE__ */ new Map()]));
428
513
  const docs = /* @__PURE__ */ new Map();
429
514
  for (const result of perFile) {
430
515
  if (!result) continue;
431
- for (const [name, value] of result.declarations) declarations.set(name, value);
516
+ for (const [name, value] of result.declarations) {
517
+ const isConditional = result.conditional.has(name);
518
+ if (isConditional && declarations.has(name) && !conditional.has(name)) continue;
519
+ declarations.set(name, value);
520
+ if (isConditional) conditional.add(name);
521
+ else conditional.delete(name);
522
+ }
432
523
  for (const [name, rule] of result.properties) properties.set(name, rule);
433
524
  for (const [name, doc] of result.docs) docs.set(name, doc);
434
525
  for (const [theme, values] of result.themes) {
@@ -449,19 +540,46 @@ function rootPattern(text) {
449
540
  const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
450
541
  return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
451
542
  }
452
- var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
543
+ var ACCESS_RE = /\s*(?:\??\.\s*([A-Za-z_$][\w$]*)|(?:\?\.\s*)?\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
453
544
  function readAccess(text, from) {
454
545
  ACCESS_RE.lastIndex = from;
455
546
  const match = ACCESS_RE.exec(text);
456
547
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
457
548
  }
549
+ var PATTERN_KEY_RE = /^\s*(?:([A-Za-z_$][\w$]*)|(['"`])((?:(?!\2).)*)\2)\s*(?:[:=]|$)/;
550
+ function readPatternKeys(text, at) {
551
+ let i = at - 1;
552
+ while (i >= 0 && /\s/.test(text[i])) i--;
553
+ if (text[i] !== "=") return [];
554
+ i--;
555
+ while (i >= 0 && /\s/.test(text[i])) i--;
556
+ if (text[i] !== "}") return [];
557
+ const open = text.lastIndexOf("{", i);
558
+ if (open === -1) return [];
559
+ const body = text.slice(open + 1, i);
560
+ if (body.includes("}")) return [];
561
+ const keys = [];
562
+ for (const part of body.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, "").split(",")) {
563
+ const match = PATTERN_KEY_RE.exec(part);
564
+ if (match) keys.push(match[1] ?? match[3]);
565
+ }
566
+ return keys;
567
+ }
458
568
  function extractUsage(text, into) {
459
569
  for (const root of text.matchAll(rootPattern(text))) {
460
570
  const first = readAccess(text, root.index + root[0].length);
461
- if (!first) continue;
571
+ const second = first && readAccess(text, first.end);
572
+ const destructured = second ? [] : readPatternKeys(text, root.index);
573
+ if (!first) {
574
+ for (const key of destructured) {
575
+ into.keys.add(key);
576
+ into.wholeKeys.add(key);
577
+ }
578
+ continue;
579
+ }
462
580
  into.keys.add(first.key);
463
- const second = readAccess(text, first.end);
464
581
  if (second) into.paths.add(`${first.key}\0${second.key}`);
582
+ else if (destructured.length > 0) for (const leaf of destructured) into.paths.add(`${first.key}\0${leaf}`);
465
583
  else into.wholeKeys.add(first.key);
466
584
  }
467
585
  }
@@ -1,6 +1,6 @@
1
1
  import { Options } from './unplugin.cjs';
2
2
  import 'unplugin';
3
- import './generator-BokM5zux.cjs';
3
+ import './generator-TJNk6ET_.cjs';
4
4
 
5
5
  declare const _default: (options: Options) => WebpackPluginInstance;
6
6
 
package/dist/webpack.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Options } from './unplugin.js';
2
2
  import 'unplugin';
3
- import './generator-BokM5zux.js';
3
+ import './generator-TJNk6ET_.js';
4
4
 
5
5
  declare const _default: (options: Options) => WebpackPluginInstance;
6
6
 
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-JGJ53FND.js";
4
- import "./chunk-XSFYQPH6.js";
3
+ } from "./chunk-7J7UZYHB.js";
4
+ import "./chunk-BPZRXE6W.js";
5
5
 
6
6
  // src/webpack.ts
7
7
  var webpack_default = unplugin_default.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-typed-vars",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",