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.
- package/README.md +91 -6
- package/dist/{chunk-LH53WYFF.js → chunk-6YWE4A6S.js} +259 -55
- package/dist/{chunk-TD7SYMDA.js → chunk-HBUSLYYB.js} +45 -29
- package/dist/{chunk-VZ2BHHXD.js → chunk-PLO2JWR7.js} +31 -29
- package/dist/cli.cjs +277 -83
- package/dist/cli.js +15 -9
- package/dist/esbuild.cjs +273 -79
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/generator-C1iVNsry.d.cts +49 -0
- package/dist/generator-C1iVNsry.d.ts +49 -0
- package/dist/index.cjs +274 -71
- package/dist/index.d.cts +16 -5
- package/dist/index.d.ts +16 -5
- package/dist/index.js +6 -2
- package/dist/rollup.cjs +273 -79
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +273 -79
- package/dist/unplugin.d.cts +12 -2
- package/dist/unplugin.d.ts +12 -2
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +273 -79
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +273 -79
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
- package/dist/generator-C2AppeJL.d.cts +0 -15
- package/dist/generator-C2AppeJL.d.ts +0 -15
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__dirname,
|
|
3
|
-
|
|
3
|
+
assertPruneCompatible,
|
|
4
|
+
collectVarNames,
|
|
4
5
|
generateDeclaration,
|
|
5
6
|
generateJs,
|
|
7
|
+
generateTokens,
|
|
8
|
+
partitionUnused,
|
|
6
9
|
scanCss,
|
|
7
|
-
|
|
10
|
+
scanUsage,
|
|
8
11
|
warnOnCollisions
|
|
9
|
-
} from "./chunk-
|
|
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
|
-
|
|
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
|
|
31
|
-
const
|
|
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
|
|
42
|
+
return used;
|
|
37
43
|
};
|
|
38
|
-
const
|
|
39
|
-
|
|
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
|
|
70
|
+
const scanned = await scanPromise;
|
|
56
71
|
if (myGeneration !== generation) return;
|
|
57
|
-
const names = await resolveNames(
|
|
72
|
+
const names = await resolveNames(scanned);
|
|
58
73
|
if (myGeneration !== generation) return;
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
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
|
-
|
|
64
|
-
|
|
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
|
|
91
|
-
const names = await resolveNames(
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
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
|
|
102
|
-
const names = await applyPrune(
|
|
103
|
-
return generateJs(names,
|
|
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
|
-
|
|
2
|
+
assertPruneCompatible,
|
|
3
|
+
collectVarNames,
|
|
3
4
|
generateCode,
|
|
4
5
|
generateDeclaration,
|
|
5
6
|
generateJs,
|
|
7
|
+
generateTokens,
|
|
8
|
+
partitionUnused,
|
|
6
9
|
scanCss,
|
|
7
|
-
|
|
8
|
-
scanVarDeclarations,
|
|
10
|
+
scanUsage,
|
|
9
11
|
warnOnCollisions
|
|
10
|
-
} from "./chunk-
|
|
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 [
|
|
31
|
-
|
|
32
|
-
|
|
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 =
|
|
35
|
-
return partitionUnused(names,
|
|
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
|
|
40
|
-
|
|
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
|
|
47
|
-
const { used, unused } = partitionUnused(names,
|
|
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,
|
|
58
|
+
const content = generateJs(names, generatorOptions);
|
|
57
59
|
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
58
|
-
const dtsContent = generateDeclaration(names,
|
|
60
|
+
const dtsContent = generateDeclaration(names, generatorOptions);
|
|
59
61
|
return { outPath, content, dtsPath, dtsContent };
|
|
60
62
|
}
|
|
61
|
-
return { outPath, content: generateCode(names,
|
|
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);
|