css-typed-vars 0.4.12 → 0.4.14

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.
@@ -4,7 +4,7 @@ import {
4
4
  generateDeclaration,
5
5
  generateJs,
6
6
  scanVarNames
7
- } from "./chunk-2GVOUCFD.js";
7
+ } from "./chunk-3UIYKYIL.js";
8
8
 
9
9
  // src/index.ts
10
10
  import { writeFile, mkdir } from "fs/promises";
@@ -19,7 +19,7 @@ async function generate(options) {
19
19
  }
20
20
  const outPath = resolve(options.output);
21
21
  await mkdir(dirname(outPath), { recursive: true });
22
- const jsExtMatch = /\.(m|c)?js$/.exec(options.output);
22
+ const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
23
23
  if (jsExtMatch) {
24
24
  await writeFile(outPath, generateJs(names, options.prefix, options.naming), "utf8");
25
25
  const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
@@ -21,14 +21,14 @@ function stripComments(text) {
21
21
  }
22
22
  continue;
23
23
  }
24
- if (urlDepth > 0) {
25
- if (char === "(") urlDepth++;
26
- else if (char === ")") urlDepth--;
24
+ if (char === '"' || char === "'") {
25
+ quote = char;
27
26
  result += char;
28
27
  continue;
29
28
  }
30
- if (char === '"' || char === "'") {
31
- quote = char;
29
+ if (urlDepth > 0) {
30
+ if (char === "(") urlDepth++;
31
+ else if (char === ")") urlDepth--;
32
32
  result += char;
33
33
  continue;
34
34
  }
@@ -74,6 +74,30 @@ function extractBlock(text, start) {
74
74
  }
75
75
  return text.slice(start, depth === 0 ? i - 1 : i);
76
76
  }
77
+ function maskQuotedContent(text) {
78
+ let result = "";
79
+ let quote = null;
80
+ for (let i = 0; i < text.length; i++) {
81
+ const char = text[i];
82
+ if (quote) {
83
+ if (char === "\\" && i + 1 < text.length) {
84
+ result += " ";
85
+ i++;
86
+ } else if (char === quote) {
87
+ quote = null;
88
+ result += char;
89
+ } else {
90
+ result += " ";
91
+ }
92
+ continue;
93
+ }
94
+ if (char === '"' || char === "'") {
95
+ quote = char;
96
+ }
97
+ result += char;
98
+ }
99
+ return result;
100
+ }
77
101
  function parseVarNames(css, selectors) {
78
102
  const stripped = stripComments(css);
79
103
  const names = /* @__PURE__ */ new Set();
@@ -85,7 +109,7 @@ function parseVarNames(css, selectors) {
85
109
  while (match = openRegex.exec(stripped)) {
86
110
  const start = match.index + match[0].length;
87
111
  const block = extractBlock(stripped, start);
88
- for (const prop of block.matchAll(/--[\w-]+(?=\s*:)/g)) {
112
+ for (const prop of maskQuotedContent(block).matchAll(/--[\w-]+(?=\s*:)/g)) {
89
113
  names.add(prop[0]);
90
114
  }
91
115
  openRegex.lastIndex = start + block.length + 1;
@@ -106,7 +130,13 @@ async function scanVarNames(patterns, exclude, selectors) {
106
130
  const all = /* @__PURE__ */ new Set();
107
131
  await Promise.all(
108
132
  files.map(async (file) => {
109
- const css = await readFile(file, "utf8");
133
+ let css;
134
+ try {
135
+ css = await readFile(file, "utf8");
136
+ } catch (err) {
137
+ console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
138
+ return;
139
+ }
110
140
  for (const name of parseVarNames(css, selectors)) {
111
141
  all.add(name);
112
142
  }
@@ -116,7 +146,11 @@ async function scanVarNames(patterns, exclude, selectors) {
116
146
  }
117
147
 
118
148
  // src/generator.ts
149
+ var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
119
150
  function convertCase(value, naming) {
151
+ if (!VALID_NAMINGS.includes(naming)) {
152
+ throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: camelCase, snake, kebab`);
153
+ }
120
154
  if (naming === "kebab") return value;
121
155
  return naming === "snake" ? value.replace(/-+/g, "_") : value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
122
156
  }
@@ -126,9 +160,14 @@ function toKey(cssVarName, naming = "camelCase") {
126
160
  }
127
161
  function applyPrefix(key, prefix, naming = "camelCase") {
128
162
  if (!prefix) return key;
129
- const normalizedPrefix = convertCase(prefix, naming);
163
+ let normalizedPrefix = convertCase(prefix, naming);
164
+ if (naming === "kebab") {
165
+ normalizedPrefix = normalizedPrefix.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
166
+ return `${normalizedPrefix}-${key}`;
167
+ }
168
+ normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
169
+ if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
130
170
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
131
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
132
171
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
133
172
  }
134
173
  function formatKey(key, naming = "camelCase") {
@@ -3,7 +3,7 @@ import {
3
3
  generateDeclaration,
4
4
  generateJs,
5
5
  scanVarNames
6
- } from "./chunk-2GVOUCFD.js";
6
+ } from "./chunk-3UIYKYIL.js";
7
7
 
8
8
  // src/unplugin.ts
9
9
  import { createUnplugin } from "unplugin";
@@ -36,9 +36,10 @@ var unplugin_default = createUnplugin((options) => {
36
36
  server.ws.send({ type: "full-reload" });
37
37
  }
38
38
  };
39
- server.watcher.on("change", handle);
40
- server.watcher.on("add", handle);
41
- server.watcher.on("unlink", handle);
39
+ const safeHandle = (file) => handle(file).catch(console.error);
40
+ server.watcher.on("change", safeHandle);
41
+ server.watcher.on("add", safeHandle);
42
+ server.watcher.on("unlink", safeHandle);
42
43
  }
43
44
  },
44
45
  async buildStart() {
package/dist/cli.cjs CHANGED
@@ -57,14 +57,14 @@ function stripComments(text) {
57
57
  }
58
58
  continue;
59
59
  }
60
- if (urlDepth > 0) {
61
- if (char === "(") urlDepth++;
62
- else if (char === ")") urlDepth--;
60
+ if (char === '"' || char === "'") {
61
+ quote = char;
63
62
  result += char;
64
63
  continue;
65
64
  }
66
- if (char === '"' || char === "'") {
67
- quote = char;
65
+ if (urlDepth > 0) {
66
+ if (char === "(") urlDepth++;
67
+ else if (char === ")") urlDepth--;
68
68
  result += char;
69
69
  continue;
70
70
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -160,9 +194,14 @@ function toKey(cssVarName, naming = "camelCase") {
160
194
  }
161
195
  function applyPrefix(key, prefix, naming = "camelCase") {
162
196
  if (!prefix) return key;
163
- const normalizedPrefix = convertCase(prefix, naming);
197
+ let normalizedPrefix = convertCase(prefix, 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}`;
164
204
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
165
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
166
205
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
167
206
  }
168
207
  function formatKey(key, naming = "camelCase") {
@@ -235,7 +274,7 @@ async function generate(options) {
235
274
  }
236
275
  const outPath = (0, import_node_path.resolve)(options.output);
237
276
  await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
238
- const jsExtMatch = /\.(m|c)?js$/.exec(options.output);
277
+ const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
239
278
  if (jsExtMatch) {
240
279
  await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
241
280
  const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
@@ -276,7 +315,7 @@ async function loadConfig() {
276
315
  return JSON.parse(content);
277
316
  } else {
278
317
  const mod = await import((0, import_node_url.pathToFileURL)(path).href);
279
- return mod.default;
318
+ return mod.default ?? {};
280
319
  }
281
320
  } catch (err) {
282
321
  const code = err.code;
@@ -328,4 +367,7 @@ async function main() {
328
367
  console.log("Watching for changes...");
329
368
  }
330
369
  }
331
- main().catch(console.error);
370
+ main().catch((err) => {
371
+ console.error(err);
372
+ process.exit(1);
373
+ });
package/dist/cli.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  generate
4
- } from "./chunk-KETRI7BY.js";
5
- import "./chunk-2GVOUCFD.js";
4
+ } from "./chunk-2JDQ2DY7.js";
5
+ import "./chunk-3UIYKYIL.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import { readFile } from "fs/promises";
@@ -39,7 +39,7 @@ async function loadConfig() {
39
39
  return JSON.parse(content);
40
40
  } else {
41
41
  const mod = await import(pathToFileURL(path).href);
42
- return mod.default;
42
+ return mod.default ?? {};
43
43
  }
44
44
  } catch (err) {
45
45
  const code = err.code;
@@ -91,4 +91,7 @@ async function main() {
91
91
  console.log("Watching for changes...");
92
92
  }
93
93
  }
94
- main().catch(console.error);
94
+ main().catch((err) => {
95
+ console.error(err);
96
+ process.exit(1);
97
+ });
package/dist/esbuild.cjs CHANGED
@@ -59,14 +59,14 @@ function stripComments(text) {
59
59
  }
60
60
  continue;
61
61
  }
62
- if (urlDepth > 0) {
63
- if (char === "(") urlDepth++;
64
- else if (char === ")") urlDepth--;
62
+ if (char === '"' || char === "'") {
63
+ quote = char;
65
64
  result += char;
66
65
  continue;
67
66
  }
68
- if (char === '"' || char === "'") {
69
- quote = char;
67
+ if (urlDepth > 0) {
68
+ if (char === "(") urlDepth++;
69
+ else if (char === ")") urlDepth--;
70
70
  result += char;
71
71
  continue;
72
72
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -162,9 +196,14 @@ function toKey(cssVarName, naming = "camelCase") {
162
196
  }
163
197
  function applyPrefix(key, prefix, naming = "camelCase") {
164
198
  if (!prefix) return key;
165
- const normalizedPrefix = convertCase(prefix, naming);
199
+ let normalizedPrefix = convertCase(prefix, 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}`;
166
206
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
167
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
168
207
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
169
208
  }
170
209
  function formatKey(key, naming = "camelCase") {
@@ -226,9 +265,10 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
226
265
  server.ws.send({ type: "full-reload" });
227
266
  }
228
267
  };
229
- server.watcher.on("change", handle);
230
- server.watcher.on("add", handle);
231
- server.watcher.on("unlink", handle);
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);
232
272
  }
233
273
  },
234
274
  async buildStart() {
package/dist/esbuild.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-DWNF2MKL.js";
4
- import "./chunk-2GVOUCFD.js";
3
+ } from "./chunk-HS4B2QT2.js";
4
+ import "./chunk-3UIYKYIL.js";
5
5
 
6
6
  // src/esbuild.ts
7
7
  var esbuild_default = unplugin_default.esbuild;
package/dist/index.cjs CHANGED
@@ -61,14 +61,14 @@ function stripComments(text) {
61
61
  }
62
62
  continue;
63
63
  }
64
- if (urlDepth > 0) {
65
- if (char === "(") urlDepth++;
66
- else if (char === ")") urlDepth--;
64
+ if (char === '"' || char === "'") {
65
+ quote = char;
67
66
  result += char;
68
67
  continue;
69
68
  }
70
- if (char === '"' || char === "'") {
71
- quote = char;
69
+ if (urlDepth > 0) {
70
+ if (char === "(") urlDepth++;
71
+ else if (char === ")") urlDepth--;
72
72
  result += char;
73
73
  continue;
74
74
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -164,9 +198,14 @@ function toKey(cssVarName, naming = "camelCase") {
164
198
  }
165
199
  function applyPrefix(key, prefix, naming = "camelCase") {
166
200
  if (!prefix) return key;
167
- const normalizedPrefix = convertCase(prefix, naming);
201
+ let normalizedPrefix = convertCase(prefix, 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}`;
168
208
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
169
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
170
209
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
171
210
  }
172
211
  function formatKey(key, naming = "camelCase") {
@@ -239,7 +278,7 @@ async function generate(options) {
239
278
  }
240
279
  const outPath = (0, import_node_path.resolve)(options.output);
241
280
  await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
242
- const jsExtMatch = /\.(m|c)?js$/.exec(options.output);
281
+ const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
243
282
  if (jsExtMatch) {
244
283
  await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
245
284
  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-KETRI7BY.js";
3
+ } from "./chunk-2JDQ2DY7.js";
4
4
  import {
5
5
  generateCode,
6
6
  generateDeclaration,
7
7
  generateJs,
8
8
  parseVarNames,
9
9
  scanVarNames
10
- } from "./chunk-2GVOUCFD.js";
10
+ } from "./chunk-3UIYKYIL.js";
11
11
  export {
12
12
  generate,
13
13
  generateCode,
package/dist/rollup.cjs CHANGED
@@ -59,14 +59,14 @@ function stripComments(text) {
59
59
  }
60
60
  continue;
61
61
  }
62
- if (urlDepth > 0) {
63
- if (char === "(") urlDepth++;
64
- else if (char === ")") urlDepth--;
62
+ if (char === '"' || char === "'") {
63
+ quote = char;
65
64
  result += char;
66
65
  continue;
67
66
  }
68
- if (char === '"' || char === "'") {
69
- quote = char;
67
+ if (urlDepth > 0) {
68
+ if (char === "(") urlDepth++;
69
+ else if (char === ")") urlDepth--;
70
70
  result += char;
71
71
  continue;
72
72
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -162,9 +196,14 @@ function toKey(cssVarName, naming = "camelCase") {
162
196
  }
163
197
  function applyPrefix(key, prefix, naming = "camelCase") {
164
198
  if (!prefix) return key;
165
- const normalizedPrefix = convertCase(prefix, naming);
199
+ let normalizedPrefix = convertCase(prefix, 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}`;
166
206
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
167
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
168
207
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
169
208
  }
170
209
  function formatKey(key, naming = "camelCase") {
@@ -226,9 +265,10 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
226
265
  server.ws.send({ type: "full-reload" });
227
266
  }
228
267
  };
229
- server.watcher.on("change", handle);
230
- server.watcher.on("add", handle);
231
- server.watcher.on("unlink", handle);
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);
232
272
  }
233
273
  },
234
274
  async buildStart() {
package/dist/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-DWNF2MKL.js";
4
- import "./chunk-2GVOUCFD.js";
3
+ } from "./chunk-HS4B2QT2.js";
4
+ import "./chunk-3UIYKYIL.js";
5
5
 
6
6
  // src/rollup.ts
7
7
  var rollup_default = unplugin_default.rollup;
package/dist/unplugin.cjs CHANGED
@@ -57,14 +57,14 @@ function stripComments(text) {
57
57
  }
58
58
  continue;
59
59
  }
60
- if (urlDepth > 0) {
61
- if (char === "(") urlDepth++;
62
- else if (char === ")") urlDepth--;
60
+ if (char === '"' || char === "'") {
61
+ quote = char;
63
62
  result += char;
64
63
  continue;
65
64
  }
66
- if (char === '"' || char === "'") {
67
- quote = char;
65
+ if (urlDepth > 0) {
66
+ if (char === "(") urlDepth++;
67
+ else if (char === ")") urlDepth--;
68
68
  result += char;
69
69
  continue;
70
70
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -160,9 +194,14 @@ function toKey(cssVarName, naming = "camelCase") {
160
194
  }
161
195
  function applyPrefix(key, prefix, naming = "camelCase") {
162
196
  if (!prefix) return key;
163
- const normalizedPrefix = convertCase(prefix, naming);
197
+ let normalizedPrefix = convertCase(prefix, 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}`;
164
204
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
165
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
166
205
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
167
206
  }
168
207
  function formatKey(key, naming = "camelCase") {
@@ -224,9 +263,10 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
224
263
  server.ws.send({ type: "full-reload" });
225
264
  }
226
265
  };
227
- server.watcher.on("change", handle);
228
- server.watcher.on("add", handle);
229
- server.watcher.on("unlink", handle);
266
+ const safeHandle = (file) => handle(file).catch(console.error);
267
+ server.watcher.on("change", safeHandle);
268
+ server.watcher.on("add", safeHandle);
269
+ server.watcher.on("unlink", safeHandle);
230
270
  }
231
271
  },
232
272
  async buildStart() {
package/dist/unplugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-DWNF2MKL.js";
4
- import "./chunk-2GVOUCFD.js";
3
+ } from "./chunk-HS4B2QT2.js";
4
+ import "./chunk-3UIYKYIL.js";
5
5
  export {
6
6
  unplugin_default as default
7
7
  };
package/dist/vite.cjs CHANGED
@@ -59,14 +59,14 @@ function stripComments(text) {
59
59
  }
60
60
  continue;
61
61
  }
62
- if (urlDepth > 0) {
63
- if (char === "(") urlDepth++;
64
- else if (char === ")") urlDepth--;
62
+ if (char === '"' || char === "'") {
63
+ quote = char;
65
64
  result += char;
66
65
  continue;
67
66
  }
68
- if (char === '"' || char === "'") {
69
- quote = char;
67
+ if (urlDepth > 0) {
68
+ if (char === "(") urlDepth++;
69
+ else if (char === ")") urlDepth--;
70
70
  result += char;
71
71
  continue;
72
72
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -162,9 +196,14 @@ function toKey(cssVarName, naming = "camelCase") {
162
196
  }
163
197
  function applyPrefix(key, prefix, naming = "camelCase") {
164
198
  if (!prefix) return key;
165
- const normalizedPrefix = convertCase(prefix, naming);
199
+ let normalizedPrefix = convertCase(prefix, 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}`;
166
206
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
167
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
168
207
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
169
208
  }
170
209
  function formatKey(key, naming = "camelCase") {
@@ -226,9 +265,10 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
226
265
  server.ws.send({ type: "full-reload" });
227
266
  }
228
267
  };
229
- server.watcher.on("change", handle);
230
- server.watcher.on("add", handle);
231
- server.watcher.on("unlink", handle);
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);
232
272
  }
233
273
  },
234
274
  async buildStart() {
package/dist/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-DWNF2MKL.js";
4
- import "./chunk-2GVOUCFD.js";
3
+ } from "./chunk-HS4B2QT2.js";
4
+ import "./chunk-3UIYKYIL.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;
package/dist/webpack.cjs CHANGED
@@ -59,14 +59,14 @@ function stripComments(text) {
59
59
  }
60
60
  continue;
61
61
  }
62
- if (urlDepth > 0) {
63
- if (char === "(") urlDepth++;
64
- else if (char === ")") urlDepth--;
62
+ if (char === '"' || char === "'") {
63
+ quote = char;
65
64
  result += char;
66
65
  continue;
67
66
  }
68
- if (char === '"' || char === "'") {
69
- quote = char;
67
+ if (urlDepth > 0) {
68
+ if (char === "(") urlDepth++;
69
+ else if (char === ")") urlDepth--;
70
70
  result += char;
71
71
  continue;
72
72
  }
@@ -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
- const css = await (0, import_promises.readFile)(file, "utf8");
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: camelCase, snake, kebab`);
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
  }
@@ -162,9 +196,14 @@ function toKey(cssVarName, naming = "camelCase") {
162
196
  }
163
197
  function applyPrefix(key, prefix, naming = "camelCase") {
164
198
  if (!prefix) return key;
165
- const normalizedPrefix = convertCase(prefix, naming);
199
+ let normalizedPrefix = convertCase(prefix, 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}`;
166
206
  if (naming === "snake") return `${normalizedPrefix}_${key}`;
167
- if (naming === "kebab") return `${normalizedPrefix}-${key}`;
168
207
  return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
169
208
  }
170
209
  function formatKey(key, naming = "camelCase") {
@@ -226,9 +265,10 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
226
265
  server.ws.send({ type: "full-reload" });
227
266
  }
228
267
  };
229
- server.watcher.on("change", handle);
230
- server.watcher.on("add", handle);
231
- server.watcher.on("unlink", handle);
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);
232
272
  }
233
273
  },
234
274
  async buildStart() {
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-DWNF2MKL.js";
4
- import "./chunk-2GVOUCFD.js";
3
+ } from "./chunk-HS4B2QT2.js";
4
+ import "./chunk-3UIYKYIL.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.12",
3
+ "version": "0.4.14",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",