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/vite.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/vite.js
CHANGED
package/dist/webpack.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/webpack.js
CHANGED