css-typed-vars 0.6.0 → 0.7.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,12 +1,15 @@
1
1
  import {
2
2
  __dirname,
3
- computeKey,
3
+ assertPruneCompatible,
4
+ collectVarNames,
4
5
  generateDeclaration,
5
6
  generateJs,
7
+ generateTokens,
8
+ partitionUnused,
6
9
  scanCss,
7
- scanUsedKeys,
10
+ scanUsage,
8
11
  warnOnCollisions
9
- } from "./chunk-LH53WYFF.js";
12
+ } from "./chunk-6YWE4A6S.js";
10
13
 
11
14
  // src/unplugin.ts
12
15
  import { createUnplugin } from "unplugin";
@@ -18,25 +21,37 @@ function getDtsPath(options) {
18
21
  if (options.dts === false) return null;
19
22
  return options.dts ? resolve(process.cwd(), options.dts) : join(__dirname, "generated.d.ts");
20
23
  }
24
+ function outputFiles(options, names, generatorOptions) {
25
+ const files = [];
26
+ const dtsPath = getDtsPath(options);
27
+ if (dtsPath) files.push([dtsPath, generateDeclaration(names, generatorOptions)]);
28
+ if (options.tokens) files.push([resolve(process.cwd(), options.tokens), generateTokens(names, generatorOptions)]);
29
+ return files;
30
+ }
21
31
  var unplugin_default = createUnplugin((options) => {
22
- if (options.prune) {
23
- if (options.group) throw new Error('css-typed-vars: "prune" cannot be combined with "group".');
24
- if (!options.usage) throw new Error('css-typed-vars: "prune" requires "usage" (glob(s) of source files to scan).');
25
- }
32
+ assertPruneCompatible(options);
26
33
  let cachedScan = null;
27
- const scan = () => scanCss(options.input, options.exclude, options.selectors);
34
+ const scan = () => scanCss(options.input, options.exclude, options.selectors, options.themes);
28
35
  const applyPrune = async (names) => {
29
36
  if (!options.prune) return names;
30
- const usedKeys = await scanUsedKeys(options.usage, options.usageExclude);
31
- const isUsed = (name) => usedKeys.has(computeKey(name, options.prefix, options.naming));
32
- const unused = names.filter((name) => !isUsed(name));
37
+ const usage = await scanUsage(options.usage, options.usageExclude);
38
+ const { used, unused } = partitionUnused(names, usage, options);
33
39
  if (unused.length > 0) {
34
40
  console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
35
41
  }
36
- return names.filter(isUsed);
42
+ return used;
37
43
  };
38
- const resolveNames = async (declarations) => {
39
- const names = [...declarations.keys()].sort();
44
+ const generatorOptions = ({ declarations, properties, themes }) => ({
45
+ prefix: options.prefix,
46
+ naming: options.naming,
47
+ declarations,
48
+ group: options.group,
49
+ properties,
50
+ helpers: options.helpers,
51
+ themes
52
+ });
53
+ const resolveNames = async (scanned) => {
54
+ const names = collectVarNames(scanned);
40
55
  warnOnCollisions(names, options.prefix, options.naming);
41
56
  return applyPrune(names);
42
57
  };
@@ -52,16 +67,17 @@ var unplugin_default = createUnplugin((options) => {
52
67
  const myGeneration = ++generation;
53
68
  const scanPromise = scan();
54
69
  cachedScan = scanPromise;
55
- const { declarations, properties } = await scanPromise;
70
+ const scanned = await scanPromise;
56
71
  if (myGeneration !== generation) return;
57
- const names = await resolveNames(declarations);
72
+ const names = await resolveNames(scanned);
58
73
  if (myGeneration !== generation) return;
59
- const dtsPath = getDtsPath(options);
60
- if (dtsPath) {
61
- const content = generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties);
74
+ const files = outputFiles(options, names, generatorOptions(scanned));
75
+ if (files.length > 0) {
62
76
  writeQueue = writeQueue.then(async () => {
63
- if (myGeneration !== generation) return;
64
- await writeFile(dtsPath, content, "utf8");
77
+ for (const [path, content] of files) {
78
+ if (myGeneration !== generation) return;
79
+ await writeFile(path, content, "utf8");
80
+ }
65
81
  }).catch(console.error);
66
82
  await writeQueue;
67
83
  }
@@ -87,20 +103,20 @@ var unplugin_default = createUnplugin((options) => {
87
103
  },
88
104
  async buildStart() {
89
105
  cachedScan = scan();
90
- const { declarations, properties } = await cachedScan;
91
- const names = await resolveNames(declarations);
92
- const dtsPath = getDtsPath(options);
93
- if (!dtsPath) return;
94
- await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties), "utf8");
106
+ const scanned = await cachedScan;
107
+ const names = await resolveNames(scanned);
108
+ for (const [path, content] of outputFiles(options, names, generatorOptions(scanned))) {
109
+ await writeFile(path, content, "utf8");
110
+ }
95
111
  },
96
112
  resolveId(id) {
97
113
  if (id === VIRTUAL_ID) return RESOLVED_ID;
98
114
  },
99
115
  async load(id) {
100
116
  if (id === RESOLVED_ID) {
101
- const { declarations, properties } = await (cachedScan ?? scan());
102
- const names = await applyPrune([...declarations.keys()].sort());
103
- return generateJs(names, options.prefix, options.naming, declarations, options.group, properties);
117
+ const scanned = await (cachedScan ?? scan());
118
+ const names = await applyPrune(collectVarNames(scanned));
119
+ return generateJs(names, generatorOptions(scanned));
104
120
  }
105
121
  }
106
122
  };
@@ -1,64 +1,66 @@
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
+ scanUsage,
9
11
  warnOnCollisions
10
- } from "./chunk-LH53WYFF.js";
12
+ } from "./chunk-6YWE4A6S.js";
11
13
 
12
14
  // src/index.ts
13
15
  import { writeFile, mkdir, readFile } from "fs/promises";
14
16
  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
17
  async function findUnusedVars(options) {
29
18
  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)
19
+ const [scanned, usage] = await Promise.all([
20
+ scanCss(options.input, options.exclude, options.selectors, options.themes),
21
+ scanUsage(options.usage, options.usageExclude)
33
22
  ]);
34
- const names = [...declarations.keys()].sort();
35
- return partitionUnused(names, usedKeys, options.prefix, options.naming).unused;
23
+ const names = collectVarNames(scanned);
24
+ return partitionUnused(names, usage, options).unused;
36
25
  }
37
26
  async function computeOutputs(options) {
38
27
  assertPruneCompatible(options);
39
- const { declarations, properties } = await scanCss(options.input, options.exclude, options.selectors);
40
- let names = [...declarations.keys()].sort();
28
+ const scanned = await scanCss(options.input, options.exclude, options.selectors, options.themes);
29
+ const { declarations, properties } = scanned;
30
+ let names = collectVarNames(scanned);
41
31
  if (names.length === 0) {
42
32
  console.warn("css-typed-vars: no CSS custom properties found.");
43
33
  }
44
34
  warnOnCollisions(names, options.prefix, options.naming);
45
35
  if (options.prune) {
46
- const usedKeys = await scanUsedKeys(options.usage, options.usageExclude);
47
- const { used, unused } = partitionUnused(names, usedKeys, options.prefix, options.naming);
36
+ const usage = await scanUsage(options.usage, options.usageExclude);
37
+ const { used, unused } = partitionUnused(names, usage, options);
48
38
  if (unused.length > 0) {
49
39
  console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
50
40
  }
51
41
  names = used;
52
42
  }
53
43
  const outPath = resolve(options.output);
44
+ const generatorOptions = {
45
+ prefix: options.prefix,
46
+ naming: options.naming,
47
+ declarations,
48
+ group: options.group,
49
+ properties,
50
+ helpers: options.helpers,
51
+ themes: scanned.themes
52
+ };
53
+ if (/\.json$/i.test(options.output)) {
54
+ return { outPath, content: generateTokens(names, generatorOptions) };
55
+ }
54
56
  const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
55
57
  if (jsExtMatch) {
56
- const content = generateJs(names, options.prefix, options.naming, declarations, options.group, properties);
58
+ const content = generateJs(names, generatorOptions);
57
59
  const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
58
- const dtsContent = generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties);
60
+ const dtsContent = generateDeclaration(names, generatorOptions);
59
61
  return { outPath, content, dtsPath, dtsContent };
60
62
  }
61
- return { outPath, content: generateCode(names, options.prefix, options.naming, declarations, options.group, properties) };
63
+ return { outPath, content: generateCode(names, generatorOptions) };
62
64
  }
63
65
  async function generate(options) {
64
66
  const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);