css-typed-vars 0.4.13 → 0.4.15
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-IBTSCKH3.js → chunk-2QDRH4OL.js} +2 -2
- package/dist/{chunk-OKHK5KHU.js → chunk-AUFD5VOT.js} +30 -10
- package/dist/{chunk-3GTZH4ZM.js → chunk-W4JDUG2N.js} +54 -16
- package/dist/cli.cjs +57 -20
- package/dist/cli.js +7 -5
- package/dist/esbuild.cjs +93 -21
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +54 -17
- package/dist/index.js +2 -2
- package/dist/rollup.cjs +93 -21
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +93 -21
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +93 -21
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +93 -21
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -114,6 +114,30 @@ function extractBlock(text, start) {
|
|
|
114
114
|
}
|
|
115
115
|
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
116
116
|
}
|
|
117
|
+
function maskQuotedContent(text) {
|
|
118
|
+
let result = "";
|
|
119
|
+
let quote = null;
|
|
120
|
+
for (let i = 0; i < text.length; i++) {
|
|
121
|
+
const char = text[i];
|
|
122
|
+
if (quote) {
|
|
123
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
124
|
+
result += " ";
|
|
125
|
+
i++;
|
|
126
|
+
} else if (char === quote) {
|
|
127
|
+
quote = null;
|
|
128
|
+
result += char;
|
|
129
|
+
} else {
|
|
130
|
+
result += " ";
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (char === '"' || char === "'") {
|
|
135
|
+
quote = char;
|
|
136
|
+
}
|
|
137
|
+
result += char;
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
117
141
|
function parseVarNames(css, selectors) {
|
|
118
142
|
const stripped = stripComments(css);
|
|
119
143
|
const names = /* @__PURE__ */ new Set();
|
|
@@ -125,7 +149,7 @@ function parseVarNames(css, selectors) {
|
|
|
125
149
|
while (match = openRegex.exec(stripped)) {
|
|
126
150
|
const start = match.index + match[0].length;
|
|
127
151
|
const block = extractBlock(stripped, start);
|
|
128
|
-
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
152
|
+
for (const prop of maskQuotedContent(block).matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
129
153
|
names.add(prop[0]);
|
|
130
154
|
}
|
|
131
155
|
openRegex.lastIndex = start + block.length + 1;
|
|
@@ -144,7 +168,13 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
144
168
|
const all = /* @__PURE__ */ new Set();
|
|
145
169
|
await Promise.all(
|
|
146
170
|
files.map(async (file) => {
|
|
147
|
-
|
|
171
|
+
let css;
|
|
172
|
+
try {
|
|
173
|
+
css = await (0, import_promises.readFile)(file, "utf8");
|
|
174
|
+
} catch (err) {
|
|
175
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
148
178
|
for (const name of parseVarNames(css, selectors)) {
|
|
149
179
|
all.add(name);
|
|
150
180
|
}
|
|
@@ -154,7 +184,11 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
154
184
|
}
|
|
155
185
|
|
|
156
186
|
// src/generator.ts
|
|
187
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
157
188
|
function convertCase(value, naming) {
|
|
189
|
+
if (!VALID_NAMINGS.includes(naming)) {
|
|
190
|
+
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
191
|
+
}
|
|
158
192
|
if (naming === "kebab") return value;
|
|
159
193
|
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
160
194
|
}
|
|
@@ -165,9 +199,13 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
165
199
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
166
200
|
if (!prefix) return key;
|
|
167
201
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
168
|
-
if (naming
|
|
202
|
+
if (naming === "kebab") {
|
|
203
|
+
normalizedPrefix = normalizedPrefix.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
204
|
+
return `${normalizedPrefix}-${key}`;
|
|
205
|
+
}
|
|
206
|
+
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
207
|
+
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
169
208
|
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
170
|
-
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
171
209
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
172
210
|
}
|
|
173
211
|
function formatKey(key, naming = "camelCase") {
|
|
@@ -187,11 +225,16 @@ function findKeyCollisions(varNames, prefix, naming) {
|
|
|
187
225
|
}
|
|
188
226
|
return byKey;
|
|
189
227
|
}
|
|
190
|
-
function
|
|
191
|
-
const
|
|
228
|
+
function buildEntries(varNames, prefix, naming) {
|
|
229
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
230
|
+
for (const name of varNames) {
|
|
192
231
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
193
|
-
|
|
194
|
-
}
|
|
232
|
+
byKey.set(key, name);
|
|
233
|
+
}
|
|
234
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
235
|
+
}
|
|
236
|
+
function generateCode(varNames, prefix, naming) {
|
|
237
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
195
238
|
return [
|
|
196
239
|
"// generated \u2014 do not edit",
|
|
197
240
|
"export const cssVars = {",
|
|
@@ -203,10 +246,7 @@ function generateCode(varNames, prefix, naming) {
|
|
|
203
246
|
].join("\n");
|
|
204
247
|
}
|
|
205
248
|
function generateJs(varNames, prefix, naming) {
|
|
206
|
-
const entries = varNames.map((name) => {
|
|
207
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
208
|
-
return ` ${key}: 'var(${name})',`;
|
|
209
|
-
});
|
|
249
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
210
250
|
return [
|
|
211
251
|
"// generated \u2014 do not edit",
|
|
212
252
|
"export const cssVars = {",
|
|
@@ -216,10 +256,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
216
256
|
].join("\n");
|
|
217
257
|
}
|
|
218
258
|
function generateDeclaration(varNames, prefix, naming) {
|
|
219
|
-
const entries = varNames.map((name) => {
|
|
220
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
221
|
-
return ` ${key}: 'var(${name})';`;
|
|
222
|
-
});
|
|
259
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
223
260
|
return [
|
|
224
261
|
"export declare const cssVars: {",
|
|
225
262
|
...entries,
|
|
@@ -240,7 +277,7 @@ async function generate(options) {
|
|
|
240
277
|
}
|
|
241
278
|
const outPath = (0, import_node_path.resolve)(options.output);
|
|
242
279
|
await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
|
|
243
|
-
const jsExtMatch = /\.(m|c)?js
|
|
280
|
+
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
|
244
281
|
if (jsExtMatch) {
|
|
245
282
|
await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
246
283
|
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
generate
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-2QDRH4OL.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-W4JDUG2N.js";
|
|
11
11
|
export {
|
|
12
12
|
generate,
|
|
13
13
|
generateCode,
|
package/dist/rollup.cjs
CHANGED
|
@@ -112,6 +112,30 @@ function extractBlock(text, start) {
|
|
|
112
112
|
}
|
|
113
113
|
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
114
114
|
}
|
|
115
|
+
function maskQuotedContent(text) {
|
|
116
|
+
let result = "";
|
|
117
|
+
let quote = null;
|
|
118
|
+
for (let i = 0; i < text.length; i++) {
|
|
119
|
+
const char = text[i];
|
|
120
|
+
if (quote) {
|
|
121
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
122
|
+
result += " ";
|
|
123
|
+
i++;
|
|
124
|
+
} else if (char === quote) {
|
|
125
|
+
quote = null;
|
|
126
|
+
result += char;
|
|
127
|
+
} else {
|
|
128
|
+
result += " ";
|
|
129
|
+
}
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (char === '"' || char === "'") {
|
|
133
|
+
quote = char;
|
|
134
|
+
}
|
|
135
|
+
result += char;
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
115
139
|
function parseVarNames(css, selectors) {
|
|
116
140
|
const stripped = stripComments(css);
|
|
117
141
|
const names = /* @__PURE__ */ new Set();
|
|
@@ -123,7 +147,7 @@ function parseVarNames(css, selectors) {
|
|
|
123
147
|
while (match = openRegex.exec(stripped)) {
|
|
124
148
|
const start = match.index + match[0].length;
|
|
125
149
|
const block = extractBlock(stripped, start);
|
|
126
|
-
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
150
|
+
for (const prop of maskQuotedContent(block).matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
127
151
|
names.add(prop[0]);
|
|
128
152
|
}
|
|
129
153
|
openRegex.lastIndex = start + block.length + 1;
|
|
@@ -142,7 +166,13 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
142
166
|
const all = /* @__PURE__ */ new Set();
|
|
143
167
|
await Promise.all(
|
|
144
168
|
files.map(async (file) => {
|
|
145
|
-
|
|
169
|
+
let css;
|
|
170
|
+
try {
|
|
171
|
+
css = await (0, import_promises.readFile)(file, "utf8");
|
|
172
|
+
} catch (err) {
|
|
173
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
146
176
|
for (const name of parseVarNames(css, selectors)) {
|
|
147
177
|
all.add(name);
|
|
148
178
|
}
|
|
@@ -152,7 +182,11 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
152
182
|
}
|
|
153
183
|
|
|
154
184
|
// src/generator.ts
|
|
185
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
155
186
|
function convertCase(value, naming) {
|
|
187
|
+
if (!VALID_NAMINGS.includes(naming)) {
|
|
188
|
+
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
189
|
+
}
|
|
156
190
|
if (naming === "kebab") return value;
|
|
157
191
|
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
158
192
|
}
|
|
@@ -163,20 +197,42 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
163
197
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
164
198
|
if (!prefix) return key;
|
|
165
199
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
166
|
-
if (naming
|
|
200
|
+
if (naming === "kebab") {
|
|
201
|
+
normalizedPrefix = normalizedPrefix.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
202
|
+
return `${normalizedPrefix}-${key}`;
|
|
203
|
+
}
|
|
204
|
+
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
205
|
+
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
167
206
|
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
168
|
-
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
169
207
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
170
208
|
}
|
|
171
209
|
function formatKey(key, naming = "camelCase") {
|
|
172
210
|
if (naming === "kebab") return `'${key}'`;
|
|
173
211
|
return key;
|
|
174
212
|
}
|
|
175
|
-
function
|
|
176
|
-
const
|
|
213
|
+
function findKeyCollisions(varNames, prefix, naming) {
|
|
214
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
215
|
+
for (const name of varNames) {
|
|
216
|
+
const key = applyPrefix(toKey(name, naming), prefix, naming);
|
|
217
|
+
const existing = byKey.get(key);
|
|
218
|
+
if (existing) existing.push(name);
|
|
219
|
+
else byKey.set(key, [name]);
|
|
220
|
+
}
|
|
221
|
+
for (const [key, names] of byKey) {
|
|
222
|
+
if (names.length < 2) byKey.delete(key);
|
|
223
|
+
}
|
|
224
|
+
return byKey;
|
|
225
|
+
}
|
|
226
|
+
function buildEntries(varNames, prefix, naming) {
|
|
227
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
228
|
+
for (const name of varNames) {
|
|
177
229
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
178
|
-
|
|
179
|
-
}
|
|
230
|
+
byKey.set(key, name);
|
|
231
|
+
}
|
|
232
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
233
|
+
}
|
|
234
|
+
function generateJs(varNames, prefix, naming) {
|
|
235
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
180
236
|
return [
|
|
181
237
|
"// generated \u2014 do not edit",
|
|
182
238
|
"export const cssVars = {",
|
|
@@ -186,10 +242,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
186
242
|
].join("\n");
|
|
187
243
|
}
|
|
188
244
|
function generateDeclaration(varNames, prefix, naming) {
|
|
189
|
-
const entries = varNames.map((name) => {
|
|
190
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
191
|
-
return ` ${key}: 'var(${name})';`;
|
|
192
|
-
});
|
|
245
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
193
246
|
return [
|
|
194
247
|
"export declare const cssVars: {",
|
|
195
248
|
...entries,
|
|
@@ -202,6 +255,11 @@ function generateDeclaration(varNames, prefix, naming) {
|
|
|
202
255
|
// src/unplugin.ts
|
|
203
256
|
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
204
257
|
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
258
|
+
function warnOnCollisions(names, options) {
|
|
259
|
+
for (const [key, vars] of findKeyCollisions(names, options.prefix, options.naming)) {
|
|
260
|
+
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
205
263
|
function getDtsPath(options) {
|
|
206
264
|
if (options.dts === false) return null;
|
|
207
265
|
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
@@ -213,31 +271,45 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
213
271
|
vite: {
|
|
214
272
|
enforce: "pre",
|
|
215
273
|
configureServer(server) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const
|
|
274
|
+
let generation = 0;
|
|
275
|
+
let debounceTimer = null;
|
|
276
|
+
const rescan = async () => {
|
|
277
|
+
const myGeneration = ++generation;
|
|
278
|
+
const namesPromise = scanVarNames(options.input, options.exclude, options.selectors);
|
|
279
|
+
cachedNames = namesPromise;
|
|
280
|
+
const names = await namesPromise;
|
|
281
|
+
if (myGeneration !== generation) return;
|
|
282
|
+
warnOnCollisions(names, options);
|
|
220
283
|
const dtsPath = getDtsPath(options);
|
|
221
284
|
if (dtsPath) {
|
|
222
285
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
223
286
|
}
|
|
287
|
+
if (myGeneration !== generation) return;
|
|
224
288
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
225
289
|
if (mod) {
|
|
226
290
|
server.moduleGraph.invalidateModule(mod);
|
|
227
291
|
server.ws.send({ type: "full-reload" });
|
|
228
292
|
}
|
|
229
293
|
};
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
294
|
+
const handle = (file) => {
|
|
295
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
296
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
297
|
+
debounceTimer = setTimeout(() => {
|
|
298
|
+
debounceTimer = null;
|
|
299
|
+
rescan().catch(console.error);
|
|
300
|
+
}, 100);
|
|
301
|
+
};
|
|
302
|
+
server.watcher.on("change", handle);
|
|
303
|
+
server.watcher.on("add", handle);
|
|
304
|
+
server.watcher.on("unlink", handle);
|
|
234
305
|
}
|
|
235
306
|
},
|
|
236
307
|
async buildStart() {
|
|
237
308
|
cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
|
|
309
|
+
const names = await cachedNames;
|
|
310
|
+
warnOnCollisions(names, options);
|
|
238
311
|
const dtsPath = getDtsPath(options);
|
|
239
312
|
if (!dtsPath) return;
|
|
240
|
-
const names = await cachedNames;
|
|
241
313
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
242
314
|
},
|
|
243
315
|
resolveId(id) {
|
package/dist/rollup.js
CHANGED
package/dist/unplugin.cjs
CHANGED
|
@@ -110,6 +110,30 @@ function extractBlock(text, start) {
|
|
|
110
110
|
}
|
|
111
111
|
return text.slice(start, depth === 0 ? i - 1 : i);
|
|
112
112
|
}
|
|
113
|
+
function maskQuotedContent(text) {
|
|
114
|
+
let result = "";
|
|
115
|
+
let quote = null;
|
|
116
|
+
for (let i = 0; i < text.length; i++) {
|
|
117
|
+
const char = text[i];
|
|
118
|
+
if (quote) {
|
|
119
|
+
if (char === "\\" && i + 1 < text.length) {
|
|
120
|
+
result += " ";
|
|
121
|
+
i++;
|
|
122
|
+
} else if (char === quote) {
|
|
123
|
+
quote = null;
|
|
124
|
+
result += char;
|
|
125
|
+
} else {
|
|
126
|
+
result += " ";
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (char === '"' || char === "'") {
|
|
131
|
+
quote = char;
|
|
132
|
+
}
|
|
133
|
+
result += char;
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
113
137
|
function parseVarNames(css, selectors) {
|
|
114
138
|
const stripped = stripComments(css);
|
|
115
139
|
const names = /* @__PURE__ */ new Set();
|
|
@@ -121,7 +145,7 @@ function parseVarNames(css, selectors) {
|
|
|
121
145
|
while (match = openRegex.exec(stripped)) {
|
|
122
146
|
const start = match.index + match[0].length;
|
|
123
147
|
const block = extractBlock(stripped, start);
|
|
124
|
-
for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
148
|
+
for (const prop of maskQuotedContent(block).matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
125
149
|
names.add(prop[0]);
|
|
126
150
|
}
|
|
127
151
|
openRegex.lastIndex = start + block.length + 1;
|
|
@@ -140,7 +164,13 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
140
164
|
const all = /* @__PURE__ */ new Set();
|
|
141
165
|
await Promise.all(
|
|
142
166
|
files.map(async (file) => {
|
|
143
|
-
|
|
167
|
+
let css;
|
|
168
|
+
try {
|
|
169
|
+
css = await (0, import_promises.readFile)(file, "utf8");
|
|
170
|
+
} catch (err) {
|
|
171
|
+
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
144
174
|
for (const name of parseVarNames(css, selectors)) {
|
|
145
175
|
all.add(name);
|
|
146
176
|
}
|
|
@@ -150,7 +180,11 @@ async function scanVarNames(patterns, exclude, selectors) {
|
|
|
150
180
|
}
|
|
151
181
|
|
|
152
182
|
// src/generator.ts
|
|
183
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
153
184
|
function convertCase(value, naming) {
|
|
185
|
+
if (!VALID_NAMINGS.includes(naming)) {
|
|
186
|
+
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
187
|
+
}
|
|
154
188
|
if (naming === "kebab") return value;
|
|
155
189
|
return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
156
190
|
}
|
|
@@ -161,20 +195,42 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
161
195
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
162
196
|
if (!prefix) return key;
|
|
163
197
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
164
|
-
if (naming
|
|
198
|
+
if (naming === "kebab") {
|
|
199
|
+
normalizedPrefix = normalizedPrefix.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
200
|
+
return `${normalizedPrefix}-${key}`;
|
|
201
|
+
}
|
|
202
|
+
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
203
|
+
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
165
204
|
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
166
|
-
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
167
205
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
168
206
|
}
|
|
169
207
|
function formatKey(key, naming = "camelCase") {
|
|
170
208
|
if (naming === "kebab") return `'${key}'`;
|
|
171
209
|
return key;
|
|
172
210
|
}
|
|
173
|
-
function
|
|
174
|
-
const
|
|
211
|
+
function findKeyCollisions(varNames, prefix, naming) {
|
|
212
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
213
|
+
for (const name of varNames) {
|
|
214
|
+
const key = applyPrefix(toKey(name, naming), prefix, naming);
|
|
215
|
+
const existing = byKey.get(key);
|
|
216
|
+
if (existing) existing.push(name);
|
|
217
|
+
else byKey.set(key, [name]);
|
|
218
|
+
}
|
|
219
|
+
for (const [key, names] of byKey) {
|
|
220
|
+
if (names.length < 2) byKey.delete(key);
|
|
221
|
+
}
|
|
222
|
+
return byKey;
|
|
223
|
+
}
|
|
224
|
+
function buildEntries(varNames, prefix, naming) {
|
|
225
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
226
|
+
for (const name of varNames) {
|
|
175
227
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
176
|
-
|
|
177
|
-
}
|
|
228
|
+
byKey.set(key, name);
|
|
229
|
+
}
|
|
230
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
231
|
+
}
|
|
232
|
+
function generateJs(varNames, prefix, naming) {
|
|
233
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
178
234
|
return [
|
|
179
235
|
"// generated \u2014 do not edit",
|
|
180
236
|
"export const cssVars = {",
|
|
@@ -184,10 +240,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
184
240
|
].join("\n");
|
|
185
241
|
}
|
|
186
242
|
function generateDeclaration(varNames, prefix, naming) {
|
|
187
|
-
const entries = varNames.map((name) => {
|
|
188
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
189
|
-
return ` ${key}: 'var(${name})';`;
|
|
190
|
-
});
|
|
243
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
191
244
|
return [
|
|
192
245
|
"export declare const cssVars: {",
|
|
193
246
|
...entries,
|
|
@@ -200,6 +253,11 @@ function generateDeclaration(varNames, prefix, naming) {
|
|
|
200
253
|
// src/unplugin.ts
|
|
201
254
|
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
202
255
|
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
256
|
+
function warnOnCollisions(names, options) {
|
|
257
|
+
for (const [key, vars] of findKeyCollisions(names, options.prefix, options.naming)) {
|
|
258
|
+
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
203
261
|
function getDtsPath(options) {
|
|
204
262
|
if (options.dts === false) return null;
|
|
205
263
|
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
@@ -211,31 +269,45 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
211
269
|
vite: {
|
|
212
270
|
enforce: "pre",
|
|
213
271
|
configureServer(server) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
272
|
+
let generation = 0;
|
|
273
|
+
let debounceTimer = null;
|
|
274
|
+
const rescan = async () => {
|
|
275
|
+
const myGeneration = ++generation;
|
|
276
|
+
const namesPromise = scanVarNames(options.input, options.exclude, options.selectors);
|
|
277
|
+
cachedNames = namesPromise;
|
|
278
|
+
const names = await namesPromise;
|
|
279
|
+
if (myGeneration !== generation) return;
|
|
280
|
+
warnOnCollisions(names, options);
|
|
218
281
|
const dtsPath = getDtsPath(options);
|
|
219
282
|
if (dtsPath) {
|
|
220
283
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
221
284
|
}
|
|
285
|
+
if (myGeneration !== generation) return;
|
|
222
286
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
223
287
|
if (mod) {
|
|
224
288
|
server.moduleGraph.invalidateModule(mod);
|
|
225
289
|
server.ws.send({ type: "full-reload" });
|
|
226
290
|
}
|
|
227
291
|
};
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
292
|
+
const handle = (file) => {
|
|
293
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
294
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
295
|
+
debounceTimer = setTimeout(() => {
|
|
296
|
+
debounceTimer = null;
|
|
297
|
+
rescan().catch(console.error);
|
|
298
|
+
}, 100);
|
|
299
|
+
};
|
|
300
|
+
server.watcher.on("change", handle);
|
|
301
|
+
server.watcher.on("add", handle);
|
|
302
|
+
server.watcher.on("unlink", handle);
|
|
232
303
|
}
|
|
233
304
|
},
|
|
234
305
|
async buildStart() {
|
|
235
306
|
cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
|
|
307
|
+
const names = await cachedNames;
|
|
308
|
+
warnOnCollisions(names, options);
|
|
236
309
|
const dtsPath = getDtsPath(options);
|
|
237
310
|
if (!dtsPath) return;
|
|
238
|
-
const names = await cachedNames;
|
|
239
311
|
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
240
312
|
},
|
|
241
313
|
resolveId(id) {
|