css-typed-vars 0.4.17 → 0.6.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 +74 -6
- package/dist/chunk-LH53WYFF.js +487 -0
- package/dist/{chunk-DOOYTSTY.js → chunk-TD7SYMDA.js} +38 -14
- package/dist/chunk-VZ2BHHXD.js +86 -0
- package/dist/cli.cjs +352 -47
- package/dist/cli.js +59 -8
- package/dist/esbuild.cjs +266 -42
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/generator-C2AppeJL.d.cts +15 -0
- package/dist/generator-C2AppeJL.d.ts +15 -0
- package/dist/index.cjs +319 -40
- package/dist/index.d.cts +18 -5
- package/dist/index.d.ts +18 -5
- package/dist/index.js +16 -2
- package/dist/rollup.cjs +266 -42
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +266 -42
- package/dist/unplugin.d.cts +7 -1
- package/dist/unplugin.d.ts +7 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +266 -42
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +266 -42
- 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-EHRNYU34.js +0 -32
- package/dist/chunk-WYLYZXQJ.js +0 -265
- package/dist/generator-Ba8XeISD.d.cts +0 -6
- package/dist/generator-Ba8XeISD.d.ts +0 -6
package/dist/unplugin.cjs
CHANGED
|
@@ -138,10 +138,28 @@ function maskQuotedContent(text) {
|
|
|
138
138
|
}
|
|
139
139
|
return result;
|
|
140
140
|
}
|
|
141
|
-
function
|
|
141
|
+
function extractValue(text, start) {
|
|
142
|
+
let i = start;
|
|
143
|
+
let quote = null;
|
|
144
|
+
for (; i < text.length; i++) {
|
|
145
|
+
const char = text[i];
|
|
146
|
+
if (quote) {
|
|
147
|
+
const step = stepQuote(text, i, quote);
|
|
148
|
+
if (step.closed) quote = null;
|
|
149
|
+
i += step.consumed - 1;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (char === '"' || char === "'") {
|
|
153
|
+
quote = char;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (char === ";" || char === "}") break;
|
|
157
|
+
}
|
|
158
|
+
return text.slice(start, i).trim();
|
|
159
|
+
}
|
|
160
|
+
function scanDeclarations(css, selectors, onProperty) {
|
|
142
161
|
const stripped = stripComments(css);
|
|
143
|
-
const
|
|
144
|
-
const allSelectors = [":root", ...selectors ?? []];
|
|
162
|
+
const allSelectors = [":root", "@theme", ...selectors ?? []];
|
|
145
163
|
for (const sel of allSelectors) {
|
|
146
164
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
147
165
|
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
@@ -149,53 +167,135 @@ function parseVarNames(css, selectors) {
|
|
|
149
167
|
while (match = openRegex.exec(stripped)) {
|
|
150
168
|
const start = match.index + match[0].length;
|
|
151
169
|
const block = extractBlock(stripped, start);
|
|
152
|
-
|
|
153
|
-
|
|
170
|
+
const masked = maskQuotedContent(block);
|
|
171
|
+
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
172
|
+
const name = prop[0];
|
|
173
|
+
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
174
|
+
onProperty(name, extractValue(block, colonIndex + 1));
|
|
154
175
|
}
|
|
155
176
|
openRegex.lastIndex = start + block.length + 1;
|
|
156
177
|
}
|
|
157
178
|
}
|
|
158
|
-
|
|
179
|
+
}
|
|
180
|
+
function parseVarDeclarations(css, selectors) {
|
|
181
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
182
|
+
scanDeclarations(css, selectors, (name, value) => declarations.set(name, value));
|
|
183
|
+
return declarations;
|
|
184
|
+
}
|
|
185
|
+
var PROPERTY_DESCRIPTORS = [
|
|
186
|
+
["syntax", "syntax"],
|
|
187
|
+
["inherits", "inherits"],
|
|
188
|
+
["initial-value", "initialValue"]
|
|
189
|
+
];
|
|
190
|
+
function parsePropertyRules(css) {
|
|
191
|
+
const stripped = stripComments(css);
|
|
192
|
+
const rules = /* @__PURE__ */ new Map();
|
|
193
|
+
const openRegex = /@property\s+(--[\w-]+)\s*\{/g;
|
|
194
|
+
let match;
|
|
195
|
+
while (match = openRegex.exec(stripped)) {
|
|
196
|
+
const name = match[1];
|
|
197
|
+
const start = match.index + match[0].length;
|
|
198
|
+
const block = extractBlock(stripped, start);
|
|
199
|
+
const masked = maskQuotedContent(block);
|
|
200
|
+
const rule = {};
|
|
201
|
+
for (const [descriptor, key] of PROPERTY_DESCRIPTORS) {
|
|
202
|
+
const descRegex = new RegExp(`(?:^|[;{\\s])${descriptor}(?=\\s*:)`);
|
|
203
|
+
const descMatch = descRegex.exec(masked);
|
|
204
|
+
if (!descMatch) continue;
|
|
205
|
+
const nameEnd = descMatch.index + descMatch[0].length;
|
|
206
|
+
const colonIndex = block.indexOf(":", nameEnd);
|
|
207
|
+
rule[key] = extractValue(block, colonIndex + 1);
|
|
208
|
+
}
|
|
209
|
+
if (Object.keys(rule).length > 0) rules.set(name, rule);
|
|
210
|
+
openRegex.lastIndex = start + block.length + 1;
|
|
211
|
+
}
|
|
212
|
+
return rules;
|
|
159
213
|
}
|
|
160
214
|
|
|
161
215
|
// src/scanner.ts
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
216
|
+
function normalizePatterns(patterns) {
|
|
217
|
+
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
218
|
+
}
|
|
219
|
+
async function scanCss(patterns, exclude, selectors) {
|
|
220
|
+
const normalized = normalizePatterns(patterns);
|
|
221
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
167
222
|
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
168
|
-
const
|
|
169
|
-
await Promise.all(
|
|
223
|
+
const perFile = await Promise.all(
|
|
170
224
|
files.map(async (file) => {
|
|
171
225
|
let css;
|
|
172
226
|
try {
|
|
173
227
|
css = await (0, import_promises.readFile)(file, "utf8");
|
|
174
228
|
} catch (err) {
|
|
175
229
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
176
|
-
return;
|
|
230
|
+
return { declarations: /* @__PURE__ */ new Map(), properties: /* @__PURE__ */ new Map() };
|
|
177
231
|
}
|
|
178
|
-
|
|
179
|
-
|
|
232
|
+
return { declarations: parseVarDeclarations(css, selectors), properties: parsePropertyRules(css) };
|
|
233
|
+
})
|
|
234
|
+
);
|
|
235
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
236
|
+
const properties = /* @__PURE__ */ new Map();
|
|
237
|
+
for (const result of perFile) {
|
|
238
|
+
for (const [name, value] of result.declarations) declarations.set(name, value);
|
|
239
|
+
for (const [name, rule] of result.properties) properties.set(name, rule);
|
|
240
|
+
}
|
|
241
|
+
return { declarations, properties };
|
|
242
|
+
}
|
|
243
|
+
var USAGE_RE = /\bcssVars\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(['"`])((?:(?!\2).)*)\2\s*\])/g;
|
|
244
|
+
function extractUsedKeys(text, into) {
|
|
245
|
+
for (const match of text.matchAll(USAGE_RE)) {
|
|
246
|
+
into.add(match[1] ?? match[3]);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async function scanUsedKeys(patterns, exclude) {
|
|
250
|
+
const normalized = normalizePatterns(patterns);
|
|
251
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
252
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
253
|
+
const perFile = await Promise.all(
|
|
254
|
+
files.map(async (file) => {
|
|
255
|
+
const used = /* @__PURE__ */ new Set();
|
|
256
|
+
let text;
|
|
257
|
+
try {
|
|
258
|
+
text = await (0, import_promises.readFile)(file, "utf8");
|
|
259
|
+
} catch (err) {
|
|
260
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
261
|
+
return used;
|
|
180
262
|
}
|
|
263
|
+
extractUsedKeys(text, used);
|
|
264
|
+
return used;
|
|
181
265
|
})
|
|
182
266
|
);
|
|
183
|
-
|
|
267
|
+
const all = /* @__PURE__ */ new Set();
|
|
268
|
+
for (const used of perFile) {
|
|
269
|
+
for (const key of used) all.add(key);
|
|
270
|
+
}
|
|
271
|
+
return all;
|
|
184
272
|
}
|
|
185
273
|
|
|
186
274
|
// src/generator.ts
|
|
187
|
-
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
275
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab", "constant", "pascal"];
|
|
188
276
|
function convertCase(value, naming) {
|
|
189
277
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
190
278
|
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
191
279
|
}
|
|
192
280
|
if (naming === "kebab") return value;
|
|
193
|
-
|
|
281
|
+
if (naming === "snake") return value.replace(/-+/g, "_");
|
|
282
|
+
if (naming === "constant") return value.replace(/-+/g, "_").toUpperCase();
|
|
283
|
+
if (naming === "pascal") return value.replace(/(^|-+)([a-z0-9])/g, (_, __, c) => c.toUpperCase()).replace(/-/g, "");
|
|
284
|
+
return value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
194
285
|
}
|
|
195
|
-
function
|
|
196
|
-
const key = convertCase(
|
|
286
|
+
function toKeyFragment(fragment, naming = "camelCase") {
|
|
287
|
+
const key = convertCase(fragment, naming);
|
|
197
288
|
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
198
289
|
}
|
|
290
|
+
function toKey(cssVarName, naming = "camelCase") {
|
|
291
|
+
return toKeyFragment(cssVarName.replace(/^--/, ""), naming);
|
|
292
|
+
}
|
|
293
|
+
function splitGroupKey(cssVarName) {
|
|
294
|
+
const stripped = cssVarName.replace(/^--/, "");
|
|
295
|
+
const dashIndex = stripped.indexOf("-");
|
|
296
|
+
if (dashIndex === -1) return null;
|
|
297
|
+
return { group: stripped.slice(0, dashIndex), rest: stripped.slice(dashIndex + 1) };
|
|
298
|
+
}
|
|
199
299
|
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
200
300
|
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
201
301
|
var SINGLE_QUOTE_ESCAPES = {};
|
|
@@ -221,13 +321,16 @@ function applyPrefix(key, prefix, naming = "camelCase") {
|
|
|
221
321
|
}
|
|
222
322
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
223
323
|
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
224
|
-
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
324
|
+
if (naming === "snake" || naming === "constant") return `${normalizedPrefix}_${key}`;
|
|
225
325
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
226
326
|
}
|
|
227
327
|
function formatKey(key, naming = "camelCase") {
|
|
228
328
|
if (naming === "kebab") return `'${key}'`;
|
|
229
329
|
return key;
|
|
230
330
|
}
|
|
331
|
+
function computeKey(cssVarName, prefix, naming) {
|
|
332
|
+
return applyPrefix(toKey(cssVarName, naming), prefix, naming);
|
|
333
|
+
}
|
|
231
334
|
function findKeyCollisions(varNames, prefix, naming) {
|
|
232
335
|
const byKey = /* @__PURE__ */ new Map();
|
|
233
336
|
for (const name of varNames) {
|
|
@@ -246,16 +349,112 @@ function warnOnCollisions(varNames, prefix, naming) {
|
|
|
246
349
|
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
247
350
|
}
|
|
248
351
|
}
|
|
249
|
-
function buildEntries(varNames, prefix, naming) {
|
|
352
|
+
function buildEntries(varNames, prefix, naming, declarations, properties) {
|
|
250
353
|
const byKey = /* @__PURE__ */ new Map();
|
|
251
354
|
for (const name of varNames) {
|
|
252
355
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
253
|
-
byKey.set(key, name);
|
|
356
|
+
byKey.set(key, { name, value: declarations?.get(name), syntax: properties?.get(name)?.syntax });
|
|
357
|
+
}
|
|
358
|
+
return [...byKey].map(([key, entry]) => ({ key, ...entry }));
|
|
359
|
+
}
|
|
360
|
+
function buildGroupedEntries(varNames, naming, declarations, properties) {
|
|
361
|
+
const groupMap = /* @__PURE__ */ new Map();
|
|
362
|
+
const ungroupedMap = /* @__PURE__ */ new Map();
|
|
363
|
+
for (const name of varNames) {
|
|
364
|
+
const value = declarations?.get(name);
|
|
365
|
+
const syntax = properties?.get(name)?.syntax;
|
|
366
|
+
const split = splitGroupKey(name);
|
|
367
|
+
if (!split) {
|
|
368
|
+
ungroupedMap.set(formatKey(toKey(name, naming), naming), { name, value, syntax });
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
const groupKey = formatKey(toKeyFragment(split.group, naming), naming);
|
|
372
|
+
const leafKey = formatKey(toKeyFragment(split.rest, naming), naming);
|
|
373
|
+
let leafMap = groupMap.get(groupKey);
|
|
374
|
+
if (!leafMap) {
|
|
375
|
+
leafMap = /* @__PURE__ */ new Map();
|
|
376
|
+
groupMap.set(groupKey, leafMap);
|
|
377
|
+
}
|
|
378
|
+
leafMap.set(leafKey, { name, value, syntax });
|
|
379
|
+
}
|
|
380
|
+
for (const groupKey of groupMap.keys()) ungroupedMap.delete(groupKey);
|
|
381
|
+
const groups = [...groupMap].map(([key, leafMap]) => ({
|
|
382
|
+
key,
|
|
383
|
+
entries: [...leafMap].map(([key2, entry]) => ({ key: key2, ...entry }))
|
|
384
|
+
}));
|
|
385
|
+
const ungrouped = [...ungroupedMap].map(([key, entry]) => ({ key, ...entry }));
|
|
386
|
+
return { groups, ungrouped };
|
|
387
|
+
}
|
|
388
|
+
function formatDefaultComment(value, indent = " ") {
|
|
389
|
+
return `${indent}/** @default ${value.replace(/\*\//g, "*\\/")} */`;
|
|
390
|
+
}
|
|
391
|
+
function unquoteCssString(value) {
|
|
392
|
+
const quote = value[0];
|
|
393
|
+
if ((quote === '"' || quote === "'") && value[value.length - 1] === quote) {
|
|
394
|
+
return value.slice(1, -1);
|
|
395
|
+
}
|
|
396
|
+
return value;
|
|
397
|
+
}
|
|
398
|
+
function formatSyntaxComment(syntax, indent = " ") {
|
|
399
|
+
return `${indent}/** @syntax ${unquoteCssString(syntax).replace(/\*\//g, "*\\/")} */`;
|
|
400
|
+
}
|
|
401
|
+
var ENUM_SYNTAX_RE = /^[\w-]+(?:\s*\|\s*[\w-]+)+$/;
|
|
402
|
+
function parseEnumSyntax(syntax) {
|
|
403
|
+
const unquoted = unquoteCssString(syntax).trim();
|
|
404
|
+
if (!ENUM_SYNTAX_RE.test(unquoted)) return null;
|
|
405
|
+
return unquoted.split("|").map((v) => v.trim());
|
|
406
|
+
}
|
|
407
|
+
function syntaxTypeName(cssVarName) {
|
|
408
|
+
const pascal = convertCase(cssVarName.replace(/^--/, ""), "pascal");
|
|
409
|
+
return (/^\d/.test(pascal) ? `_${pascal}` : pascal) + "Syntax";
|
|
410
|
+
}
|
|
411
|
+
function buildSyntaxTypes(varNames, properties) {
|
|
412
|
+
if (!properties) return [];
|
|
413
|
+
const byName = /* @__PURE__ */ new Map();
|
|
414
|
+
for (const name of varNames) {
|
|
415
|
+
const values = parseEnumSyntax(properties.get(name)?.syntax ?? "");
|
|
416
|
+
if (values) byName.set(syntaxTypeName(name), values);
|
|
254
417
|
}
|
|
255
|
-
return [...
|
|
418
|
+
return [...byName].map(([name, values]) => ({ name, values }));
|
|
256
419
|
}
|
|
257
|
-
function
|
|
258
|
-
|
|
420
|
+
function renderSyntaxTypeLines(types) {
|
|
421
|
+
return types.map(({ name, values }) => `export type ${name} = ${values.map((v) => `'${escapeSingleQuoted(v)}'`).join(" | ")};`);
|
|
422
|
+
}
|
|
423
|
+
function buildTailBlocks(varNames, group, properties) {
|
|
424
|
+
const blocks = [];
|
|
425
|
+
if (!group) blocks.push(["export type CssVarName = keyof typeof cssVars;"]);
|
|
426
|
+
const syntaxLines = renderSyntaxTypeLines(buildSyntaxTypes(varNames, properties));
|
|
427
|
+
if (syntaxLines.length > 0) blocks.push(syntaxLines);
|
|
428
|
+
return blocks;
|
|
429
|
+
}
|
|
430
|
+
function renderEntryLines(entries, indent, lineFor) {
|
|
431
|
+
return entries.flatMap(({ key, name, value, syntax }) => [
|
|
432
|
+
...value ? [formatDefaultComment(value, indent)] : [],
|
|
433
|
+
...syntax ? [formatSyntaxComment(syntax, indent)] : [],
|
|
434
|
+
`${indent}${lineFor(key, name)}`
|
|
435
|
+
]);
|
|
436
|
+
}
|
|
437
|
+
function renderGroupedBlock(varNames, naming, declarations, properties, indent, closeSuffix, lineFor) {
|
|
438
|
+
const { groups, ungrouped } = buildGroupedEntries(varNames, naming, declarations, properties);
|
|
439
|
+
const lines = [];
|
|
440
|
+
for (const group of groups) {
|
|
441
|
+
lines.push(`${indent}${group.key}: {`);
|
|
442
|
+
lines.push(...renderEntryLines(group.entries, indent + " ", lineFor));
|
|
443
|
+
lines.push(`${indent}}${closeSuffix}`);
|
|
444
|
+
}
|
|
445
|
+
lines.push(...renderEntryLines(ungrouped, indent, lineFor));
|
|
446
|
+
return lines;
|
|
447
|
+
}
|
|
448
|
+
function assertGroupPrefixCompatible(prefix, group) {
|
|
449
|
+
if (group && prefix) {
|
|
450
|
+
throw new Error('css-typed-vars: the "group" option cannot be combined with "prefix".');
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
var codeLineFor = (key, name) => `${key}: 'var(${name})',`;
|
|
454
|
+
var declarationLineFor = (key, name) => `${key}: 'var(${name})';`;
|
|
455
|
+
function generateJs(varNames, prefix, naming, declarations, group, properties) {
|
|
456
|
+
assertGroupPrefixCompatible(prefix, group);
|
|
457
|
+
const entries = group ? renderGroupedBlock(varNames, naming, declarations, properties, " ", ",", codeLineFor) : renderEntryLines(buildEntries(varNames, prefix, naming, declarations, properties), " ", codeLineFor);
|
|
259
458
|
return [
|
|
260
459
|
"// generated \u2014 do not edit",
|
|
261
460
|
"export const cssVars = {",
|
|
@@ -264,13 +463,16 @@ function generateJs(varNames, prefix, naming) {
|
|
|
264
463
|
""
|
|
265
464
|
].join("\n");
|
|
266
465
|
}
|
|
267
|
-
function generateDeclaration(varNames, prefix, naming) {
|
|
268
|
-
|
|
466
|
+
function generateDeclaration(varNames, prefix, naming, declarations, group, properties) {
|
|
467
|
+
assertGroupPrefixCompatible(prefix, group);
|
|
468
|
+
const entries = group ? renderGroupedBlock(varNames, naming, declarations, properties, " ", ";", declarationLineFor) : renderEntryLines(buildEntries(varNames, prefix, naming, declarations, properties), " ", declarationLineFor);
|
|
469
|
+
const blocks = buildTailBlocks(varNames, group, properties);
|
|
470
|
+
const tailBody = blocks.flatMap((block, i) => i === 0 ? block : ["", ...block]);
|
|
269
471
|
return [
|
|
270
472
|
"export declare const cssVars: {",
|
|
271
473
|
...entries,
|
|
272
474
|
"};",
|
|
273
|
-
|
|
475
|
+
...tailBody,
|
|
274
476
|
""
|
|
275
477
|
].join("\n");
|
|
276
478
|
}
|
|
@@ -283,7 +485,27 @@ function getDtsPath(options) {
|
|
|
283
485
|
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
284
486
|
}
|
|
285
487
|
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
286
|
-
|
|
488
|
+
if (options.prune) {
|
|
489
|
+
if (options.group) throw new Error('css-typed-vars: "prune" cannot be combined with "group".');
|
|
490
|
+
if (!options.usage) throw new Error('css-typed-vars: "prune" requires "usage" (glob(s) of source files to scan).');
|
|
491
|
+
}
|
|
492
|
+
let cachedScan = null;
|
|
493
|
+
const scan = () => scanCss(options.input, options.exclude, options.selectors);
|
|
494
|
+
const applyPrune = async (names) => {
|
|
495
|
+
if (!options.prune) return names;
|
|
496
|
+
const usedKeys = await scanUsedKeys(options.usage, options.usageExclude);
|
|
497
|
+
const isUsed = (name) => usedKeys.has(computeKey(name, options.prefix, options.naming));
|
|
498
|
+
const unused = names.filter((name) => !isUsed(name));
|
|
499
|
+
if (unused.length > 0) {
|
|
500
|
+
console.warn(`css-typed-vars: pruned ${unused.length} unused variable(s): ${unused.join(", ")}`);
|
|
501
|
+
}
|
|
502
|
+
return names.filter(isUsed);
|
|
503
|
+
};
|
|
504
|
+
const resolveNames = async (declarations) => {
|
|
505
|
+
const names = [...declarations.keys()].sort();
|
|
506
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
507
|
+
return applyPrune(names);
|
|
508
|
+
};
|
|
287
509
|
return {
|
|
288
510
|
name: "css-typed-vars",
|
|
289
511
|
vite: {
|
|
@@ -294,14 +516,15 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
294
516
|
let writeQueue = Promise.resolve();
|
|
295
517
|
const rescan = async () => {
|
|
296
518
|
const myGeneration = ++generation;
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
const
|
|
519
|
+
const scanPromise = scan();
|
|
520
|
+
cachedScan = scanPromise;
|
|
521
|
+
const { declarations, properties } = await scanPromise;
|
|
522
|
+
if (myGeneration !== generation) return;
|
|
523
|
+
const names = await resolveNames(declarations);
|
|
300
524
|
if (myGeneration !== generation) return;
|
|
301
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
302
525
|
const dtsPath = getDtsPath(options);
|
|
303
526
|
if (dtsPath) {
|
|
304
|
-
const content = generateDeclaration(names, options.prefix, options.naming);
|
|
527
|
+
const content = generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties);
|
|
305
528
|
writeQueue = writeQueue.then(async () => {
|
|
306
529
|
if (myGeneration !== generation) return;
|
|
307
530
|
await (0, import_promises2.writeFile)(dtsPath, content, "utf8");
|
|
@@ -329,20 +552,21 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
329
552
|
}
|
|
330
553
|
},
|
|
331
554
|
async buildStart() {
|
|
332
|
-
|
|
333
|
-
const
|
|
334
|
-
|
|
555
|
+
cachedScan = scan();
|
|
556
|
+
const { declarations, properties } = await cachedScan;
|
|
557
|
+
const names = await resolveNames(declarations);
|
|
335
558
|
const dtsPath = getDtsPath(options);
|
|
336
559
|
if (!dtsPath) return;
|
|
337
|
-
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
560
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming, declarations, options.group, properties), "utf8");
|
|
338
561
|
},
|
|
339
562
|
resolveId(id) {
|
|
340
563
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
341
564
|
},
|
|
342
565
|
async load(id) {
|
|
343
566
|
if (id === RESOLVED_ID) {
|
|
344
|
-
const
|
|
345
|
-
|
|
567
|
+
const { declarations, properties } = await (cachedScan ?? scan());
|
|
568
|
+
const names = await applyPrune([...declarations.keys()].sort());
|
|
569
|
+
return generateJs(names, options.prefix, options.naming, declarations, options.group, properties);
|
|
346
570
|
}
|
|
347
571
|
}
|
|
348
572
|
};
|
package/dist/unplugin.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as unplugin from 'unplugin';
|
|
2
|
-
import { N as NamingConvention } from './generator-
|
|
2
|
+
import { N as NamingConvention } from './generator-C2AppeJL.cjs';
|
|
3
3
|
|
|
4
4
|
interface Options {
|
|
5
5
|
input: string | string[];
|
|
@@ -14,6 +14,12 @@ interface Options {
|
|
|
14
14
|
prefix?: string;
|
|
15
15
|
naming?: NamingConvention;
|
|
16
16
|
selectors?: string[];
|
|
17
|
+
group?: boolean;
|
|
18
|
+
/** Prune generated vars with no detected `cssVars.<key>` usage. Requires `usage`, and can't be combined with `group`. */
|
|
19
|
+
prune?: boolean;
|
|
20
|
+
/** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true. */
|
|
21
|
+
usage?: string | string[];
|
|
22
|
+
usageExclude?: string | string[];
|
|
17
23
|
}
|
|
18
24
|
declare const _default: unplugin.UnpluginInstance<Options, boolean>;
|
|
19
25
|
|
package/dist/unplugin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as unplugin from 'unplugin';
|
|
2
|
-
import { N as NamingConvention } from './generator-
|
|
2
|
+
import { N as NamingConvention } from './generator-C2AppeJL.js';
|
|
3
3
|
|
|
4
4
|
interface Options {
|
|
5
5
|
input: string | string[];
|
|
@@ -14,6 +14,12 @@ interface Options {
|
|
|
14
14
|
prefix?: string;
|
|
15
15
|
naming?: NamingConvention;
|
|
16
16
|
selectors?: string[];
|
|
17
|
+
group?: boolean;
|
|
18
|
+
/** Prune generated vars with no detected `cssVars.<key>` usage. Requires `usage`, and can't be combined with `group`. */
|
|
19
|
+
prune?: boolean;
|
|
20
|
+
/** Glob(s) of source files to scan for `cssVars` usage. Required when `prune` is true. */
|
|
21
|
+
usage?: string | string[];
|
|
22
|
+
usageExclude?: string | string[];
|
|
17
23
|
}
|
|
18
24
|
declare const _default: unplugin.UnpluginInstance<Options, boolean>;
|
|
19
25
|
|