css-typed-vars 0.5.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 +152 -5
- package/dist/chunk-6YWE4A6S.js +691 -0
- package/dist/chunk-HBUSLYYB.js +127 -0
- package/dist/chunk-PLO2JWR7.js +88 -0
- package/dist/cli.cjs +480 -57
- package/dist/cli.js +51 -9
- package/dist/esbuild.cjs +433 -60
- 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 +452 -47
- package/dist/index.d.cts +27 -6
- package/dist/index.d.ts +27 -6
- package/dist/index.js +14 -2
- package/dist/rollup.cjs +433 -60
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +433 -60
- package/dist/unplugin.d.cts +17 -1
- package/dist/unplugin.d.ts +17 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +433 -60
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +433 -60
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +6 -2
- package/dist/chunk-H3T6RMQQ.js +0 -51
- package/dist/chunk-JDZAH2YQ.js +0 -320
- package/dist/chunk-YBZGHG2G.js +0 -91
- package/dist/generator-t6h8Ic7V.d.cts +0 -6
- package/dist/generator-t6h8Ic7V.d.ts +0 -6
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__dirname,
|
|
3
|
+
assertPruneCompatible,
|
|
4
|
+
collectVarNames,
|
|
5
|
+
generateDeclaration,
|
|
6
|
+
generateJs,
|
|
7
|
+
generateTokens,
|
|
8
|
+
partitionUnused,
|
|
9
|
+
scanCss,
|
|
10
|
+
scanUsage,
|
|
11
|
+
warnOnCollisions
|
|
12
|
+
} from "./chunk-6YWE4A6S.js";
|
|
13
|
+
|
|
14
|
+
// src/unplugin.ts
|
|
15
|
+
import { createUnplugin } from "unplugin";
|
|
16
|
+
import { writeFile } from "fs/promises";
|
|
17
|
+
import { join, resolve } from "path";
|
|
18
|
+
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
19
|
+
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
20
|
+
function getDtsPath(options) {
|
|
21
|
+
if (options.dts === false) return null;
|
|
22
|
+
return options.dts ? resolve(process.cwd(), options.dts) : join(__dirname, "generated.d.ts");
|
|
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
|
+
}
|
|
31
|
+
var unplugin_default = createUnplugin((options) => {
|
|
32
|
+
assertPruneCompatible(options);
|
|
33
|
+
let cachedScan = null;
|
|
34
|
+
const scan = () => scanCss(options.input, options.exclude, options.selectors, options.themes);
|
|
35
|
+
const applyPrune = async (names) => {
|
|
36
|
+
if (!options.prune) return names;
|
|
37
|
+
const usage = await scanUsage(options.usage, options.usageExclude);
|
|
38
|
+
const { used, unused } = partitionUnused(names, usage, options);
|
|
39
|
+
if (unused.length > 0) {
|
|
40
|
+
console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
|
|
41
|
+
}
|
|
42
|
+
return used;
|
|
43
|
+
};
|
|
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);
|
|
55
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
56
|
+
return applyPrune(names);
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
name: "css-typed-vars",
|
|
60
|
+
vite: {
|
|
61
|
+
enforce: "pre",
|
|
62
|
+
configureServer(server) {
|
|
63
|
+
let generation = 0;
|
|
64
|
+
let debounceTimer = null;
|
|
65
|
+
let writeQueue = Promise.resolve();
|
|
66
|
+
const rescan = async () => {
|
|
67
|
+
const myGeneration = ++generation;
|
|
68
|
+
const scanPromise = scan();
|
|
69
|
+
cachedScan = scanPromise;
|
|
70
|
+
const scanned = await scanPromise;
|
|
71
|
+
if (myGeneration !== generation) return;
|
|
72
|
+
const names = await resolveNames(scanned);
|
|
73
|
+
if (myGeneration !== generation) return;
|
|
74
|
+
const files = outputFiles(options, names, generatorOptions(scanned));
|
|
75
|
+
if (files.length > 0) {
|
|
76
|
+
writeQueue = writeQueue.then(async () => {
|
|
77
|
+
for (const [path, content] of files) {
|
|
78
|
+
if (myGeneration !== generation) return;
|
|
79
|
+
await writeFile(path, content, "utf8");
|
|
80
|
+
}
|
|
81
|
+
}).catch(console.error);
|
|
82
|
+
await writeQueue;
|
|
83
|
+
}
|
|
84
|
+
if (myGeneration !== generation) return;
|
|
85
|
+
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
86
|
+
if (mod) {
|
|
87
|
+
server.moduleGraph.invalidateModule(mod);
|
|
88
|
+
server.ws.send({ type: "full-reload" });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const handle = (file) => {
|
|
92
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
93
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
94
|
+
debounceTimer = setTimeout(() => {
|
|
95
|
+
debounceTimer = null;
|
|
96
|
+
rescan().catch(console.error);
|
|
97
|
+
}, 100);
|
|
98
|
+
};
|
|
99
|
+
server.watcher.on("change", handle);
|
|
100
|
+
server.watcher.on("add", handle);
|
|
101
|
+
server.watcher.on("unlink", handle);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
async buildStart() {
|
|
105
|
+
cachedScan = scan();
|
|
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
|
+
}
|
|
111
|
+
},
|
|
112
|
+
resolveId(id) {
|
|
113
|
+
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
114
|
+
},
|
|
115
|
+
async load(id) {
|
|
116
|
+
if (id === RESOLVED_ID) {
|
|
117
|
+
const scanned = await (cachedScan ?? scan());
|
|
118
|
+
const names = await applyPrune(collectVarNames(scanned));
|
|
119
|
+
return generateJs(names, generatorOptions(scanned));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
export {
|
|
126
|
+
unplugin_default
|
|
127
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertPruneCompatible,
|
|
3
|
+
collectVarNames,
|
|
4
|
+
generateCode,
|
|
5
|
+
generateDeclaration,
|
|
6
|
+
generateJs,
|
|
7
|
+
generateTokens,
|
|
8
|
+
partitionUnused,
|
|
9
|
+
scanCss,
|
|
10
|
+
scanUsage,
|
|
11
|
+
warnOnCollisions
|
|
12
|
+
} from "./chunk-6YWE4A6S.js";
|
|
13
|
+
|
|
14
|
+
// src/index.ts
|
|
15
|
+
import { writeFile, mkdir, readFile } from "fs/promises";
|
|
16
|
+
import { dirname, resolve } from "path";
|
|
17
|
+
async function findUnusedVars(options) {
|
|
18
|
+
if (!options.usage) throw new Error('css-typed-vars: findUnusedVars requires "usage" (glob(s) of source files to scan).');
|
|
19
|
+
const [scanned, usage] = await Promise.all([
|
|
20
|
+
scanCss(options.input, options.exclude, options.selectors, options.themes),
|
|
21
|
+
scanUsage(options.usage, options.usageExclude)
|
|
22
|
+
]);
|
|
23
|
+
const names = collectVarNames(scanned);
|
|
24
|
+
return partitionUnused(names, usage, options).unused;
|
|
25
|
+
}
|
|
26
|
+
async function computeOutputs(options) {
|
|
27
|
+
assertPruneCompatible(options);
|
|
28
|
+
const scanned = await scanCss(options.input, options.exclude, options.selectors, options.themes);
|
|
29
|
+
const { declarations, properties } = scanned;
|
|
30
|
+
let names = collectVarNames(scanned);
|
|
31
|
+
if (names.length === 0) {
|
|
32
|
+
console.warn("css-typed-vars: no CSS custom properties found.");
|
|
33
|
+
}
|
|
34
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
35
|
+
if (options.prune) {
|
|
36
|
+
const usage = await scanUsage(options.usage, options.usageExclude);
|
|
37
|
+
const { used, unused } = partitionUnused(names, usage, options);
|
|
38
|
+
if (unused.length > 0) {
|
|
39
|
+
console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
|
|
40
|
+
}
|
|
41
|
+
names = used;
|
|
42
|
+
}
|
|
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
|
+
}
|
|
56
|
+
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
|
57
|
+
if (jsExtMatch) {
|
|
58
|
+
const content = generateJs(names, generatorOptions);
|
|
59
|
+
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
60
|
+
const dtsContent = generateDeclaration(names, generatorOptions);
|
|
61
|
+
return { outPath, content, dtsPath, dtsContent };
|
|
62
|
+
}
|
|
63
|
+
return { outPath, content: generateCode(names, generatorOptions) };
|
|
64
|
+
}
|
|
65
|
+
async function generate(options) {
|
|
66
|
+
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
67
|
+
await mkdir(dirname(outPath), { recursive: true });
|
|
68
|
+
await writeFile(outPath, content, "utf8");
|
|
69
|
+
if (dtsPath && dtsContent !== void 0) {
|
|
70
|
+
await writeFile(dtsPath, dtsContent, "utf8");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function checkGenerated(options) {
|
|
74
|
+
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
75
|
+
const current = await readFile(outPath, "utf8").catch(() => null);
|
|
76
|
+
if (current !== content) return false;
|
|
77
|
+
if (dtsPath && dtsContent !== void 0) {
|
|
78
|
+
const currentDts = await readFile(dtsPath, "utf8").catch(() => null);
|
|
79
|
+
if (currentDts !== dtsContent) return false;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export {
|
|
85
|
+
findUnusedVars,
|
|
86
|
+
generate,
|
|
87
|
+
checkGenerated
|
|
88
|
+
};
|