css-typed-vars 0.10.1 → 0.12.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/unplugin.cjs CHANGED
@@ -52,8 +52,8 @@ var import_node_path3 = require("path");
52
52
  // src/scanner.ts
53
53
  var import_promises2 = require("fs/promises");
54
54
  var import_node_path2 = require("path");
55
- var import_fast_glob = __toESM(require("fast-glob"), 1);
56
55
  var import_picomatch = __toESM(require("picomatch"), 1);
56
+ var import_tinyglobby = require("tinyglobby");
57
57
 
58
58
  // src/parser.ts
59
59
  function stepQuote(text, i, quote) {
@@ -335,6 +335,39 @@ function toRawPosition(stripped, strippedPos) {
335
335
  function normalizePatterns(patterns) {
336
336
  return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
337
337
  }
338
+ function comparePaths(a, b) {
339
+ const as = a.split("/");
340
+ const bs = b.split("/");
341
+ for (let i = 0; i < Math.min(as.length, bs.length); i++) {
342
+ if (as[i] === bs[i]) continue;
343
+ const aIsFile = i === as.length - 1;
344
+ const bIsFile = i === bs.length - 1;
345
+ if (aIsFile !== bIsFile) return aIsFile ? -1 : 1;
346
+ return as[i] < bs[i] ? -1 : 1;
347
+ }
348
+ return as.length - bs.length;
349
+ }
350
+ async function globFiles(pattern, ignore) {
351
+ if (!/^(?:[A-Za-z]:)?\//.test(pattern)) {
352
+ return (0, import_tinyglobby.glob)(pattern, { absolute: true, ignore, expandDirectories: false });
353
+ }
354
+ const { base, glob: rest, isGlob } = import_picomatch.default.scan(pattern);
355
+ const [cwd, relativeGlob] = isGlob ? [base, rest] : [import_node_path2.posix.dirname(pattern), import_node_path2.posix.basename(pattern)];
356
+ const matches = await (0, import_tinyglobby.glob)(relativeGlob, { cwd, absolute: true, expandDirectories: false });
357
+ if (ignore.length === 0) return matches;
358
+ const isIgnored = (0, import_picomatch.default)(ignore, { dot: true });
359
+ return matches.filter((file) => !isIgnored(file));
360
+ }
361
+ async function findFiles(patterns, exclude) {
362
+ const normalized = normalizePatterns(patterns);
363
+ const ignore = [
364
+ ...exclude ? normalizePatterns(exclude) : [],
365
+ ...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
366
+ ];
367
+ const perGlob = await Promise.all(normalized.filter((p) => !p.startsWith("!")).map((p) => globFiles(p, ignore)));
368
+ const files = perGlob.flatMap((matches) => matches.sort(comparePaths));
369
+ return [...new Set(files.reverse())].reverse();
370
+ }
338
371
  function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
339
372
  const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
340
373
  const include = compile(patterns);
@@ -346,12 +379,20 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
346
379
  return matches(include) && !matches(ignore);
347
380
  };
348
381
  }
382
+ function globWatchRoots(patterns, cwd = process.cwd()) {
383
+ const roots = /* @__PURE__ */ new Map();
384
+ for (const pattern of normalizePatterns(patterns)) {
385
+ const { base, glob: glob2, isGlob } = import_picomatch.default.scan(pattern);
386
+ const dir = (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
387
+ const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
388
+ const previous = roots.get(dir);
389
+ const unlimited = roots.has(dir) && previous === void 0 || depth === void 0;
390
+ roots.set(dir, unlimited ? void 0 : Math.max(previous ?? 0, depth));
391
+ }
392
+ return [...roots].map(([dir, depth]) => depth === void 0 ? { dir } : { dir, depth });
393
+ }
349
394
  function globBaseDirs(patterns, cwd = process.cwd()) {
350
- const dirs = normalizePatterns(patterns).map((pattern) => {
351
- const { base, isGlob } = import_picomatch.default.scan(pattern);
352
- return (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
353
- });
354
- return [...new Set(dirs)];
395
+ return globWatchRoots(patterns, cwd).map((root) => root.dir);
355
396
  }
356
397
  function collectVarNames(scanned) {
357
398
  const names = new Set(scanned.declarations.keys());
@@ -361,9 +402,7 @@ function collectVarNames(scanned) {
361
402
  return [...names].sort();
362
403
  }
363
404
  async function scanCss(patterns, exclude, selectors, themes = {}) {
364
- const normalized = normalizePatterns(patterns);
365
- const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
366
- const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
405
+ const files = await findFiles(patterns, exclude);
367
406
  const perFile = await Promise.all(
368
407
  files.map(async (file) => {
369
408
  let css;
@@ -389,7 +428,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
389
428
  for (const [name, value] of values) themeValues.get(theme).set(name, value);
390
429
  }
391
430
  }
392
- return { declarations, properties, themes: themeValues, docs };
431
+ return { declarations, properties, themes: themeValues, docs, files };
393
432
  }
394
433
  var ROOT_RE = /\bcssVars\b/g;
395
434
  var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
@@ -420,10 +459,8 @@ function extractUsage(text, into) {
420
459
  }
421
460
  }
422
461
  async function scanUsage(patterns, exclude) {
423
- const normalized = normalizePatterns(patterns);
424
- const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
425
- const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
426
- const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set() };
462
+ const files = await findFiles(patterns, exclude);
463
+ const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set(), files };
427
464
  const texts = await Promise.all(
428
465
  files.map(async (file) => {
429
466
  try {
@@ -792,18 +829,23 @@ function outputFiles(options, names, generatorOptions) {
792
829
  if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
793
830
  return files;
794
831
  }
795
- var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
832
+ var unplugin_default = (0, import_unplugin.createUnplugin)((options, meta) => {
796
833
  assertPruneCompatible(options);
797
834
  let cachedScan = null;
835
+ let devServer = false;
798
836
  const scan = () => scanCss(options.input, options.exclude, options.selectors, options.themes);
799
837
  const applyPrune = async (names) => {
800
- if (!options.prune) return names;
838
+ if (!options.prune) return { names, usageFiles: [] };
801
839
  const usage = await scanUsage(options.usage, options.usageExclude);
802
840
  const { used, unused } = partitionUnused(names, usage, options);
803
841
  if (unused.length > 0) {
804
842
  console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
805
843
  }
806
- return used;
844
+ return { names: used, usageFiles: usage.files };
845
+ };
846
+ const watchFiles = (context, scanned, usageFiles) => {
847
+ if (devServer) return;
848
+ for (const file of [...scanned.files, ...usageFiles]) context.addWatchFile((0, import_node_path3.resolve)(file));
807
849
  };
808
850
  const generatorOptions = ({ declarations, properties, themes, docs }) => ({
809
851
  prefix: options.prefix,
@@ -824,6 +866,9 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
824
866
  name: "css-typed-vars",
825
867
  vite: {
826
868
  enforce: "pre",
869
+ configResolved(config) {
870
+ devServer = config.command === "serve";
871
+ },
827
872
  configureServer(server) {
828
873
  let generation = 0;
829
874
  let debounceTimer = null;
@@ -834,7 +879,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
834
879
  cachedScan = scanPromise;
835
880
  const scanned = await scanPromise;
836
881
  if (myGeneration !== generation) return;
837
- const names = await resolveNames(scanned);
882
+ const { names } = await resolveNames(scanned);
838
883
  if (myGeneration !== generation) return;
839
884
  const files = outputFiles(options, names, generatorOptions(scanned));
840
885
  if (files.length > 0) {
@@ -874,7 +919,8 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
874
919
  async buildStart() {
875
920
  cachedScan = scan();
876
921
  const scanned = await cachedScan;
877
- const names = await resolveNames(scanned);
922
+ const { names, usageFiles } = await resolveNames(scanned);
923
+ if (meta.framework !== "esbuild") watchFiles(this, scanned, usageFiles);
878
924
  for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
879
925
  await writeOutput(path, content);
880
926
  }
@@ -885,7 +931,8 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
885
931
  async load(id) {
886
932
  if (id === RESOLVED_ID) {
887
933
  const scanned = await (cachedScan ?? scan());
888
- const names = await applyPrune(collectVarNames(scanned));
934
+ const { names, usageFiles } = await applyPrune(collectVarNames(scanned));
935
+ watchFiles(this, scanned, usageFiles);
889
936
  return generateJs(names, generatorOptions(scanned));
890
937
  }
891
938
  }
package/dist/unplugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-NZUQZYNM.js";
4
- import "./chunk-XCAFJREQ.js";
3
+ } from "./chunk-XWMRTHOE.js";
4
+ import "./chunk-RZLASYCF.js";
5
5
  export {
6
6
  unplugin_default as default
7
7
  };
package/dist/vite.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) {
@@ -337,6 +337,39 @@ function toRawPosition(stripped, strippedPos) {
337
337
  function normalizePatterns(patterns) {
338
338
  return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
339
339
  }
340
+ function comparePaths(a, b) {
341
+ const as = a.split("/");
342
+ const bs = b.split("/");
343
+ for (let i = 0; i < Math.min(as.length, bs.length); i++) {
344
+ if (as[i] === bs[i]) continue;
345
+ const aIsFile = i === as.length - 1;
346
+ const bIsFile = i === bs.length - 1;
347
+ if (aIsFile !== bIsFile) return aIsFile ? -1 : 1;
348
+ return as[i] < bs[i] ? -1 : 1;
349
+ }
350
+ return as.length - bs.length;
351
+ }
352
+ async function globFiles(pattern, ignore) {
353
+ if (!/^(?:[A-Za-z]:)?\//.test(pattern)) {
354
+ return (0, import_tinyglobby.glob)(pattern, { absolute: true, ignore, expandDirectories: false });
355
+ }
356
+ const { base, glob: rest, isGlob } = import_picomatch.default.scan(pattern);
357
+ const [cwd, relativeGlob] = isGlob ? [base, rest] : [import_node_path2.posix.dirname(pattern), import_node_path2.posix.basename(pattern)];
358
+ const matches = await (0, import_tinyglobby.glob)(relativeGlob, { cwd, absolute: true, expandDirectories: false });
359
+ if (ignore.length === 0) return matches;
360
+ const isIgnored = (0, import_picomatch.default)(ignore, { dot: true });
361
+ return matches.filter((file) => !isIgnored(file));
362
+ }
363
+ async function findFiles(patterns, exclude) {
364
+ const normalized = normalizePatterns(patterns);
365
+ const ignore = [
366
+ ...exclude ? normalizePatterns(exclude) : [],
367
+ ...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
368
+ ];
369
+ const perGlob = await Promise.all(normalized.filter((p) => !p.startsWith("!")).map((p) => globFiles(p, ignore)));
370
+ const files = perGlob.flatMap((matches) => matches.sort(comparePaths));
371
+ return [...new Set(files.reverse())].reverse();
372
+ }
340
373
  function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
341
374
  const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
342
375
  const include = compile(patterns);
@@ -348,12 +381,20 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
348
381
  return matches(include) && !matches(ignore);
349
382
  };
350
383
  }
384
+ function globWatchRoots(patterns, cwd = process.cwd()) {
385
+ const roots = /* @__PURE__ */ new Map();
386
+ for (const pattern of normalizePatterns(patterns)) {
387
+ const { base, glob: glob2, isGlob } = import_picomatch.default.scan(pattern);
388
+ const dir = (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
389
+ const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
390
+ const previous = roots.get(dir);
391
+ const unlimited = roots.has(dir) && previous === void 0 || depth === void 0;
392
+ roots.set(dir, unlimited ? void 0 : Math.max(previous ?? 0, depth));
393
+ }
394
+ return [...roots].map(([dir, depth]) => depth === void 0 ? { dir } : { dir, depth });
395
+ }
351
396
  function globBaseDirs(patterns, cwd = process.cwd()) {
352
- const dirs = normalizePatterns(patterns).map((pattern) => {
353
- const { base, isGlob } = import_picomatch.default.scan(pattern);
354
- return (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
355
- });
356
- return [...new Set(dirs)];
397
+ return globWatchRoots(patterns, cwd).map((root) => root.dir);
357
398
  }
358
399
  function collectVarNames(scanned) {
359
400
  const names = new Set(scanned.declarations.keys());
@@ -363,9 +404,7 @@ function collectVarNames(scanned) {
363
404
  return [...names].sort();
364
405
  }
365
406
  async function scanCss(patterns, exclude, selectors, themes = {}) {
366
- const normalized = normalizePatterns(patterns);
367
- const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
368
- const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
407
+ const files = await findFiles(patterns, exclude);
369
408
  const perFile = await Promise.all(
370
409
  files.map(async (file) => {
371
410
  let css;
@@ -391,7 +430,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
391
430
  for (const [name, value] of values) themeValues.get(theme).set(name, value);
392
431
  }
393
432
  }
394
- return { declarations, properties, themes: themeValues, docs };
433
+ return { declarations, properties, themes: themeValues, docs, files };
395
434
  }
396
435
  var ROOT_RE = /\bcssVars\b/g;
397
436
  var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
@@ -422,10 +461,8 @@ function extractUsage(text, into) {
422
461
  }
423
462
  }
424
463
  async function scanUsage(patterns, exclude) {
425
- const normalized = normalizePatterns(patterns);
426
- const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
427
- const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
428
- const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set() };
464
+ const files = await findFiles(patterns, exclude);
465
+ const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set(), files };
429
466
  const texts = await Promise.all(
430
467
  files.map(async (file) => {
431
468
  try {
@@ -794,18 +831,23 @@ function outputFiles(options, names, generatorOptions) {
794
831
  if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
795
832
  return files;
796
833
  }
797
- var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
834
+ var unplugin_default = (0, import_unplugin.createUnplugin)((options, meta) => {
798
835
  assertPruneCompatible(options);
799
836
  let cachedScan = null;
837
+ let devServer = false;
800
838
  const scan = () => scanCss(options.input, options.exclude, options.selectors, options.themes);
801
839
  const applyPrune = async (names) => {
802
- if (!options.prune) return names;
840
+ if (!options.prune) return { names, usageFiles: [] };
803
841
  const usage = await scanUsage(options.usage, options.usageExclude);
804
842
  const { used, unused } = partitionUnused(names, usage, options);
805
843
  if (unused.length > 0) {
806
844
  console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
807
845
  }
808
- return used;
846
+ return { names: used, usageFiles: usage.files };
847
+ };
848
+ const watchFiles = (context, scanned, usageFiles) => {
849
+ if (devServer) return;
850
+ for (const file of [...scanned.files, ...usageFiles]) context.addWatchFile((0, import_node_path3.resolve)(file));
809
851
  };
810
852
  const generatorOptions = ({ declarations, properties, themes, docs }) => ({
811
853
  prefix: options.prefix,
@@ -826,6 +868,9 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
826
868
  name: "css-typed-vars",
827
869
  vite: {
828
870
  enforce: "pre",
871
+ configResolved(config) {
872
+ devServer = config.command === "serve";
873
+ },
829
874
  configureServer(server) {
830
875
  let generation = 0;
831
876
  let debounceTimer = null;
@@ -836,7 +881,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
836
881
  cachedScan = scanPromise;
837
882
  const scanned = await scanPromise;
838
883
  if (myGeneration !== generation) return;
839
- const names = await resolveNames(scanned);
884
+ const { names } = await resolveNames(scanned);
840
885
  if (myGeneration !== generation) return;
841
886
  const files = outputFiles(options, names, generatorOptions(scanned));
842
887
  if (files.length > 0) {
@@ -876,7 +921,8 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
876
921
  async buildStart() {
877
922
  cachedScan = scan();
878
923
  const scanned = await cachedScan;
879
- const names = await resolveNames(scanned);
924
+ const { names, usageFiles } = await resolveNames(scanned);
925
+ if (meta.framework !== "esbuild") watchFiles(this, scanned, usageFiles);
880
926
  for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
881
927
  await writeOutput(path, content);
882
928
  }
@@ -887,7 +933,8 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
887
933
  async load(id) {
888
934
  if (id === RESOLVED_ID) {
889
935
  const scanned = await (cachedScan ?? scan());
890
- const names = await applyPrune(collectVarNames(scanned));
936
+ const { names, usageFiles } = await applyPrune(collectVarNames(scanned));
937
+ watchFiles(this, scanned, usageFiles);
891
938
  return generateJs(names, generatorOptions(scanned));
892
939
  }
893
940
  }
package/dist/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-NZUQZYNM.js";
4
- import "./chunk-XCAFJREQ.js";
3
+ } from "./chunk-XWMRTHOE.js";
4
+ import "./chunk-RZLASYCF.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;
package/dist/webpack.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) {
@@ -337,6 +337,39 @@ function toRawPosition(stripped, strippedPos) {
337
337
  function normalizePatterns(patterns) {
338
338
  return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
339
339
  }
340
+ function comparePaths(a, b) {
341
+ const as = a.split("/");
342
+ const bs = b.split("/");
343
+ for (let i = 0; i < Math.min(as.length, bs.length); i++) {
344
+ if (as[i] === bs[i]) continue;
345
+ const aIsFile = i === as.length - 1;
346
+ const bIsFile = i === bs.length - 1;
347
+ if (aIsFile !== bIsFile) return aIsFile ? -1 : 1;
348
+ return as[i] < bs[i] ? -1 : 1;
349
+ }
350
+ return as.length - bs.length;
351
+ }
352
+ async function globFiles(pattern, ignore) {
353
+ if (!/^(?:[A-Za-z]:)?\//.test(pattern)) {
354
+ return (0, import_tinyglobby.glob)(pattern, { absolute: true, ignore, expandDirectories: false });
355
+ }
356
+ const { base, glob: rest, isGlob } = import_picomatch.default.scan(pattern);
357
+ const [cwd, relativeGlob] = isGlob ? [base, rest] : [import_node_path2.posix.dirname(pattern), import_node_path2.posix.basename(pattern)];
358
+ const matches = await (0, import_tinyglobby.glob)(relativeGlob, { cwd, absolute: true, expandDirectories: false });
359
+ if (ignore.length === 0) return matches;
360
+ const isIgnored = (0, import_picomatch.default)(ignore, { dot: true });
361
+ return matches.filter((file) => !isIgnored(file));
362
+ }
363
+ async function findFiles(patterns, exclude) {
364
+ const normalized = normalizePatterns(patterns);
365
+ const ignore = [
366
+ ...exclude ? normalizePatterns(exclude) : [],
367
+ ...normalized.filter((p) => p.startsWith("!")).map((p) => p.slice(1))
368
+ ];
369
+ const perGlob = await Promise.all(normalized.filter((p) => !p.startsWith("!")).map((p) => globFiles(p, ignore)));
370
+ const files = perGlob.flatMap((matches) => matches.sort(comparePaths));
371
+ return [...new Set(files.reverse())].reverse();
372
+ }
340
373
  function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
341
374
  const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
342
375
  const include = compile(patterns);
@@ -348,12 +381,20 @@ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
348
381
  return matches(include) && !matches(ignore);
349
382
  };
350
383
  }
384
+ function globWatchRoots(patterns, cwd = process.cwd()) {
385
+ const roots = /* @__PURE__ */ new Map();
386
+ for (const pattern of normalizePatterns(patterns)) {
387
+ const { base, glob: glob2, isGlob } = import_picomatch.default.scan(pattern);
388
+ const dir = (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
389
+ const depth = !isGlob ? 0 : /\*\*|\{[^}]*\//.test(glob2) ? void 0 : glob2.split("/").length - 1;
390
+ const previous = roots.get(dir);
391
+ const unlimited = roots.has(dir) && previous === void 0 || depth === void 0;
392
+ roots.set(dir, unlimited ? void 0 : Math.max(previous ?? 0, depth));
393
+ }
394
+ return [...roots].map(([dir, depth]) => depth === void 0 ? { dir } : { dir, depth });
395
+ }
351
396
  function globBaseDirs(patterns, cwd = process.cwd()) {
352
- const dirs = normalizePatterns(patterns).map((pattern) => {
353
- const { base, isGlob } = import_picomatch.default.scan(pattern);
354
- return (0, import_node_path2.resolve)(cwd, isGlob ? base || "." : (0, import_node_path2.dirname)(pattern));
355
- });
356
- return [...new Set(dirs)];
397
+ return globWatchRoots(patterns, cwd).map((root) => root.dir);
357
398
  }
358
399
  function collectVarNames(scanned) {
359
400
  const names = new Set(scanned.declarations.keys());
@@ -363,9 +404,7 @@ function collectVarNames(scanned) {
363
404
  return [...names].sort();
364
405
  }
365
406
  async function scanCss(patterns, exclude, selectors, themes = {}) {
366
- const normalized = normalizePatterns(patterns);
367
- const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
368
- const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
407
+ const files = await findFiles(patterns, exclude);
369
408
  const perFile = await Promise.all(
370
409
  files.map(async (file) => {
371
410
  let css;
@@ -391,7 +430,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
391
430
  for (const [name, value] of values) themeValues.get(theme).set(name, value);
392
431
  }
393
432
  }
394
- return { declarations, properties, themes: themeValues, docs };
433
+ return { declarations, properties, themes: themeValues, docs, files };
395
434
  }
396
435
  var ROOT_RE = /\bcssVars\b/g;
397
436
  var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
@@ -422,10 +461,8 @@ function extractUsage(text, into) {
422
461
  }
423
462
  }
424
463
  async function scanUsage(patterns, exclude) {
425
- const normalized = normalizePatterns(patterns);
426
- const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
427
- const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
428
- const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set() };
464
+ const files = await findFiles(patterns, exclude);
465
+ const usage = { keys: /* @__PURE__ */ new Set(), wholeKeys: /* @__PURE__ */ new Set(), paths: /* @__PURE__ */ new Set(), files };
429
466
  const texts = await Promise.all(
430
467
  files.map(async (file) => {
431
468
  try {
@@ -794,18 +831,23 @@ function outputFiles(options, names, generatorOptions) {
794
831
  if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
795
832
  return files;
796
833
  }
797
- var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
834
+ var unplugin_default = (0, import_unplugin.createUnplugin)((options, meta) => {
798
835
  assertPruneCompatible(options);
799
836
  let cachedScan = null;
837
+ let devServer = false;
800
838
  const scan = () => scanCss(options.input, options.exclude, options.selectors, options.themes);
801
839
  const applyPrune = async (names) => {
802
- if (!options.prune) return names;
840
+ if (!options.prune) return { names, usageFiles: [] };
803
841
  const usage = await scanUsage(options.usage, options.usageExclude);
804
842
  const { used, unused } = partitionUnused(names, usage, options);
805
843
  if (unused.length > 0) {
806
844
  console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
807
845
  }
808
- return used;
846
+ return { names: used, usageFiles: usage.files };
847
+ };
848
+ const watchFiles = (context, scanned, usageFiles) => {
849
+ if (devServer) return;
850
+ for (const file of [...scanned.files, ...usageFiles]) context.addWatchFile((0, import_node_path3.resolve)(file));
809
851
  };
810
852
  const generatorOptions = ({ declarations, properties, themes, docs }) => ({
811
853
  prefix: options.prefix,
@@ -826,6 +868,9 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
826
868
  name: "css-typed-vars",
827
869
  vite: {
828
870
  enforce: "pre",
871
+ configResolved(config) {
872
+ devServer = config.command === "serve";
873
+ },
829
874
  configureServer(server) {
830
875
  let generation = 0;
831
876
  let debounceTimer = null;
@@ -836,7 +881,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
836
881
  cachedScan = scanPromise;
837
882
  const scanned = await scanPromise;
838
883
  if (myGeneration !== generation) return;
839
- const names = await resolveNames(scanned);
884
+ const { names } = await resolveNames(scanned);
840
885
  if (myGeneration !== generation) return;
841
886
  const files = outputFiles(options, names, generatorOptions(scanned));
842
887
  if (files.length > 0) {
@@ -876,7 +921,8 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
876
921
  async buildStart() {
877
922
  cachedScan = scan();
878
923
  const scanned = await cachedScan;
879
- const names = await resolveNames(scanned);
924
+ const { names, usageFiles } = await resolveNames(scanned);
925
+ if (meta.framework !== "esbuild") watchFiles(this, scanned, usageFiles);
880
926
  for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
881
927
  await writeOutput(path, content);
882
928
  }
@@ -887,7 +933,8 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
887
933
  async load(id) {
888
934
  if (id === RESOLVED_ID) {
889
935
  const scanned = await (cachedScan ?? scan());
890
- const names = await applyPrune(collectVarNames(scanned));
936
+ const { names, usageFiles } = await applyPrune(collectVarNames(scanned));
937
+ watchFiles(this, scanned, usageFiles);
891
938
  return generateJs(names, generatorOptions(scanned));
892
939
  }
893
940
  }
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-NZUQZYNM.js";
4
- import "./chunk-XCAFJREQ.js";
3
+ } from "./chunk-XWMRTHOE.js";
4
+ import "./chunk-RZLASYCF.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.10.1",
3
+ "version": "0.12.0",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -36,7 +36,8 @@
36
36
  "types": "./dist/generated.d.ts",
37
37
  "import": "./dist/vars.js",
38
38
  "require": "./dist/vars.cjs"
39
- }
39
+ },
40
+ "./schema.json": "./schema.json"
40
41
  },
41
42
  "bin": {
42
43
  "css-typed-vars": "./dist/cli.js"
@@ -69,7 +70,8 @@
69
70
  "tailwindcss"
70
71
  ],
71
72
  "files": [
72
- "dist"
73
+ "dist",
74
+ "schema.json"
73
75
  ],
74
76
  "scripts": {
75
77
  "build": "tsup && node scripts/postbuild.mjs",
@@ -80,14 +82,14 @@
80
82
  "docs:preview": "vitepress preview docs"
81
83
  },
82
84
  "dependencies": {
83
- "chokidar": "^3.6.0",
84
- "fast-glob": "^3.3.2",
85
- "picomatch": "^2.3.2",
85
+ "chokidar": "^5.0.0",
86
+ "picomatch": "^4.0.7",
87
+ "tinyglobby": "^0.2.17",
86
88
  "unplugin": "^3.0.0"
87
89
  },
88
90
  "devDependencies": {
89
91
  "@types/node": "^25.5.2",
90
- "@types/picomatch": "^2.3.4",
92
+ "@types/picomatch": "^4.0.3",
91
93
  "tsup": "^8.0.0",
92
94
  "typescript": "^5.4.0",
93
95
  "vitepress": "^1.6.4",