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/vite.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
- result += char;
55
- if (char === "\\" && i + 1 < text.length) {
56
- result += text[++i];
57
- } else if (char === quote) {
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
- if (char === "\\" && i + 1 < text.length) {
122
- result += " ";
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: camelCase, snake, kebab`);
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.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
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 generateJs(varNames, prefix, naming) {
214
- const entries = varNames.map((name) => {
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
- return ` ${key}: 'var(${name})',`;
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
- const handle = async (file) => {
255
- if (!/\.(css|scss|less)$/i.test(file)) return;
256
- cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
257
- const names = await cachedNames;
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
- await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
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 safeHandle = (file) => handle(file).catch(console.error);
269
- server.watcher.on("change", safeHandle);
270
- server.watcher.on("add", safeHandle);
271
- server.watcher.on("unlink", safeHandle);
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/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-HS4B2QT2.js";
4
- import "./chunk-3UIYKYIL.js";
3
+ } from "./chunk-YJPHNISO.js";
4
+ import "./chunk-NS2BULH3.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;
package/dist/webpack.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
- result += char;
55
- if (char === "\\" && i + 1 < text.length) {
56
- result += text[++i];
57
- } else if (char === quote) {
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
- if (char === "\\" && i + 1 < text.length) {
122
- result += " ";
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: camelCase, snake, kebab`);
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.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
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 generateJs(varNames, prefix, naming) {
214
- const entries = varNames.map((name) => {
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
- return ` ${key}: 'var(${name})',`;
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
- const handle = async (file) => {
255
- if (!/\.(css|scss|less)$/i.test(file)) return;
256
- cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
257
- const names = await cachedNames;
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
- await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
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 safeHandle = (file) => handle(file).catch(console.error);
269
- server.watcher.on("change", safeHandle);
270
- server.watcher.on("add", safeHandle);
271
- server.watcher.on("unlink", safeHandle);
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/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-HS4B2QT2.js";
4
- import "./chunk-3UIYKYIL.js";
3
+ } from "./chunk-YJPHNISO.js";
4
+ import "./chunk-NS2BULH3.js";
5
5
 
6
6
  // src/webpack.ts
7
7
  var webpack_default = unplugin_default.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-typed-vars",
3
- "version": "0.4.14",
3
+ "version": "0.4.16",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",