css-typed-vars 0.6.0 → 0.8.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.
@@ -1,64 +1,76 @@
1
1
  import {
2
- computeKey,
2
+ assertPruneCompatible,
3
+ collectVarNames,
3
4
  generateCode,
4
5
  generateDeclaration,
5
6
  generateJs,
7
+ generateTokens,
8
+ partitionUnused,
6
9
  scanCss,
7
- scanUsedKeys,
8
- scanVarDeclarations,
10
+ scanReferences,
11
+ scanUsage,
9
12
  warnOnCollisions
10
- } from "./chunk-LH53WYFF.js";
13
+ } from "./chunk-5HPTHW7S.js";
11
14
 
12
15
  // src/index.ts
13
16
  import { writeFile, mkdir, readFile } from "fs/promises";
14
17
  import { dirname, resolve } from "path";
15
- function assertPruneCompatible(options) {
16
- if (!options.prune) return;
17
- if (options.group) throw new Error('css-typed-vars: "prune" cannot be combined with "group".');
18
- if (!options.usage) throw new Error('css-typed-vars: "prune" requires "usage" (glob(s) of source files to scan).');
19
- }
20
- function partitionUnused(names, usedKeys, prefix, naming) {
21
- const used = [];
22
- const unused = [];
23
- for (const name of names) {
24
- (usedKeys.has(computeKey(name, prefix, naming)) ? used : unused).push(name);
25
- }
26
- return { used, unused };
27
- }
28
18
  async function findUnusedVars(options) {
29
19
  if (!options.usage) throw new Error('css-typed-vars: findUnusedVars requires "usage" (glob(s) of source files to scan).');
30
- const [declarations, usedKeys] = await Promise.all([
31
- scanVarDeclarations(options.input, options.exclude, options.selectors),
32
- scanUsedKeys(options.usage, options.usageExclude)
20
+ const [scanned, usage] = await Promise.all([
21
+ scanCss(options.input, options.exclude, options.selectors, options.themes),
22
+ scanUsage(options.usage, options.usageExclude)
33
23
  ]);
34
- const names = [...declarations.keys()].sort();
35
- return partitionUnused(names, usedKeys, options.prefix, options.naming).unused;
24
+ const names = collectVarNames(scanned);
25
+ return partitionUnused(names, usage, options).unused;
26
+ }
27
+ function matchesIgnore(name, patterns) {
28
+ return patterns.some((pattern) => pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
29
+ }
30
+ async function findUndefinedRefs(options) {
31
+ const { references, declared } = await scanReferences(options.input, options.exclude);
32
+ const ignore = options.refsIgnore ?? [];
33
+ return references.filter((ref) => !declared.has(ref.name) && !matchesIgnore(ref.name, ignore));
36
34
  }
37
35
  async function computeOutputs(options) {
38
36
  assertPruneCompatible(options);
39
- const { declarations, properties } = await scanCss(options.input, options.exclude, options.selectors);
40
- let names = [...declarations.keys()].sort();
37
+ const scanned = await scanCss(options.input, options.exclude, options.selectors, options.themes);
38
+ const { declarations, properties } = scanned;
39
+ let names = collectVarNames(scanned);
41
40
  if (names.length === 0) {
42
41
  console.warn("css-typed-vars: no CSS custom properties found.");
43
42
  }
44
43
  warnOnCollisions(names, options.prefix, options.naming);
45
44
  if (options.prune) {
46
- const usedKeys = await scanUsedKeys(options.usage, options.usageExclude);
47
- const { used, unused } = partitionUnused(names, usedKeys, options.prefix, options.naming);
45
+ const usage = await scanUsage(options.usage, options.usageExclude);
46
+ const { used, unused } = partitionUnused(names, usage, options);
48
47
  if (unused.length > 0) {
49
48
  console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
50
49
  }
51
50
  names = used;
52
51
  }
53
52
  const outPath = resolve(options.output);
53
+ const generatorOptions = {
54
+ prefix: options.prefix,
55
+ naming: options.naming,
56
+ declarations,
57
+ group: options.group,
58
+ properties,
59
+ helpers: options.helpers,
60
+ themes: scanned.themes,
61
+ docs: scanned.docs
62
+ };
63
+ if (/\.json$/i.test(options.output)) {
64
+ return { outPath, content: generateTokens(names, generatorOptions) };
65
+ }
54
66
  const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
55
67
  if (jsExtMatch) {
56
- const content = generateJs(names, options.prefix, options.naming, declarations, options.group, properties);
68
+ const content = generateJs(names, generatorOptions);
57
69
  const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
58
- const dtsContent = generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties);
70
+ const dtsContent = generateDeclaration(names, generatorOptions);
59
71
  return { outPath, content, dtsPath, dtsContent };
60
72
  }
61
- return { outPath, content: generateCode(names, options.prefix, options.naming, declarations, options.group, properties) };
73
+ return { outPath, content: generateCode(names, generatorOptions) };
62
74
  }
63
75
  async function generate(options) {
64
76
  const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
@@ -81,6 +93,7 @@ async function checkGenerated(options) {
81
93
 
82
94
  export {
83
95
  findUnusedVars,
96
+ findUndefinedRefs,
84
97
  generate,
85
98
  checkGenerated
86
99
  };