css-typed-vars 0.4.14 → 0.4.16
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/dist/{chunk-2JDQ2DY7.js → chunk-ARQR224L.js} +4 -6
- package/dist/{chunk-3UIYKYIL.js → chunk-NS2BULH3.js} +51 -27
- package/dist/{chunk-HS4B2QT2.js → chunk-YJPHNISO.js} +33 -12
- package/dist/cli.cjs +52 -31
- package/dist/cli.js +6 -4
- package/dist/esbuild.cjs +91 -32
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +50 -29
- package/dist/index.js +2 -2
- package/dist/rollup.cjs +91 -32
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +91 -32
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +91 -32
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +91 -32
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -46,6 +46,12 @@ var import_promises = require("fs/promises");
|
|
|
46
46
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
47
47
|
|
|
48
48
|
// src/parser.ts
|
|
49
|
+
function stepQuote(text, i, quote) {
|
|
50
|
+
const char = text[i];
|
|
51
|
+
if (char === "\\" && i + 1 < text.length) return { consumed: 2, closed: false };
|
|
52
|
+
if (char === quote) return { consumed: 1, closed: true };
|
|
53
|
+
return { consumed: 1, closed: false };
|
|
54
|
+
}
|
|
49
55
|
function stripComments(text) {
|
|
50
56
|
let result = "";
|
|
51
57
|
let quote = null;
|
|
@@ -53,12 +59,10 @@ function stripComments(text) {
|
|
|
53
59
|
for (let i = 0; i < text.length; i++) {
|
|
54
60
|
const char = text[i];
|
|
55
61
|
if (quote) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
quote = null;
|
|
61
|
-
}
|
|
62
|
+
const step = stepQuote(text, i, quote);
|
|
63
|
+
result += text.slice(i, i + step.consumed);
|
|
64
|
+
if (step.closed) quote = null;
|
|
65
|
+
i += step.consumed - 1;
|
|
62
66
|
continue;
|
|
63
67
|
}
|
|
64
68
|
if (char === '"' || char === "'") {
|
|
@@ -120,15 +124,14 @@ function maskQuotedContent(text) {
|
|
|
120
124
|
for (let i = 0; i < text.length; i++) {
|
|
121
125
|
const char = text[i];
|
|
122
126
|
if (quote) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
i++;
|
|
126
|
-
} else if (char === quote) {
|
|
127
|
-
quote = null;
|
|
127
|
+
const step = stepQuote(text, i, quote);
|
|
128
|
+
if (step.closed) {
|
|
128
129
|
result += char;
|
|
130
|
+
quote = null;
|
|
129
131
|
} else {
|
|
130
|
-
result += " ";
|
|
132
|
+
result += " ".repeat(step.consumed);
|
|
131
133
|
}
|
|
134
|
+
i += step.consumed - 1;
|
|
132
135
|
continue;
|
|
133
136
|
}
|
|
134
137
|
if (char === '"' || char === "'") {
|
|
@@ -187,7 +190,7 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
187
190
|
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
188
191
|
function convertCase(value, naming) {
|
|
189
192
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
190
|
-
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values:
|
|
193
|
+
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
191
194
|
}
|
|
192
195
|
if (naming === "kebab") return value;
|
|
193
196
|
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
@@ -196,11 +199,27 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
196
199
|
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
197
200
|
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
198
201
|
}
|
|
202
|
+
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
203
|
+
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
204
|
+
var SINGLE_QUOTE_ESCAPES = {};
|
|
205
|
+
SINGLE_QUOTE_ESCAPES["\\"] = "\\\\";
|
|
206
|
+
SINGLE_QUOTE_ESCAPES["'"] = "\\'";
|
|
207
|
+
SINGLE_QUOTE_ESCAPES["\n"] = "\\n";
|
|
208
|
+
SINGLE_QUOTE_ESCAPES["\r"] = "\\r";
|
|
209
|
+
SINGLE_QUOTE_ESCAPES[LINE_SEPARATOR_CHAR] = "\\u2028";
|
|
210
|
+
SINGLE_QUOTE_ESCAPES[PARAGRAPH_SEPARATOR_CHAR] = "\\u2029";
|
|
211
|
+
function escapeSingleQuoted(value) {
|
|
212
|
+
let result = "";
|
|
213
|
+
for (const char of value) {
|
|
214
|
+
result += SINGLE_QUOTE_ESCAPES[char] ?? char;
|
|
215
|
+
}
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
199
218
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
200
219
|
if (!prefix) return key;
|
|
201
220
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
202
221
|
if (naming === "kebab") {
|
|
203
|
-
normalizedPrefix = normalizedPrefix
|
|
222
|
+
normalizedPrefix = escapeSingleQuoted(normalizedPrefix);
|
|
204
223
|
return `${normalizedPrefix}-${key}`;
|
|
205
224
|
}
|
|
206
225
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
@@ -225,11 +244,21 @@ function findKeyCollisions(varNames, prefix, naming) {
|
|
|
225
244
|
}
|
|
226
245
|
return byKey;
|
|
227
246
|
}
|
|
228
|
-
function
|
|
229
|
-
const
|
|
247
|
+
function warnOnCollisions(varNames, prefix, naming) {
|
|
248
|
+
for (const [key, vars] of findKeyCollisions(varNames, prefix, naming)) {
|
|
249
|
+
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function buildEntries(varNames, prefix, naming) {
|
|
253
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
254
|
+
for (const name of varNames) {
|
|
230
255
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
231
|
-
|
|
232
|
-
}
|
|
256
|
+
byKey.set(key, name);
|
|
257
|
+
}
|
|
258
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
259
|
+
}
|
|
260
|
+
function generateCode(varNames, prefix, naming) {
|
|
261
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
233
262
|
return [
|
|
234
263
|
"// generated \u2014 do not edit",
|
|
235
264
|
"export const cssVars = {",
|
|
@@ -241,10 +270,7 @@ function generateCode(varNames, prefix, naming) {
|
|
|
241
270
|
].join("\n");
|
|
242
271
|
}
|
|
243
272
|
function generateJs(varNames, prefix, naming) {
|
|
244
|
-
const entries = varNames.map((name) => {
|
|
245
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
246
|
-
return ` ${key}: 'var(${name})',`;
|
|
247
|
-
});
|
|
273
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
248
274
|
return [
|
|
249
275
|
"// generated \u2014 do not edit",
|
|
250
276
|
"export const cssVars = {",
|
|
@@ -254,10 +280,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
254
280
|
].join("\n");
|
|
255
281
|
}
|
|
256
282
|
function generateDeclaration(varNames, prefix, naming) {
|
|
257
|
-
const entries = varNames.map((name) => {
|
|
258
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
259
|
-
return ` ${key}: 'var(${name})';`;
|
|
260
|
-
});
|
|
283
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
261
284
|
return [
|
|
262
285
|
"export declare const cssVars: {",
|
|
263
286
|
...entries,
|
|
@@ -273,9 +296,7 @@ async function generate(options) {
|
|
|
273
296
|
if (names.length === 0) {
|
|
274
297
|
console.warn("css-typed-vars: no CSS custom properties found.");
|
|
275
298
|
}
|
|
276
|
-
|
|
277
|
-
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
278
|
-
}
|
|
299
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
279
300
|
const outPath = (0, import_node_path.resolve)(options.output);
|
|
280
301
|
await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
|
|
281
302
|
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
generate
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-ARQR224L.js";
|
|
4
4
|
import {
|
|
5
5
|
generateCode,
|
|
6
6
|
generateDeclaration,
|
|
7
7
|
generateJs,
|
|
8
8
|
parseVarNames,
|
|
9
9
|
scanVarNames
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-NS2BULH3.js";
|
|
11
11
|
export {
|
|
12
12
|
generate,
|
|
13
13
|
generateCode,
|
package/dist/rollup.cjs
CHANGED
|
@@ -44,6 +44,12 @@ var import_promises = require("fs/promises");
|
|
|
44
44
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
45
|
|
|
46
46
|
// src/parser.ts
|
|
47
|
+
function stepQuote(text, i, quote) {
|
|
48
|
+
const char = text[i];
|
|
49
|
+
if (char === "\\" && i + 1 < text.length) return { consumed: 2, closed: false };
|
|
50
|
+
if (char === quote) return { consumed: 1, closed: true };
|
|
51
|
+
return { consumed: 1, closed: false };
|
|
52
|
+
}
|
|
47
53
|
function stripComments(text) {
|
|
48
54
|
let result = "";
|
|
49
55
|
let quote = null;
|
|
@@ -51,12 +57,10 @@ function stripComments(text) {
|
|
|
51
57
|
for (let i = 0; i < text.length; i++) {
|
|
52
58
|
const char = text[i];
|
|
53
59
|
if (quote) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
quote = null;
|
|
59
|
-
}
|
|
60
|
+
const step = stepQuote(text, i, quote);
|
|
61
|
+
result += text.slice(i, i + step.consumed);
|
|
62
|
+
if (step.closed) quote = null;
|
|
63
|
+
i += step.consumed - 1;
|
|
60
64
|
continue;
|
|
61
65
|
}
|
|
62
66
|
if (char === '"' || char === "'") {
|
|
@@ -118,15 +122,14 @@ function maskQuotedContent(text) {
|
|
|
118
122
|
for (let i = 0; i < text.length; i++) {
|
|
119
123
|
const char = text[i];
|
|
120
124
|
if (quote) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
i++;
|
|
124
|
-
} else if (char === quote) {
|
|
125
|
-
quote = null;
|
|
125
|
+
const step = stepQuote(text, i, quote);
|
|
126
|
+
if (step.closed) {
|
|
126
127
|
result += char;
|
|
128
|
+
quote = null;
|
|
127
129
|
} else {
|
|
128
|
-
result += " ";
|
|
130
|
+
result += " ".repeat(step.consumed);
|
|
129
131
|
}
|
|
132
|
+
i += step.consumed - 1;
|
|
130
133
|
continue;
|
|
131
134
|
}
|
|
132
135
|
if (char === '"' || char === "'") {
|
|
@@ -185,7 +188,7 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
185
188
|
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
186
189
|
function convertCase(value, naming) {
|
|
187
190
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
188
|
-
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values:
|
|
191
|
+
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
189
192
|
}
|
|
190
193
|
if (naming === "kebab") return value;
|
|
191
194
|
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
@@ -194,11 +197,27 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
194
197
|
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
195
198
|
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
196
199
|
}
|
|
200
|
+
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
201
|
+
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
202
|
+
var SINGLE_QUOTE_ESCAPES = {};
|
|
203
|
+
SINGLE_QUOTE_ESCAPES["\\"] = "\\\\";
|
|
204
|
+
SINGLE_QUOTE_ESCAPES["'"] = "\\'";
|
|
205
|
+
SINGLE_QUOTE_ESCAPES["\n"] = "\\n";
|
|
206
|
+
SINGLE_QUOTE_ESCAPES["\r"] = "\\r";
|
|
207
|
+
SINGLE_QUOTE_ESCAPES[LINE_SEPARATOR_CHAR] = "\\u2028";
|
|
208
|
+
SINGLE_QUOTE_ESCAPES[PARAGRAPH_SEPARATOR_CHAR] = "\\u2029";
|
|
209
|
+
function escapeSingleQuoted(value) {
|
|
210
|
+
let result = "";
|
|
211
|
+
for (const char of value) {
|
|
212
|
+
result += SINGLE_QUOTE_ESCAPES[char] ?? char;
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
197
216
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
198
217
|
if (!prefix) return key;
|
|
199
218
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
200
219
|
if (naming === "kebab") {
|
|
201
|
-
normalizedPrefix = normalizedPrefix
|
|
220
|
+
normalizedPrefix = escapeSingleQuoted(normalizedPrefix);
|
|
202
221
|
return `${normalizedPrefix}-${key}`;
|
|
203
222
|
}
|
|
204
223
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
@@ -210,11 +229,34 @@ function formatKey(key, naming = "camelCase") {
|
|
|
210
229
|
if (naming === "kebab") return `'${key}'`;
|
|
211
230
|
return key;
|
|
212
231
|
}
|
|
213
|
-
function
|
|
214
|
-
const
|
|
232
|
+
function findKeyCollisions(varNames, prefix, naming) {
|
|
233
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
234
|
+
for (const name of varNames) {
|
|
235
|
+
const key = applyPrefix(toKey(name, naming), prefix, naming);
|
|
236
|
+
const existing = byKey.get(key);
|
|
237
|
+
if (existing) existing.push(name);
|
|
238
|
+
else byKey.set(key, [name]);
|
|
239
|
+
}
|
|
240
|
+
for (const [key, names] of byKey) {
|
|
241
|
+
if (names.length < 2) byKey.delete(key);
|
|
242
|
+
}
|
|
243
|
+
return byKey;
|
|
244
|
+
}
|
|
245
|
+
function warnOnCollisions(varNames, prefix, naming) {
|
|
246
|
+
for (const [key, vars] of findKeyCollisions(varNames, prefix, naming)) {
|
|
247
|
+
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function buildEntries(varNames, prefix, naming) {
|
|
251
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
252
|
+
for (const name of varNames) {
|
|
215
253
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
216
|
-
|
|
217
|
-
}
|
|
254
|
+
byKey.set(key, name);
|
|
255
|
+
}
|
|
256
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
257
|
+
}
|
|
258
|
+
function generateJs(varNames, prefix, naming) {
|
|
259
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
218
260
|
return [
|
|
219
261
|
"// generated \u2014 do not edit",
|
|
220
262
|
"export const cssVars = {",
|
|
@@ -224,10 +266,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
224
266
|
].join("\n");
|
|
225
267
|
}
|
|
226
268
|
function generateDeclaration(varNames, prefix, naming) {
|
|
227
|
-
const entries = varNames.map((name) => {
|
|
228
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
229
|
-
return ` ${key}: 'var(${name})';`;
|
|
230
|
-
});
|
|
269
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
231
270
|
return [
|
|
232
271
|
"export declare const cssVars: {",
|
|
233
272
|
...entries,
|
|
@@ -251,31 +290,51 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
251
290
|
vite: {
|
|
252
291
|
enforce: "pre",
|
|
253
292
|
configureServer(server) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
293
|
+
let generation = 0;
|
|
294
|
+
let debounceTimer = null;
|
|
295
|
+
let writeQueue = Promise.resolve();
|
|
296
|
+
const rescan = async () => {
|
|
297
|
+
const myGeneration = ++generation;
|
|
298
|
+
const namesPromise = scanVarNames(options.input, options.exclude, options.selectors);
|
|
299
|
+
cachedNames = namesPromise;
|
|
300
|
+
const names = await namesPromise;
|
|
301
|
+
if (myGeneration !== generation) return;
|
|
302
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
258
303
|
const dtsPath = getDtsPath(options);
|
|
259
304
|
if (dtsPath) {
|
|
260
|
-
|
|
305
|
+
const content = generateDeclaration(names, options.prefix, options.naming);
|
|
306
|
+
writeQueue = writeQueue.then(async () => {
|
|
307
|
+
if (myGeneration !== generation) return;
|
|
308
|
+
await (0, import_promises2.writeFile)(dtsPath, content, "utf8");
|
|
309
|
+
}).catch(console.error);
|
|
310
|
+
await writeQueue;
|
|
261
311
|
}
|
|
312
|
+
if (myGeneration !== generation) return;
|
|
262
313
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
263
314
|
if (mod) {
|
|
264
315
|
server.moduleGraph.invalidateModule(mod);
|
|
265
316
|
server.ws.send({ type: "full-reload" });
|
|
266
317
|
}
|
|
267
318
|
};
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
319
|
+
const handle = (file) => {
|
|
320
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
321
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
322
|
+
debounceTimer = setTimeout(() => {
|
|
323
|
+
debounceTimer = null;
|
|
324
|
+
rescan().catch(console.error);
|
|
325
|
+
}, 100);
|
|
326
|
+
};
|
|
327
|
+
server.watcher.on("change", handle);
|
|
328
|
+
server.watcher.on("add", handle);
|
|
329
|
+
server.watcher.on("unlink", handle);
|
|
272
330
|
}
|
|
273
331
|
},
|
|
274
332
|
async buildStart() {
|
|
275
333
|
cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
|
|
334
|
+
const names = await cachedNames;
|
|
335
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
276
336
|
const dtsPath = getDtsPath(options);
|
|
277
337
|
if (!dtsPath) return;
|
|
278
|
-
const names = await cachedNames;
|
|
279
338
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
280
339
|
},
|
|
281
340
|
resolveId(id) {
|
package/dist/rollup.js
CHANGED
package/dist/unplugin.cjs
CHANGED
|
@@ -42,6 +42,12 @@ var import_promises = require("fs/promises");
|
|
|
42
42
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
43
43
|
|
|
44
44
|
// src/parser.ts
|
|
45
|
+
function stepQuote(text, i, quote) {
|
|
46
|
+
const char = text[i];
|
|
47
|
+
if (char === "\\" && i + 1 < text.length) return { consumed: 2, closed: false };
|
|
48
|
+
if (char === quote) return { consumed: 1, closed: true };
|
|
49
|
+
return { consumed: 1, closed: false };
|
|
50
|
+
}
|
|
45
51
|
function stripComments(text) {
|
|
46
52
|
let result = "";
|
|
47
53
|
let quote = null;
|
|
@@ -49,12 +55,10 @@ function stripComments(text) {
|
|
|
49
55
|
for (let i = 0; i < text.length; i++) {
|
|
50
56
|
const char = text[i];
|
|
51
57
|
if (quote) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
quote = null;
|
|
57
|
-
}
|
|
58
|
+
const step = stepQuote(text, i, quote);
|
|
59
|
+
result += text.slice(i, i + step.consumed);
|
|
60
|
+
if (step.closed) quote = null;
|
|
61
|
+
i += step.consumed - 1;
|
|
58
62
|
continue;
|
|
59
63
|
}
|
|
60
64
|
if (char === '"' || char === "'") {
|
|
@@ -116,15 +120,14 @@ function maskQuotedContent(text) {
|
|
|
116
120
|
for (let i = 0; i < text.length; i++) {
|
|
117
121
|
const char = text[i];
|
|
118
122
|
if (quote) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
i++;
|
|
122
|
-
} else if (char === quote) {
|
|
123
|
-
quote = null;
|
|
123
|
+
const step = stepQuote(text, i, quote);
|
|
124
|
+
if (step.closed) {
|
|
124
125
|
result += char;
|
|
126
|
+
quote = null;
|
|
125
127
|
} else {
|
|
126
|
-
result += " ";
|
|
128
|
+
result += " ".repeat(step.consumed);
|
|
127
129
|
}
|
|
130
|
+
i += step.consumed - 1;
|
|
128
131
|
continue;
|
|
129
132
|
}
|
|
130
133
|
if (char === '"' || char === "'") {
|
|
@@ -183,7 +186,7 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
183
186
|
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
184
187
|
function convertCase(value, naming) {
|
|
185
188
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
186
|
-
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values:
|
|
189
|
+
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
187
190
|
}
|
|
188
191
|
if (naming === "kebab") return value;
|
|
189
192
|
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
@@ -192,11 +195,27 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
192
195
|
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
193
196
|
return naming !== "kebab" && /^\d/.test(key) ? `_${key}` : key;
|
|
194
197
|
}
|
|
198
|
+
var LINE_SEPARATOR_CHAR = String.fromCharCode(8232);
|
|
199
|
+
var PARAGRAPH_SEPARATOR_CHAR = String.fromCharCode(8233);
|
|
200
|
+
var SINGLE_QUOTE_ESCAPES = {};
|
|
201
|
+
SINGLE_QUOTE_ESCAPES["\\"] = "\\\\";
|
|
202
|
+
SINGLE_QUOTE_ESCAPES["'"] = "\\'";
|
|
203
|
+
SINGLE_QUOTE_ESCAPES["\n"] = "\\n";
|
|
204
|
+
SINGLE_QUOTE_ESCAPES["\r"] = "\\r";
|
|
205
|
+
SINGLE_QUOTE_ESCAPES[LINE_SEPARATOR_CHAR] = "\\u2028";
|
|
206
|
+
SINGLE_QUOTE_ESCAPES[PARAGRAPH_SEPARATOR_CHAR] = "\\u2029";
|
|
207
|
+
function escapeSingleQuoted(value) {
|
|
208
|
+
let result = "";
|
|
209
|
+
for (const char of value) {
|
|
210
|
+
result += SINGLE_QUOTE_ESCAPES[char] ?? char;
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
195
214
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
196
215
|
if (!prefix) return key;
|
|
197
216
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
198
217
|
if (naming === "kebab") {
|
|
199
|
-
normalizedPrefix = normalizedPrefix
|
|
218
|
+
normalizedPrefix = escapeSingleQuoted(normalizedPrefix);
|
|
200
219
|
return `${normalizedPrefix}-${key}`;
|
|
201
220
|
}
|
|
202
221
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
@@ -208,11 +227,34 @@ function formatKey(key, naming = "camelCase") {
|
|
|
208
227
|
if (naming === "kebab") return `'${key}'`;
|
|
209
228
|
return key;
|
|
210
229
|
}
|
|
211
|
-
function
|
|
212
|
-
const
|
|
230
|
+
function findKeyCollisions(varNames, prefix, naming) {
|
|
231
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
232
|
+
for (const name of varNames) {
|
|
233
|
+
const key = applyPrefix(toKey(name, naming), prefix, naming);
|
|
234
|
+
const existing = byKey.get(key);
|
|
235
|
+
if (existing) existing.push(name);
|
|
236
|
+
else byKey.set(key, [name]);
|
|
237
|
+
}
|
|
238
|
+
for (const [key, names] of byKey) {
|
|
239
|
+
if (names.length < 2) byKey.delete(key);
|
|
240
|
+
}
|
|
241
|
+
return byKey;
|
|
242
|
+
}
|
|
243
|
+
function warnOnCollisions(varNames, prefix, naming) {
|
|
244
|
+
for (const [key, vars] of findKeyCollisions(varNames, prefix, naming)) {
|
|
245
|
+
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function buildEntries(varNames, prefix, naming) {
|
|
249
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
250
|
+
for (const name of varNames) {
|
|
213
251
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
214
|
-
|
|
215
|
-
}
|
|
252
|
+
byKey.set(key, name);
|
|
253
|
+
}
|
|
254
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
255
|
+
}
|
|
256
|
+
function generateJs(varNames, prefix, naming) {
|
|
257
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
216
258
|
return [
|
|
217
259
|
"// generated \u2014 do not edit",
|
|
218
260
|
"export const cssVars = {",
|
|
@@ -222,10 +264,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
222
264
|
].join("\n");
|
|
223
265
|
}
|
|
224
266
|
function generateDeclaration(varNames, prefix, naming) {
|
|
225
|
-
const entries = varNames.map((name) => {
|
|
226
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
227
|
-
return ` ${key}: 'var(${name})';`;
|
|
228
|
-
});
|
|
267
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
229
268
|
return [
|
|
230
269
|
"export declare const cssVars: {",
|
|
231
270
|
...entries,
|
|
@@ -249,31 +288,51 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
249
288
|
vite: {
|
|
250
289
|
enforce: "pre",
|
|
251
290
|
configureServer(server) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
291
|
+
let generation = 0;
|
|
292
|
+
let debounceTimer = null;
|
|
293
|
+
let writeQueue = Promise.resolve();
|
|
294
|
+
const rescan = async () => {
|
|
295
|
+
const myGeneration = ++generation;
|
|
296
|
+
const namesPromise = scanVarNames(options.input, options.exclude, options.selectors);
|
|
297
|
+
cachedNames = namesPromise;
|
|
298
|
+
const names = await namesPromise;
|
|
299
|
+
if (myGeneration !== generation) return;
|
|
300
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
256
301
|
const dtsPath = getDtsPath(options);
|
|
257
302
|
if (dtsPath) {
|
|
258
|
-
|
|
303
|
+
const content = generateDeclaration(names, options.prefix, options.naming);
|
|
304
|
+
writeQueue = writeQueue.then(async () => {
|
|
305
|
+
if (myGeneration !== generation) return;
|
|
306
|
+
await (0, import_promises2.writeFile)(dtsPath, content, "utf8");
|
|
307
|
+
}).catch(console.error);
|
|
308
|
+
await writeQueue;
|
|
259
309
|
}
|
|
310
|
+
if (myGeneration !== generation) return;
|
|
260
311
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
261
312
|
if (mod) {
|
|
262
313
|
server.moduleGraph.invalidateModule(mod);
|
|
263
314
|
server.ws.send({ type: "full-reload" });
|
|
264
315
|
}
|
|
265
316
|
};
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
317
|
+
const handle = (file) => {
|
|
318
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
319
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
320
|
+
debounceTimer = setTimeout(() => {
|
|
321
|
+
debounceTimer = null;
|
|
322
|
+
rescan().catch(console.error);
|
|
323
|
+
}, 100);
|
|
324
|
+
};
|
|
325
|
+
server.watcher.on("change", handle);
|
|
326
|
+
server.watcher.on("add", handle);
|
|
327
|
+
server.watcher.on("unlink", handle);
|
|
270
328
|
}
|
|
271
329
|
},
|
|
272
330
|
async buildStart() {
|
|
273
331
|
cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
|
|
332
|
+
const names = await cachedNames;
|
|
333
|
+
warnOnCollisions(names, options.prefix, options.naming);
|
|
274
334
|
const dtsPath = getDtsPath(options);
|
|
275
335
|
if (!dtsPath) return;
|
|
276
|
-
const names = await cachedNames;
|
|
277
336
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
278
337
|
},
|
|
279
338
|
resolveId(id) {
|