css-typed-vars 0.8.0 → 0.10.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
@@ -34,12 +34,26 @@ __export(unplugin_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(unplugin_exports);
36
36
  var import_unplugin = require("unplugin");
37
- var import_promises2 = require("fs/promises");
37
+
38
+ // src/write.ts
39
+ var import_promises = require("fs/promises");
38
40
  var import_node_path = require("path");
41
+ async function writeOutput(path, content) {
42
+ const current = await (0, import_promises.readFile)(path, "utf8").catch(() => null);
43
+ if (current === content) return false;
44
+ await (0, import_promises.mkdir)((0, import_node_path.dirname)(path), { recursive: true });
45
+ await (0, import_promises.writeFile)(path, content, "utf8");
46
+ return true;
47
+ }
48
+
49
+ // src/unplugin.ts
50
+ var import_node_path3 = require("path");
39
51
 
40
52
  // src/scanner.ts
41
- var import_promises = require("fs/promises");
53
+ var import_promises2 = require("fs/promises");
54
+ var import_node_path2 = require("path");
42
55
  var import_fast_glob = __toESM(require("fast-glob"), 1);
56
+ var import_picomatch = __toESM(require("picomatch"), 1);
43
57
 
44
58
  // src/parser.ts
45
59
  function stepQuote(text, i, quote) {
@@ -321,6 +335,24 @@ function toRawPosition(stripped, strippedPos) {
321
335
  function normalizePatterns(patterns) {
322
336
  return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
323
337
  }
338
+ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
339
+ const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
340
+ const include = compile(patterns);
341
+ const ignore = exclude ? compile(exclude) : () => false;
342
+ return (file) => {
343
+ const absolute = (0, import_node_path2.resolve)(cwd, file).replace(/\\/g, "/");
344
+ const fromCwd = (0, import_node_path2.relative)(cwd, absolute).replace(/\\/g, "/");
345
+ const matches = (test) => test(fromCwd) || test(absolute);
346
+ return matches(include) && !matches(ignore);
347
+ };
348
+ }
349
+ 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)];
355
+ }
324
356
  function collectVarNames(scanned) {
325
357
  const names = new Set(scanned.declarations.keys());
326
358
  for (const values of scanned.themes.values()) {
@@ -336,7 +368,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
336
368
  files.map(async (file) => {
337
369
  let css;
338
370
  try {
339
- css = await (0, import_promises.readFile)(file, "utf8");
371
+ css = await (0, import_promises2.readFile)(file, "utf8");
340
372
  } catch (err) {
341
373
  console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
342
374
  return null;
@@ -360,6 +392,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
360
392
  return { declarations, properties, themes: themeValues, docs };
361
393
  }
362
394
  var ROOT_RE = /\bcssVars\b/g;
395
+ var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
396
+ var ALIAS_RE = /(?:^|,)\s*(?:type\s+)?cssVars\s+as\s+([A-Za-z_$][\w$]*)\s*(?=,|$)/g;
397
+ function rootPattern(text) {
398
+ const aliases = /* @__PURE__ */ new Set();
399
+ for (const [, specifiers] of text.matchAll(IMPORT_RE)) {
400
+ for (const [, alias] of specifiers.matchAll(ALIAS_RE)) aliases.add(alias);
401
+ }
402
+ if (aliases.size === 0) return ROOT_RE;
403
+ const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
404
+ return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
405
+ }
363
406
  var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
364
407
  function readAccess(text, from) {
365
408
  ACCESS_RE.lastIndex = from;
@@ -367,7 +410,7 @@ function readAccess(text, from) {
367
410
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
368
411
  }
369
412
  function extractUsage(text, into) {
370
- for (const root of text.matchAll(ROOT_RE)) {
413
+ for (const root of text.matchAll(rootPattern(text))) {
371
414
  const first = readAccess(text, root.index + root[0].length);
372
415
  if (!first) continue;
373
416
  into.keys.add(first.key);
@@ -384,7 +427,7 @@ async function scanUsage(patterns, exclude) {
384
427
  const texts = await Promise.all(
385
428
  files.map(async (file) => {
386
429
  try {
387
- return await (0, import_promises.readFile)(file, "utf8");
430
+ return await (0, import_promises2.readFile)(file, "utf8");
388
431
  } catch (err) {
389
432
  console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
390
433
  return "";
@@ -688,19 +731,19 @@ function generateTokens(varNames, options = {}) {
688
731
  return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
689
732
  });
690
733
  const paths = new Map(tokens.map(({ meta, path }) => [meta.name, path]));
691
- const resolve2 = (value) => {
734
+ const resolve3 = (value) => {
692
735
  const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
693
736
  return target ? `{${target.join(".")}}` : value;
694
737
  };
695
738
  const root = {};
696
739
  for (const { meta, value, path } of tokens) {
697
- const token = { $value: resolve2(value) };
740
+ const token = { $value: resolve3(value) };
698
741
  const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
699
742
  if (type) token.$type = type;
700
743
  if (meta.doc?.description) token.$description = meta.doc.description;
701
744
  if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
702
745
  if (meta.themes) {
703
- const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
746
+ const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve3(themeValue)]));
704
747
  token.$extensions = { "css-typed-vars": { themes } };
705
748
  }
706
749
  const [key, leaf] = path;
@@ -735,17 +778,18 @@ function partitionUnused(names, usage, options) {
735
778
  }
736
779
 
737
780
  // src/unplugin.ts
781
+ var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
738
782
  var VIRTUAL_ID = "css-typed-vars/vars";
739
783
  var RESOLVED_ID = "\0css-typed-vars/vars";
740
784
  function getDtsPath(options) {
741
785
  if (options.dts === false) return null;
742
- return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
786
+ return options.dts ? (0, import_node_path3.resolve)(process.cwd(), options.dts) : (0, import_node_path3.join)(__dirname, "generated.d.ts");
743
787
  }
744
788
  function outputFiles(options, names, generatorOptions) {
745
789
  const files = [];
746
790
  const dtsPath = getDtsPath(options);
747
791
  if (dtsPath) files.push([dtsPath, generateDeclaration(names, generatorOptions)]);
748
- if (options.tokens) files.push([(0, import_node_path.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
792
+ if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
749
793
  return files;
750
794
  }
751
795
  var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
@@ -797,7 +841,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
797
841
  writeQueue = writeQueue.then(async () => {
798
842
  for (const [path, content] of files) {
799
843
  if (myGeneration !== generation) return;
800
- await (0, import_promises2.writeFile)(path, content, "utf8");
844
+ await writeOutput(path, content);
801
845
  }
802
846
  }).catch(console.error);
803
847
  await writeQueue;
@@ -809,8 +853,12 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
809
853
  server.ws.send({ type: "full-reload" });
810
854
  }
811
855
  };
856
+ const isInput = createFileMatcher(options.input, options.exclude);
857
+ const isUsage = options.prune ? createFileMatcher(options.usage, options.usageExclude) : () => false;
858
+ const outputs = new Set([getDtsPath(options), options.tokens && (0, import_node_path3.resolve)(process.cwd(), options.tokens)].filter((path) => !!path).map((path) => (0, import_node_path3.resolve)(path)));
812
859
  const handle = (file) => {
813
- if (!/\.(css|scss|less)$/i.test(file)) return;
860
+ if (outputs.has((0, import_node_path3.resolve)(file))) return;
861
+ if (!isInput(file) && !isUsage(file)) return;
814
862
  if (debounceTimer) clearTimeout(debounceTimer);
815
863
  debounceTimer = setTimeout(() => {
816
864
  debounceTimer = null;
@@ -820,6 +868,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
820
868
  server.watcher.on("change", handle);
821
869
  server.watcher.on("add", handle);
822
870
  server.watcher.on("unlink", handle);
871
+ server.watcher.add(globBaseDirs([...toArray(options.input), ...options.prune ? toArray(options.usage) : []]));
823
872
  }
824
873
  },
825
874
  async buildStart() {
@@ -827,7 +876,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
827
876
  const scanned = await cachedScan;
828
877
  const names = await resolveNames(scanned);
829
878
  for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
830
- await (0, import_promises2.writeFile)(path, content, "utf8");
879
+ await writeOutput(path, content);
831
880
  }
832
881
  },
833
882
  resolveId(id) {
package/dist/unplugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-32V3V6WT.js";
4
- import "./chunk-5HPTHW7S.js";
3
+ } from "./chunk-NZUQZYNM.js";
4
+ import "./chunk-XCAFJREQ.js";
5
5
  export {
6
6
  unplugin_default as default
7
7
  };
package/dist/vite.cjs CHANGED
@@ -36,12 +36,26 @@ module.exports = __toCommonJS(vite_exports);
36
36
 
37
37
  // src/unplugin.ts
38
38
  var import_unplugin = require("unplugin");
39
- var import_promises2 = require("fs/promises");
39
+
40
+ // src/write.ts
41
+ var import_promises = require("fs/promises");
40
42
  var import_node_path = require("path");
43
+ async function writeOutput(path, content) {
44
+ const current = await (0, import_promises.readFile)(path, "utf8").catch(() => null);
45
+ if (current === content) return false;
46
+ await (0, import_promises.mkdir)((0, import_node_path.dirname)(path), { recursive: true });
47
+ await (0, import_promises.writeFile)(path, content, "utf8");
48
+ return true;
49
+ }
50
+
51
+ // src/unplugin.ts
52
+ var import_node_path3 = require("path");
41
53
 
42
54
  // src/scanner.ts
43
- var import_promises = require("fs/promises");
55
+ var import_promises2 = require("fs/promises");
56
+ var import_node_path2 = require("path");
44
57
  var import_fast_glob = __toESM(require("fast-glob"), 1);
58
+ var import_picomatch = __toESM(require("picomatch"), 1);
45
59
 
46
60
  // src/parser.ts
47
61
  function stepQuote(text, i, quote) {
@@ -323,6 +337,24 @@ function toRawPosition(stripped, strippedPos) {
323
337
  function normalizePatterns(patterns) {
324
338
  return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
325
339
  }
340
+ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
341
+ const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
342
+ const include = compile(patterns);
343
+ const ignore = exclude ? compile(exclude) : () => false;
344
+ return (file) => {
345
+ const absolute = (0, import_node_path2.resolve)(cwd, file).replace(/\\/g, "/");
346
+ const fromCwd = (0, import_node_path2.relative)(cwd, absolute).replace(/\\/g, "/");
347
+ const matches = (test) => test(fromCwd) || test(absolute);
348
+ return matches(include) && !matches(ignore);
349
+ };
350
+ }
351
+ 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)];
357
+ }
326
358
  function collectVarNames(scanned) {
327
359
  const names = new Set(scanned.declarations.keys());
328
360
  for (const values of scanned.themes.values()) {
@@ -338,7 +370,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
338
370
  files.map(async (file) => {
339
371
  let css;
340
372
  try {
341
- css = await (0, import_promises.readFile)(file, "utf8");
373
+ css = await (0, import_promises2.readFile)(file, "utf8");
342
374
  } catch (err) {
343
375
  console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
344
376
  return null;
@@ -362,6 +394,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
362
394
  return { declarations, properties, themes: themeValues, docs };
363
395
  }
364
396
  var ROOT_RE = /\bcssVars\b/g;
397
+ var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
398
+ var ALIAS_RE = /(?:^|,)\s*(?:type\s+)?cssVars\s+as\s+([A-Za-z_$][\w$]*)\s*(?=,|$)/g;
399
+ function rootPattern(text) {
400
+ const aliases = /* @__PURE__ */ new Set();
401
+ for (const [, specifiers] of text.matchAll(IMPORT_RE)) {
402
+ for (const [, alias] of specifiers.matchAll(ALIAS_RE)) aliases.add(alias);
403
+ }
404
+ if (aliases.size === 0) return ROOT_RE;
405
+ const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
406
+ return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
407
+ }
365
408
  var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
366
409
  function readAccess(text, from) {
367
410
  ACCESS_RE.lastIndex = from;
@@ -369,7 +412,7 @@ function readAccess(text, from) {
369
412
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
370
413
  }
371
414
  function extractUsage(text, into) {
372
- for (const root of text.matchAll(ROOT_RE)) {
415
+ for (const root of text.matchAll(rootPattern(text))) {
373
416
  const first = readAccess(text, root.index + root[0].length);
374
417
  if (!first) continue;
375
418
  into.keys.add(first.key);
@@ -386,7 +429,7 @@ async function scanUsage(patterns, exclude) {
386
429
  const texts = await Promise.all(
387
430
  files.map(async (file) => {
388
431
  try {
389
- return await (0, import_promises.readFile)(file, "utf8");
432
+ return await (0, import_promises2.readFile)(file, "utf8");
390
433
  } catch (err) {
391
434
  console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
392
435
  return "";
@@ -690,19 +733,19 @@ function generateTokens(varNames, options = {}) {
690
733
  return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
691
734
  });
692
735
  const paths = new Map(tokens.map(({ meta, path }) => [meta.name, path]));
693
- const resolve2 = (value) => {
736
+ const resolve3 = (value) => {
694
737
  const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
695
738
  return target ? `{${target.join(".")}}` : value;
696
739
  };
697
740
  const root = {};
698
741
  for (const { meta, value, path } of tokens) {
699
- const token = { $value: resolve2(value) };
742
+ const token = { $value: resolve3(value) };
700
743
  const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
701
744
  if (type) token.$type = type;
702
745
  if (meta.doc?.description) token.$description = meta.doc.description;
703
746
  if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
704
747
  if (meta.themes) {
705
- const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
748
+ const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve3(themeValue)]));
706
749
  token.$extensions = { "css-typed-vars": { themes } };
707
750
  }
708
751
  const [key, leaf] = path;
@@ -737,17 +780,18 @@ function partitionUnused(names, usage, options) {
737
780
  }
738
781
 
739
782
  // src/unplugin.ts
783
+ var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
740
784
  var VIRTUAL_ID = "css-typed-vars/vars";
741
785
  var RESOLVED_ID = "\0css-typed-vars/vars";
742
786
  function getDtsPath(options) {
743
787
  if (options.dts === false) return null;
744
- return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
788
+ return options.dts ? (0, import_node_path3.resolve)(process.cwd(), options.dts) : (0, import_node_path3.join)(__dirname, "generated.d.ts");
745
789
  }
746
790
  function outputFiles(options, names, generatorOptions) {
747
791
  const files = [];
748
792
  const dtsPath = getDtsPath(options);
749
793
  if (dtsPath) files.push([dtsPath, generateDeclaration(names, generatorOptions)]);
750
- if (options.tokens) files.push([(0, import_node_path.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
794
+ if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
751
795
  return files;
752
796
  }
753
797
  var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
@@ -799,7 +843,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
799
843
  writeQueue = writeQueue.then(async () => {
800
844
  for (const [path, content] of files) {
801
845
  if (myGeneration !== generation) return;
802
- await (0, import_promises2.writeFile)(path, content, "utf8");
846
+ await writeOutput(path, content);
803
847
  }
804
848
  }).catch(console.error);
805
849
  await writeQueue;
@@ -811,8 +855,12 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
811
855
  server.ws.send({ type: "full-reload" });
812
856
  }
813
857
  };
858
+ const isInput = createFileMatcher(options.input, options.exclude);
859
+ const isUsage = options.prune ? createFileMatcher(options.usage, options.usageExclude) : () => false;
860
+ const outputs = new Set([getDtsPath(options), options.tokens && (0, import_node_path3.resolve)(process.cwd(), options.tokens)].filter((path) => !!path).map((path) => (0, import_node_path3.resolve)(path)));
814
861
  const handle = (file) => {
815
- if (!/\.(css|scss|less)$/i.test(file)) return;
862
+ if (outputs.has((0, import_node_path3.resolve)(file))) return;
863
+ if (!isInput(file) && !isUsage(file)) return;
816
864
  if (debounceTimer) clearTimeout(debounceTimer);
817
865
  debounceTimer = setTimeout(() => {
818
866
  debounceTimer = null;
@@ -822,6 +870,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
822
870
  server.watcher.on("change", handle);
823
871
  server.watcher.on("add", handle);
824
872
  server.watcher.on("unlink", handle);
873
+ server.watcher.add(globBaseDirs([...toArray(options.input), ...options.prune ? toArray(options.usage) : []]));
825
874
  }
826
875
  },
827
876
  async buildStart() {
@@ -829,7 +878,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
829
878
  const scanned = await cachedScan;
830
879
  const names = await resolveNames(scanned);
831
880
  for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
832
- await (0, import_promises2.writeFile)(path, content, "utf8");
881
+ await writeOutput(path, content);
833
882
  }
834
883
  },
835
884
  resolveId(id) {
package/dist/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-32V3V6WT.js";
4
- import "./chunk-5HPTHW7S.js";
3
+ } from "./chunk-NZUQZYNM.js";
4
+ import "./chunk-XCAFJREQ.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;
package/dist/webpack.cjs CHANGED
@@ -36,12 +36,26 @@ module.exports = __toCommonJS(webpack_exports);
36
36
 
37
37
  // src/unplugin.ts
38
38
  var import_unplugin = require("unplugin");
39
- var import_promises2 = require("fs/promises");
39
+
40
+ // src/write.ts
41
+ var import_promises = require("fs/promises");
40
42
  var import_node_path = require("path");
43
+ async function writeOutput(path, content) {
44
+ const current = await (0, import_promises.readFile)(path, "utf8").catch(() => null);
45
+ if (current === content) return false;
46
+ await (0, import_promises.mkdir)((0, import_node_path.dirname)(path), { recursive: true });
47
+ await (0, import_promises.writeFile)(path, content, "utf8");
48
+ return true;
49
+ }
50
+
51
+ // src/unplugin.ts
52
+ var import_node_path3 = require("path");
41
53
 
42
54
  // src/scanner.ts
43
- var import_promises = require("fs/promises");
55
+ var import_promises2 = require("fs/promises");
56
+ var import_node_path2 = require("path");
44
57
  var import_fast_glob = __toESM(require("fast-glob"), 1);
58
+ var import_picomatch = __toESM(require("picomatch"), 1);
45
59
 
46
60
  // src/parser.ts
47
61
  function stepQuote(text, i, quote) {
@@ -323,6 +337,24 @@ function toRawPosition(stripped, strippedPos) {
323
337
  function normalizePatterns(patterns) {
324
338
  return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
325
339
  }
340
+ function createFileMatcher(patterns, exclude, cwd = process.cwd()) {
341
+ const compile = (globs) => (0, import_picomatch.default)(normalizePatterns(globs).map((p) => p.replace(/^\.\//, "")));
342
+ const include = compile(patterns);
343
+ const ignore = exclude ? compile(exclude) : () => false;
344
+ return (file) => {
345
+ const absolute = (0, import_node_path2.resolve)(cwd, file).replace(/\\/g, "/");
346
+ const fromCwd = (0, import_node_path2.relative)(cwd, absolute).replace(/\\/g, "/");
347
+ const matches = (test) => test(fromCwd) || test(absolute);
348
+ return matches(include) && !matches(ignore);
349
+ };
350
+ }
351
+ 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)];
357
+ }
326
358
  function collectVarNames(scanned) {
327
359
  const names = new Set(scanned.declarations.keys());
328
360
  for (const values of scanned.themes.values()) {
@@ -338,7 +370,7 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
338
370
  files.map(async (file) => {
339
371
  let css;
340
372
  try {
341
- css = await (0, import_promises.readFile)(file, "utf8");
373
+ css = await (0, import_promises2.readFile)(file, "utf8");
342
374
  } catch (err) {
343
375
  console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
344
376
  return null;
@@ -362,6 +394,17 @@ async function scanCss(patterns, exclude, selectors, themes = {}) {
362
394
  return { declarations, properties, themes: themeValues, docs };
363
395
  }
364
396
  var ROOT_RE = /\bcssVars\b/g;
397
+ var IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]*)\}\s*from\b/g;
398
+ var ALIAS_RE = /(?:^|,)\s*(?:type\s+)?cssVars\s+as\s+([A-Za-z_$][\w$]*)\s*(?=,|$)/g;
399
+ function rootPattern(text) {
400
+ const aliases = /* @__PURE__ */ new Set();
401
+ for (const [, specifiers] of text.matchAll(IMPORT_RE)) {
402
+ for (const [, alias] of specifiers.matchAll(ALIAS_RE)) aliases.add(alias);
403
+ }
404
+ if (aliases.size === 0) return ROOT_RE;
405
+ const names = [...aliases].map((alias) => alias.replace(/\$/g, "\\$")).join("|");
406
+ return new RegExp(`${ROOT_RE.source}|(?<![\\w$.])(?:${names})(?![\\w$])`, "g");
407
+ }
365
408
  var ACCESS_RE = /\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/y;
366
409
  function readAccess(text, from) {
367
410
  ACCESS_RE.lastIndex = from;
@@ -369,7 +412,7 @@ function readAccess(text, from) {
369
412
  return match ? { key: match[1] ?? match[3], end: ACCESS_RE.lastIndex } : null;
370
413
  }
371
414
  function extractUsage(text, into) {
372
- for (const root of text.matchAll(ROOT_RE)) {
415
+ for (const root of text.matchAll(rootPattern(text))) {
373
416
  const first = readAccess(text, root.index + root[0].length);
374
417
  if (!first) continue;
375
418
  into.keys.add(first.key);
@@ -386,7 +429,7 @@ async function scanUsage(patterns, exclude) {
386
429
  const texts = await Promise.all(
387
430
  files.map(async (file) => {
388
431
  try {
389
- return await (0, import_promises.readFile)(file, "utf8");
432
+ return await (0, import_promises2.readFile)(file, "utf8");
390
433
  } catch (err) {
391
434
  console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
392
435
  return "";
@@ -690,19 +733,19 @@ function generateTokens(varNames, options = {}) {
690
733
  return value === void 0 ? [] : [{ meta, value, path: computeKeyPath(name, options.prefix, options.naming, options.group) }];
691
734
  });
692
735
  const paths = new Map(tokens.map(({ meta, path }) => [meta.name, path]));
693
- const resolve2 = (value) => {
736
+ const resolve3 = (value) => {
694
737
  const target = paths.get(VAR_REF_RE.exec(value.trim())?.[1] ?? "");
695
738
  return target ? `{${target.join(".")}}` : value;
696
739
  };
697
740
  const root = {};
698
741
  for (const { meta, value, path } of tokens) {
699
- const token = { $value: resolve2(value) };
742
+ const token = { $value: resolve3(value) };
700
743
  const type = meta.syntax && TOKEN_TYPES[unquoteCssString(meta.syntax).trim()];
701
744
  if (type) token.$type = type;
702
745
  if (meta.doc?.description) token.$description = meta.doc.description;
703
746
  if (meta.doc?.deprecated) token.$deprecated = meta.doc.deprecated;
704
747
  if (meta.themes) {
705
- const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve2(themeValue)]));
748
+ const themes = Object.fromEntries(meta.themes.map(([theme, themeValue]) => [theme, resolve3(themeValue)]));
706
749
  token.$extensions = { "css-typed-vars": { themes } };
707
750
  }
708
751
  const [key, leaf] = path;
@@ -737,17 +780,18 @@ function partitionUnused(names, usage, options) {
737
780
  }
738
781
 
739
782
  // src/unplugin.ts
783
+ var toArray = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
740
784
  var VIRTUAL_ID = "css-typed-vars/vars";
741
785
  var RESOLVED_ID = "\0css-typed-vars/vars";
742
786
  function getDtsPath(options) {
743
787
  if (options.dts === false) return null;
744
- return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
788
+ return options.dts ? (0, import_node_path3.resolve)(process.cwd(), options.dts) : (0, import_node_path3.join)(__dirname, "generated.d.ts");
745
789
  }
746
790
  function outputFiles(options, names, generatorOptions) {
747
791
  const files = [];
748
792
  const dtsPath = getDtsPath(options);
749
793
  if (dtsPath) files.push([dtsPath, generateDeclaration(names, generatorOptions)]);
750
- if (options.tokens) files.push([(0, import_node_path.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
794
+ if (options.tokens) files.push([(0, import_node_path3.resolve)(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
751
795
  return files;
752
796
  }
753
797
  var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
@@ -799,7 +843,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
799
843
  writeQueue = writeQueue.then(async () => {
800
844
  for (const [path, content] of files) {
801
845
  if (myGeneration !== generation) return;
802
- await (0, import_promises2.writeFile)(path, content, "utf8");
846
+ await writeOutput(path, content);
803
847
  }
804
848
  }).catch(console.error);
805
849
  await writeQueue;
@@ -811,8 +855,12 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
811
855
  server.ws.send({ type: "full-reload" });
812
856
  }
813
857
  };
858
+ const isInput = createFileMatcher(options.input, options.exclude);
859
+ const isUsage = options.prune ? createFileMatcher(options.usage, options.usageExclude) : () => false;
860
+ const outputs = new Set([getDtsPath(options), options.tokens && (0, import_node_path3.resolve)(process.cwd(), options.tokens)].filter((path) => !!path).map((path) => (0, import_node_path3.resolve)(path)));
814
861
  const handle = (file) => {
815
- if (!/\.(css|scss|less)$/i.test(file)) return;
862
+ if (outputs.has((0, import_node_path3.resolve)(file))) return;
863
+ if (!isInput(file) && !isUsage(file)) return;
816
864
  if (debounceTimer) clearTimeout(debounceTimer);
817
865
  debounceTimer = setTimeout(() => {
818
866
  debounceTimer = null;
@@ -822,6 +870,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
822
870
  server.watcher.on("change", handle);
823
871
  server.watcher.on("add", handle);
824
872
  server.watcher.on("unlink", handle);
873
+ server.watcher.add(globBaseDirs([...toArray(options.input), ...options.prune ? toArray(options.usage) : []]));
825
874
  }
826
875
  },
827
876
  async buildStart() {
@@ -829,7 +878,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
829
878
  const scanned = await cachedScan;
830
879
  const names = await resolveNames(scanned);
831
880
  for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
832
- await (0, import_promises2.writeFile)(path, content, "utf8");
881
+ await writeOutput(path, content);
833
882
  }
834
883
  },
835
884
  resolveId(id) {
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-32V3V6WT.js";
4
- import "./chunk-5HPTHW7S.js";
3
+ } from "./chunk-NZUQZYNM.js";
4
+ import "./chunk-XCAFJREQ.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.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -42,6 +42,9 @@
42
42
  "css-typed-vars": "./dist/cli.js"
43
43
  },
44
44
  "license": "MIT",
45
+ "engines": {
46
+ "node": "^20.19.0 || >=22.12.0"
47
+ },
45
48
  "homepage": "https://github.com/StanislavKozachenko/css-typed-vars#readme",
46
49
  "repository": {
47
50
  "type": "git",
@@ -50,11 +53,20 @@
50
53
  "keywords": [
51
54
  "css",
52
55
  "scss",
56
+ "less",
53
57
  "css-variables",
54
58
  "custom-properties",
55
59
  "typescript",
56
60
  "codegen",
57
- "typed"
61
+ "typed",
62
+ "vite-plugin",
63
+ "unplugin",
64
+ "webpack-plugin",
65
+ "rollup-plugin",
66
+ "esbuild-plugin",
67
+ "design-tokens",
68
+ "dtcg",
69
+ "tailwindcss"
58
70
  ],
59
71
  "files": [
60
72
  "dist"
@@ -70,10 +82,12 @@
70
82
  "dependencies": {
71
83
  "chokidar": "^3.6.0",
72
84
  "fast-glob": "^3.3.2",
85
+ "picomatch": "^2.3.2",
73
86
  "unplugin": "^3.0.0"
74
87
  },
75
88
  "devDependencies": {
76
89
  "@types/node": "^25.5.2",
90
+ "@types/picomatch": "^2.3.4",
77
91
  "tsup": "^8.0.0",
78
92
  "typescript": "^5.4.0",
79
93
  "vitepress": "^1.6.4",