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
package/dist/chunk-H3T6RMQQ.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
generateCode,
|
|
3
|
-
generateDeclaration,
|
|
4
|
-
generateJs,
|
|
5
|
-
scanVarDeclarations,
|
|
6
|
-
warnOnCollisions
|
|
7
|
-
} from "./chunk-JDZAH2YQ.js";
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
import { writeFile, mkdir, readFile } from "fs/promises";
|
|
11
|
-
import { dirname, resolve } from "path";
|
|
12
|
-
async function computeOutputs(options) {
|
|
13
|
-
const declarations = await scanVarDeclarations(options.input, options.exclude, options.selectors);
|
|
14
|
-
const names = [...declarations.keys()].sort();
|
|
15
|
-
if (names.length === 0) {
|
|
16
|
-
console.warn("css-typed-vars: no CSS custom properties found.");
|
|
17
|
-
}
|
|
18
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
19
|
-
const outPath = resolve(options.output);
|
|
20
|
-
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
|
21
|
-
if (jsExtMatch) {
|
|
22
|
-
const content = generateJs(names, options.prefix, options.naming, declarations);
|
|
23
|
-
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
24
|
-
const dtsContent = generateDeclaration(names, options.prefix, options.naming, declarations);
|
|
25
|
-
return { outPath, content, dtsPath, dtsContent };
|
|
26
|
-
}
|
|
27
|
-
return { outPath, content: generateCode(names, options.prefix, options.naming, declarations) };
|
|
28
|
-
}
|
|
29
|
-
async function generate(options) {
|
|
30
|
-
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
31
|
-
await mkdir(dirname(outPath), { recursive: true });
|
|
32
|
-
await writeFile(outPath, content, "utf8");
|
|
33
|
-
if (dtsPath && dtsContent !== void 0) {
|
|
34
|
-
await writeFile(dtsPath, dtsContent, "utf8");
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
async function checkGenerated(options) {
|
|
38
|
-
const { outPath, content, dtsPath, dtsContent } = await computeOutputs(options);
|
|
39
|
-
const current = await readFile(outPath, "utf8").catch(() => null);
|
|
40
|
-
if (current !== content) return false;
|
|
41
|
-
if (dtsPath && dtsContent !== void 0) {
|
|
42
|
-
const currentDts = await readFile(dtsPath, "utf8").catch(() => null);
|
|
43
|
-
if (currentDts !== dtsContent) return false;
|
|
44
|
-
}
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export {
|
|
49
|
-
generate,
|
|
50
|
-
checkGenerated
|
|
51
|
-
};
|
package/dist/chunk-JDZAH2YQ.js
DELETED
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
// node_modules/tsup/assets/esm_shims.js
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
var getFilename = () => fileURLToPath(import.meta.url);
|
|
5
|
-
var getDirname = () => path.dirname(getFilename());
|
|
6
|
-
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
|
-
|
|
8
|
-
// src/parser.ts
|
|
9
|
-
function stepQuote(text, i, quote) {
|
|
10
|
-
const char = text[i];
|
|
11
|
-
if (char === "\\" && i + 1 < text.length) return { consumed: 2, closed: false };
|
|
12
|
-
if (char === quote) return { consumed: 1, closed: true };
|
|
13
|
-
return { consumed: 1, closed: false };
|
|
14
|
-
}
|
|
15
|
-
function stripComments(text) {
|
|
16
|
-
let result = "";
|
|
17
|
-
let quote = null;
|
|
18
|
-
let urlDepth = 0;
|
|
19
|
-
for (let i = 0; i < text.length; i++) {
|
|
20
|
-
const char = text[i];
|
|
21
|
-
if (quote) {
|
|
22
|
-
const step = stepQuote(text, i, quote);
|
|
23
|
-
result += text.slice(i, i + step.consumed);
|
|
24
|
-
if (step.closed) quote = null;
|
|
25
|
-
i += step.consumed - 1;
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
if (char === '"' || char === "'") {
|
|
29
|
-
quote = char;
|
|
30
|
-
result += char;
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
if (urlDepth > 0) {
|
|
34
|
-
if (char === "(") urlDepth++;
|
|
35
|
-
else if (char === ")") urlDepth--;
|
|
36
|
-
result += char;
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (char === "(" && result.slice(-3).toLowerCase() === "url") {
|
|
40
|
-
urlDepth = 1;
|
|
41
|
-
result += char;
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
if (char === "/" && text[i + 1] === "*") {
|
|
45
|
-
const end = text.indexOf("*/", i + 2);
|
|
46
|
-
if (end === -1) {
|
|
47
|
-
result += char;
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
i = end + 1;
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
if (char === "/" && text[i + 1] === "/" && text[i - 1] !== ":") {
|
|
54
|
-
const nl = text.indexOf("\n", i);
|
|
55
|
-
i = nl === -1 ? text.length - 1 : nl - 1;
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
result += char;
|
|
59
|
-
}
|
|
60
|
-
return result;
|
|
61
|
-
}
|
|
62
|
-
function extractBlock(text, start) {
|
|
63
|
-
let depth = 1;
|
|
64
|
-
let quote = null;
|
|
65
|
-
let i = start;
|
|
66
|
-
for (; i < text.length && depth > 0; i++) {
|
|
67
|
-
const char = text[i];
|
|
68
|
-
if (quote) {
|
|
69
|
-
const step = stepQuote(text, i, quote);
|
|
70
|
-
if (step.closed) quote = null;
|
|
71
|
-
i += step.consumed - 1;
|
|
72
|
-
} else if (char === '"' || char === "'") {
|
|
73
|
-
quote = char;
|
|
74
|
-
} else if (char === "{") {
|
|
75
|
-
depth++;
|
|
76
|
-
} else if (char === "}") {
|
|
77
|
-
depth--;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
81
|
-
}
|
|
82
|
-
function maskQuotedContent(text) {
|
|
83
|
-
let result = "";
|
|
84
|
-
let quote = null;
|
|
85
|
-
for (let i = 0; i < text.length; i++) {
|
|
86
|
-
const char = text[i];
|
|
87
|
-
if (quote) {
|
|
88
|
-
const step = stepQuote(text, i, quote);
|
|
89
|
-
if (step.closed) {
|
|
90
|
-
result += char;
|
|
91
|
-
quote = null;
|
|
92
|
-
} else {
|
|
93
|
-
result += " ".repeat(step.consumed);
|
|
94
|
-
}
|
|
95
|
-
i += step.consumed - 1;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (char === '"' || char === "'") {
|
|
99
|
-
quote = char;
|
|
100
|
-
}
|
|
101
|
-
result += char;
|
|
102
|
-
}
|
|
103
|
-
return result;
|
|
104
|
-
}
|
|
105
|
-
function extractValue(text, start) {
|
|
106
|
-
let i = start;
|
|
107
|
-
let quote = null;
|
|
108
|
-
for (; i < text.length; i++) {
|
|
109
|
-
const char = text[i];
|
|
110
|
-
if (quote) {
|
|
111
|
-
const step = stepQuote(text, i, quote);
|
|
112
|
-
if (step.closed) quote = null;
|
|
113
|
-
i += step.consumed - 1;
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
if (char === '"' || char === "'") {
|
|
117
|
-
quote = char;
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
if (char === ";" || char === "}") break;
|
|
121
|
-
}
|
|
122
|
-
return text.slice(start, i).trim();
|
|
123
|
-
}
|
|
124
|
-
function scanDeclarations(css, selectors, onProperty) {
|
|
125
|
-
const stripped = stripComments(css);
|
|
126
|
-
const allSelectors = [":root", ...selectors ?? []];
|
|
127
|
-
for (const sel of allSelectors) {
|
|
128
|
-
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
129
|
-
const openRegex = new RegExp(`${escaped}(?![\\w-])[^{]*\\{`, "g");
|
|
130
|
-
let match;
|
|
131
|
-
while (match = openRegex.exec(stripped)) {
|
|
132
|
-
const start = match.index + match[0].length;
|
|
133
|
-
const block = extractBlock(stripped, start);
|
|
134
|
-
const masked = maskQuotedContent(block);
|
|
135
|
-
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
136
|
-
const name = prop[0];
|
|
137
|
-
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
138
|
-
onProperty(name, extractValue(block, colonIndex + 1));
|
|
139
|
-
}
|
|
140
|
-
openRegex.lastIndex = start + block.length + 1;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
function parseVarNames(css, selectors) {
|
|
145
|
-
const names = /* @__PURE__ */ new Set();
|
|
146
|
-
scanDeclarations(css, selectors, (name) => names.add(name));
|
|
147
|
-
return [...names];
|
|
148
|
-
}
|
|
149
|
-
function parseVarDeclarations(css, selectors) {
|
|
150
|
-
const declarations = /* @__PURE__ */ new Map();
|
|
151
|
-
scanDeclarations(css, selectors, (name, value) => declarations.set(name, value));
|
|
152
|
-
return declarations;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// src/scanner.ts
|
|
156
|
-
import { readFile } from "fs/promises";
|
|
157
|
-
import fg from "fast-glob";
|
|
158
|
-
function normalizePatterns(patterns) {
|
|
159
|
-
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
160
|
-
}
|
|
161
|
-
async function scanVarDeclarations(patterns, exclude, selectors) {
|
|
162
|
-
const normalized = normalizePatterns(patterns);
|
|
163
|
-
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
164
|
-
const files = await fg(normalized, { absolute: true, ignore: normalizedExclude });
|
|
165
|
-
const perFile = await Promise.all(
|
|
166
|
-
files.map(async (file) => {
|
|
167
|
-
let css;
|
|
168
|
-
try {
|
|
169
|
-
css = await readFile(file, "utf8");
|
|
170
|
-
} catch (err) {
|
|
171
|
-
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
172
|
-
return /* @__PURE__ */ new Map();
|
|
173
|
-
}
|
|
174
|
-
return parseVarDeclarations(css, selectors);
|
|
175
|
-
})
|
|
176
|
-
);
|
|
177
|
-
const all = /* @__PURE__ */ new Map();
|
|
178
|
-
for (const declarations of perFile) {
|
|
179
|
-
for (const [name, value] of declarations) {
|
|
180
|
-
all.set(name, value);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return all;
|
|
184
|
-
}
|
|
185
|
-
async function scanVarNames(patterns, exclude, selectors) {
|
|
186
|
-
const declarations = await scanVarDeclarations(patterns, exclude, selectors);
|
|
187
|
-
return [...declarations.keys()].sort();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// src/generator.ts
|
|
191
|
-
var VALID_NAMINGS = ["camelCase", "snake", "kebab", "constant", "pascal"];
|
|
192
|
-
function convertCase(value, naming) {
|
|
193
|
-
if (!VALID_NAMINGS.includes(naming)) {
|
|
194
|
-
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
195
|
-
}
|
|
196
|
-
if (naming === "kebab") return value;
|
|
197
|
-
if (naming === "snake") return value.replace(/-+/g, "_");
|
|
198
|
-
if (naming === "constant") return value.replace(/-+/g, "_").toUpperCase();
|
|
199
|
-
if (naming === "pascal") return value.replace(/(^|-+)([a-z0-9])/g, (_, __, c) => c.toUpperCase()).replace(/-/g, "");
|
|
200
|
-
return value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
201
|
-
}
|
|
202
|
-
function toKey(cssVarName, naming = "camelCase") {
|
|
203
|
-
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
204
|
-
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
205
|
-
}
|
|
206
|
-
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
207
|
-
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
208
|
-
var SINGLE_QUOTE_ESCAPES = {};
|
|
209
|
-
SINGLE_QUOTE_ESCAPES["\\"] = "\\\\";
|
|
210
|
-
SINGLE_QUOTE_ESCAPES["'"] = "\\'";
|
|
211
|
-
SINGLE_QUOTE_ESCAPES["\n"] = "\\n";
|
|
212
|
-
SINGLE_QUOTE_ESCAPES["\r"] = "\\r";
|
|
213
|
-
SINGLE_QUOTE_ESCAPES[LINE_SEPARATOR_CHAR] = "\\u2028";
|
|
214
|
-
SINGLE_QUOTE_ESCAPES[PARAGRAPH_SEPARATOR_CHAR] = "\\u2029";
|
|
215
|
-
function escapeSingleQuoted(value) {
|
|
216
|
-
let result = "";
|
|
217
|
-
for (const char of value) {
|
|
218
|
-
result += SINGLE_QUOTE_ESCAPES[char] ?? char;
|
|
219
|
-
}
|
|
220
|
-
return result;
|
|
221
|
-
}
|
|
222
|
-
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
223
|
-
if (!prefix) return key;
|
|
224
|
-
let normalizedPrefix = convertCase(prefix, naming);
|
|
225
|
-
if (naming === "kebab") {
|
|
226
|
-
normalizedPrefix = escapeSingleQuoted(normalizedPrefix);
|
|
227
|
-
return `${normalizedPrefix}-${key}`;
|
|
228
|
-
}
|
|
229
|
-
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
230
|
-
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
231
|
-
if (naming === "snake" || naming === "constant") return `${normalizedPrefix}_${key}`;
|
|
232
|
-
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
233
|
-
}
|
|
234
|
-
function formatKey(key, naming = "camelCase") {
|
|
235
|
-
if (naming === "kebab") return `'${key}'`;
|
|
236
|
-
return key;
|
|
237
|
-
}
|
|
238
|
-
function findKeyCollisions(varNames, prefix, naming) {
|
|
239
|
-
const byKey = /* @__PURE__ */ new Map();
|
|
240
|
-
for (const name of varNames) {
|
|
241
|
-
const key = applyPrefix(toKey(name, naming), prefix, naming);
|
|
242
|
-
const existing = byKey.get(key);
|
|
243
|
-
if (existing) existing.push(name);
|
|
244
|
-
else byKey.set(key, [name]);
|
|
245
|
-
}
|
|
246
|
-
for (const [key, names] of byKey) {
|
|
247
|
-
if (names.length < 2) byKey.delete(key);
|
|
248
|
-
}
|
|
249
|
-
return byKey;
|
|
250
|
-
}
|
|
251
|
-
function warnOnCollisions(varNames, prefix, naming) {
|
|
252
|
-
for (const [key, vars] of findKeyCollisions(varNames, prefix, naming)) {
|
|
253
|
-
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
function buildEntries(varNames, prefix, naming, declarations) {
|
|
257
|
-
const byKey = /* @__PURE__ */ new Map();
|
|
258
|
-
for (const name of varNames) {
|
|
259
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
260
|
-
byKey.set(key, { name, value: declarations?.get(name) });
|
|
261
|
-
}
|
|
262
|
-
return [...byKey].map(([key, entry]) => ({ key, ...entry }));
|
|
263
|
-
}
|
|
264
|
-
function formatDefaultComment(value) {
|
|
265
|
-
return ` /** @default ${value.replace(/\*\//g, "*\\/")} */`;
|
|
266
|
-
}
|
|
267
|
-
function generateCode(varNames, prefix, naming, declarations) {
|
|
268
|
-
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
269
|
-
...value ? [formatDefaultComment(value)] : [],
|
|
270
|
-
` ${key}: 'var(${name})',`
|
|
271
|
-
]);
|
|
272
|
-
return [
|
|
273
|
-
"// generated \u2014 do not edit",
|
|
274
|
-
"export const cssVars = {",
|
|
275
|
-
...entries,
|
|
276
|
-
"} as const;",
|
|
277
|
-
"",
|
|
278
|
-
"export type CssVarName = keyof typeof cssVars;",
|
|
279
|
-
""
|
|
280
|
-
].join("\n");
|
|
281
|
-
}
|
|
282
|
-
function generateJs(varNames, prefix, naming, declarations) {
|
|
283
|
-
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
284
|
-
...value ? [formatDefaultComment(value)] : [],
|
|
285
|
-
` ${key}: 'var(${name})',`
|
|
286
|
-
]);
|
|
287
|
-
return [
|
|
288
|
-
"// generated \u2014 do not edit",
|
|
289
|
-
"export const cssVars = {",
|
|
290
|
-
...entries,
|
|
291
|
-
"};",
|
|
292
|
-
""
|
|
293
|
-
].join("\n");
|
|
294
|
-
}
|
|
295
|
-
function generateDeclaration(varNames, prefix, naming, declarations) {
|
|
296
|
-
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
297
|
-
...value ? [formatDefaultComment(value)] : [],
|
|
298
|
-
` ${key}: 'var(${name})';`
|
|
299
|
-
]);
|
|
300
|
-
return [
|
|
301
|
-
"export declare const cssVars: {",
|
|
302
|
-
...entries,
|
|
303
|
-
"};",
|
|
304
|
-
"export type CssVarName = keyof typeof cssVars;",
|
|
305
|
-
""
|
|
306
|
-
].join("\n");
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export {
|
|
310
|
-
__dirname,
|
|
311
|
-
parseVarNames,
|
|
312
|
-
parseVarDeclarations,
|
|
313
|
-
scanVarDeclarations,
|
|
314
|
-
scanVarNames,
|
|
315
|
-
VALID_NAMINGS,
|
|
316
|
-
warnOnCollisions,
|
|
317
|
-
generateCode,
|
|
318
|
-
generateJs,
|
|
319
|
-
generateDeclaration
|
|
320
|
-
};
|
package/dist/chunk-YBZGHG2G.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__dirname,
|
|
3
|
-
generateDeclaration,
|
|
4
|
-
generateJs,
|
|
5
|
-
scanVarDeclarations,
|
|
6
|
-
warnOnCollisions
|
|
7
|
-
} from "./chunk-JDZAH2YQ.js";
|
|
8
|
-
|
|
9
|
-
// src/unplugin.ts
|
|
10
|
-
import { createUnplugin } from "unplugin";
|
|
11
|
-
import { writeFile } from "fs/promises";
|
|
12
|
-
import { join, resolve } from "path";
|
|
13
|
-
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
14
|
-
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
15
|
-
function getDtsPath(options) {
|
|
16
|
-
if (options.dts === false) return null;
|
|
17
|
-
return options.dts ? resolve(process.cwd(), options.dts) : join(__dirname, "generated.d.ts");
|
|
18
|
-
}
|
|
19
|
-
var unplugin_default = createUnplugin((options) => {
|
|
20
|
-
let cachedDeclarations = null;
|
|
21
|
-
const scan = () => scanVarDeclarations(options.input, options.exclude, options.selectors);
|
|
22
|
-
return {
|
|
23
|
-
name: "css-typed-vars",
|
|
24
|
-
vite: {
|
|
25
|
-
enforce: "pre",
|
|
26
|
-
configureServer(server) {
|
|
27
|
-
let generation = 0;
|
|
28
|
-
let debounceTimer = null;
|
|
29
|
-
let writeQueue = Promise.resolve();
|
|
30
|
-
const rescan = async () => {
|
|
31
|
-
const myGeneration = ++generation;
|
|
32
|
-
const declarationsPromise = scan();
|
|
33
|
-
cachedDeclarations = declarationsPromise;
|
|
34
|
-
const declarations = await declarationsPromise;
|
|
35
|
-
if (myGeneration !== generation) return;
|
|
36
|
-
const names = [...declarations.keys()].sort();
|
|
37
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
38
|
-
const dtsPath = getDtsPath(options);
|
|
39
|
-
if (dtsPath) {
|
|
40
|
-
const content = generateDeclaration(names, options.prefix, options.naming, declarations);
|
|
41
|
-
writeQueue = writeQueue.then(async () => {
|
|
42
|
-
if (myGeneration !== generation) return;
|
|
43
|
-
await writeFile(dtsPath, content, "utf8");
|
|
44
|
-
}).catch(console.error);
|
|
45
|
-
await writeQueue;
|
|
46
|
-
}
|
|
47
|
-
if (myGeneration !== generation) return;
|
|
48
|
-
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
49
|
-
if (mod) {
|
|
50
|
-
server.moduleGraph.invalidateModule(mod);
|
|
51
|
-
server.ws.send({ type: "full-reload" });
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const handle = (file) => {
|
|
55
|
-
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
56
|
-
if (debounceTimer) clearTimeout(debounceTimer);
|
|
57
|
-
debounceTimer = setTimeout(() => {
|
|
58
|
-
debounceTimer = null;
|
|
59
|
-
rescan().catch(console.error);
|
|
60
|
-
}, 100);
|
|
61
|
-
};
|
|
62
|
-
server.watcher.on("change", handle);
|
|
63
|
-
server.watcher.on("add", handle);
|
|
64
|
-
server.watcher.on("unlink", handle);
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
async buildStart() {
|
|
68
|
-
cachedDeclarations = scan();
|
|
69
|
-
const declarations = await cachedDeclarations;
|
|
70
|
-
const names = [...declarations.keys()].sort();
|
|
71
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
72
|
-
const dtsPath = getDtsPath(options);
|
|
73
|
-
if (!dtsPath) return;
|
|
74
|
-
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming, declarations), "utf8");
|
|
75
|
-
},
|
|
76
|
-
resolveId(id) {
|
|
77
|
-
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
78
|
-
},
|
|
79
|
-
async load(id) {
|
|
80
|
-
if (id === RESOLVED_ID) {
|
|
81
|
-
const declarations = await (cachedDeclarations ?? scan());
|
|
82
|
-
const names = [...declarations.keys()].sort();
|
|
83
|
-
return generateJs(names, options.prefix, options.naming, declarations);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
export {
|
|
90
|
-
unplugin_default
|
|
91
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type NamingConvention = 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal';
|
|
2
|
-
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention, declarations?: Map<string, string>): string;
|
|
3
|
-
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention, declarations?: Map<string, string>): string;
|
|
4
|
-
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention, declarations?: Map<string, string>): string;
|
|
5
|
-
|
|
6
|
-
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type NamingConvention = 'camelCase' | 'snake' | 'kebab' | 'constant' | 'pascal';
|
|
2
|
-
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention, declarations?: Map<string, string>): string;
|
|
3
|
-
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention, declarations?: Map<string, string>): string;
|
|
4
|
-
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention, declarations?: Map<string, string>): string;
|
|
5
|
-
|
|
6
|
-
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|