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
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
generateDeclaration,
|
|
5
5
|
generateJs,
|
|
6
6
|
scanVarNames
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-W4JDUG2N.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
|
|
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";
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__dirname,
|
|
3
|
+
findKeyCollisions,
|
|
3
4
|
generateDeclaration,
|
|
4
5
|
generateJs,
|
|
5
6
|
scanVarNames
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-W4JDUG2N.js";
|
|
7
8
|
|
|
8
9
|
// src/unplugin.ts
|
|
9
10
|
import { createUnplugin } from "unplugin";
|
|
@@ -11,6 +12,11 @@ import { writeFile } from "fs/promises";
|
|
|
11
12
|
import { join, resolve } from "path";
|
|
12
13
|
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
13
14
|
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
15
|
+
function warnOnCollisions(names, options) {
|
|
16
|
+
for (const [key, vars] of findKeyCollisions(names, options.prefix, options.naming)) {
|
|
17
|
+
console.warn(`css-typed-vars: multiple CSS variables map to the same key "${key}" (${vars.join(", ")}) \u2014 only the last one will be kept.`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
14
20
|
function getDtsPath(options) {
|
|
15
21
|
if (options.dts === false) return null;
|
|
16
22
|
return options.dts ? resolve(process.cwd(), options.dts) : join(__dirname, "generated.d.ts");
|
|
@@ -22,31 +28,45 @@ var unplugin_default = createUnplugin((options) => {
|
|
|
22
28
|
vite: {
|
|
23
29
|
enforce: "pre",
|
|
24
30
|
configureServer(server) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
31
|
+
let generation = 0;
|
|
32
|
+
let debounceTimer = null;
|
|
33
|
+
const rescan = async () => {
|
|
34
|
+
const myGeneration = ++generation;
|
|
35
|
+
const namesPromise = scanVarNames(options.input, options.exclude, options.selectors);
|
|
36
|
+
cachedNames = namesPromise;
|
|
37
|
+
const names = await namesPromise;
|
|
38
|
+
if (myGeneration !== generation) return;
|
|
39
|
+
warnOnCollisions(names, options);
|
|
29
40
|
const dtsPath = getDtsPath(options);
|
|
30
41
|
if (dtsPath) {
|
|
31
42
|
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
32
43
|
}
|
|
44
|
+
if (myGeneration !== generation) return;
|
|
33
45
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
34
46
|
if (mod) {
|
|
35
47
|
server.moduleGraph.invalidateModule(mod);
|
|
36
48
|
server.ws.send({ type: "full-reload" });
|
|
37
49
|
}
|
|
38
50
|
};
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
const handle = (file) => {
|
|
52
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
53
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
54
|
+
debounceTimer = setTimeout(() => {
|
|
55
|
+
debounceTimer = null;
|
|
56
|
+
rescan().catch(console.error);
|
|
57
|
+
}, 100);
|
|
58
|
+
};
|
|
59
|
+
server.watcher.on("change", handle);
|
|
60
|
+
server.watcher.on("add", handle);
|
|
61
|
+
server.watcher.on("unlink", handle);
|
|
43
62
|
}
|
|
44
63
|
},
|
|
45
64
|
async buildStart() {
|
|
46
65
|
cachedNames = scanVarNames(options.input, options.exclude, options.selectors);
|
|
66
|
+
const names = await cachedNames;
|
|
67
|
+
warnOnCollisions(names, options);
|
|
47
68
|
const dtsPath = getDtsPath(options);
|
|
48
69
|
if (!dtsPath) return;
|
|
49
|
-
const names = await cachedNames;
|
|
50
70
|
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
51
71
|
},
|
|
52
72
|
resolveId(id) {
|
|
@@ -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
|
-
|
|
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: ${VALID_NAMINGS.join(", ")}`);
|
|
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
|
}
|
|
@@ -127,9 +161,13 @@ function toKey(cssVarName, naming = "camelCase") {
|
|
|
127
161
|
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
128
162
|
if (!prefix) return key;
|
|
129
163
|
let normalizedPrefix = convertCase(prefix, naming);
|
|
130
|
-
if (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}`;
|
|
131
170
|
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
132
|
-
if (naming === "kebab") return `${normalizedPrefix}-${key}`;
|
|
133
171
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
134
172
|
}
|
|
135
173
|
function formatKey(key, naming = "camelCase") {
|
|
@@ -149,11 +187,16 @@ function findKeyCollisions(varNames, prefix, naming) {
|
|
|
149
187
|
}
|
|
150
188
|
return byKey;
|
|
151
189
|
}
|
|
152
|
-
function
|
|
153
|
-
const
|
|
190
|
+
function buildEntries(varNames, prefix, naming) {
|
|
191
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
192
|
+
for (const name of varNames) {
|
|
154
193
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
155
|
-
|
|
156
|
-
}
|
|
194
|
+
byKey.set(key, name);
|
|
195
|
+
}
|
|
196
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
197
|
+
}
|
|
198
|
+
function generateCode(varNames, prefix, naming) {
|
|
199
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
157
200
|
return [
|
|
158
201
|
"// generated \u2014 do not edit",
|
|
159
202
|
"export const cssVars = {",
|
|
@@ -165,10 +208,7 @@ function generateCode(varNames, prefix, naming) {
|
|
|
165
208
|
].join("\n");
|
|
166
209
|
}
|
|
167
210
|
function generateJs(varNames, prefix, naming) {
|
|
168
|
-
const entries = varNames.map((name) => {
|
|
169
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
170
|
-
return ` ${key}: 'var(${name})',`;
|
|
171
|
-
});
|
|
211
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
172
212
|
return [
|
|
173
213
|
"// generated \u2014 do not edit",
|
|
174
214
|
"export const cssVars = {",
|
|
@@ -178,10 +218,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
178
218
|
].join("\n");
|
|
179
219
|
}
|
|
180
220
|
function generateDeclaration(varNames, prefix, naming) {
|
|
181
|
-
const entries = varNames.map((name) => {
|
|
182
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
183
|
-
return ` ${key}: 'var(${name})';`;
|
|
184
|
-
});
|
|
221
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
185
222
|
return [
|
|
186
223
|
"export declare const cssVars: {",
|
|
187
224
|
...entries,
|
|
@@ -195,6 +232,7 @@ export {
|
|
|
195
232
|
__dirname,
|
|
196
233
|
parseVarNames,
|
|
197
234
|
scanVarNames,
|
|
235
|
+
VALID_NAMINGS,
|
|
198
236
|
findKeyCollisions,
|
|
199
237
|
generateCode,
|
|
200
238
|
generateJs,
|
package/dist/cli.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,9 +195,13 @@ 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") {
|
|
@@ -183,11 +221,16 @@ function findKeyCollisions(varNames, prefix, naming) {
|
|
|
183
221
|
}
|
|
184
222
|
return byKey;
|
|
185
223
|
}
|
|
186
|
-
function
|
|
187
|
-
const
|
|
224
|
+
function buildEntries(varNames, prefix, naming) {
|
|
225
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
226
|
+
for (const name of varNames) {
|
|
188
227
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
189
|
-
|
|
190
|
-
}
|
|
228
|
+
byKey.set(key, name);
|
|
229
|
+
}
|
|
230
|
+
return [...byKey].map(([key, name]) => ({ key, name }));
|
|
231
|
+
}
|
|
232
|
+
function generateCode(varNames, prefix, naming) {
|
|
233
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
191
234
|
return [
|
|
192
235
|
"// generated \u2014 do not edit",
|
|
193
236
|
"export const cssVars = {",
|
|
@@ -199,10 +242,7 @@ function generateCode(varNames, prefix, naming) {
|
|
|
199
242
|
].join("\n");
|
|
200
243
|
}
|
|
201
244
|
function generateJs(varNames, prefix, naming) {
|
|
202
|
-
const entries = varNames.map((name) => {
|
|
203
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
204
|
-
return ` ${key}: 'var(${name})',`;
|
|
205
|
-
});
|
|
245
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})',`);
|
|
206
246
|
return [
|
|
207
247
|
"// generated \u2014 do not edit",
|
|
208
248
|
"export const cssVars = {",
|
|
@@ -212,10 +252,7 @@ function generateJs(varNames, prefix, naming) {
|
|
|
212
252
|
].join("\n");
|
|
213
253
|
}
|
|
214
254
|
function generateDeclaration(varNames, prefix, naming) {
|
|
215
|
-
const entries = varNames.map((name) => {
|
|
216
|
-
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
217
|
-
return ` ${key}: 'var(${name})';`;
|
|
218
|
-
});
|
|
255
|
+
const entries = buildEntries(varNames, prefix, naming).map(({ key, name }) => ` ${key}: 'var(${name})';`);
|
|
219
256
|
return [
|
|
220
257
|
"export declare const cssVars: {",
|
|
221
258
|
...entries,
|
|
@@ -236,7 +273,7 @@ async function generate(options) {
|
|
|
236
273
|
}
|
|
237
274
|
const outPath = (0, import_node_path.resolve)(options.output);
|
|
238
275
|
await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
|
|
239
|
-
const jsExtMatch = /\.(m|c)?js
|
|
276
|
+
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
|
240
277
|
if (jsExtMatch) {
|
|
241
278
|
await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
242
279
|
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
@@ -277,7 +314,7 @@ async function loadConfig() {
|
|
|
277
314
|
return JSON.parse(content);
|
|
278
315
|
} else {
|
|
279
316
|
const mod = await import((0, import_node_url.pathToFileURL)(path).href);
|
|
280
|
-
return mod.default;
|
|
317
|
+
return mod.default ?? {};
|
|
281
318
|
}
|
|
282
319
|
} catch (err) {
|
|
283
320
|
const code = err.code;
|
|
@@ -305,8 +342,8 @@ async function main() {
|
|
|
305
342
|
const exclude = excludeArgs.length > 0 ? excludeArgs : config.exclude;
|
|
306
343
|
const prefix = getArg("--prefix") ?? config.prefix;
|
|
307
344
|
const namingRaw = getArg("--naming") ?? config.naming;
|
|
308
|
-
if (namingRaw !== void 0 && !
|
|
309
|
-
console.error(`css-typed-vars: invalid --naming value "${namingRaw}". Valid values:
|
|
345
|
+
if (namingRaw !== void 0 && !VALID_NAMINGS.includes(namingRaw)) {
|
|
346
|
+
console.error(`css-typed-vars: invalid --naming value "${namingRaw}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
310
347
|
process.exit(1);
|
|
311
348
|
}
|
|
312
349
|
const naming = namingRaw;
|
package/dist/cli.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
generate
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import
|
|
4
|
+
} from "./chunk-2QDRH4OL.js";
|
|
5
|
+
import {
|
|
6
|
+
VALID_NAMINGS
|
|
7
|
+
} from "./chunk-W4JDUG2N.js";
|
|
6
8
|
|
|
7
9
|
// src/cli.ts
|
|
8
10
|
import { readFile } from "fs/promises";
|
|
@@ -39,7 +41,7 @@ async function loadConfig() {
|
|
|
39
41
|
return JSON.parse(content);
|
|
40
42
|
} else {
|
|
41
43
|
const mod = await import(pathToFileURL(path).href);
|
|
42
|
-
return mod.default;
|
|
44
|
+
return mod.default ?? {};
|
|
43
45
|
}
|
|
44
46
|
} catch (err) {
|
|
45
47
|
const code = err.code;
|
|
@@ -67,8 +69,8 @@ async function main() {
|
|
|
67
69
|
const exclude = excludeArgs.length > 0 ? excludeArgs : config.exclude;
|
|
68
70
|
const prefix = getArg("--prefix") ?? config.prefix;
|
|
69
71
|
const namingRaw = getArg("--naming") ?? config.naming;
|
|
70
|
-
if (namingRaw !== void 0 && !
|
|
71
|
-
console.error(`css-typed-vars: invalid --naming value "${namingRaw}". Valid values:
|
|
72
|
+
if (namingRaw !== void 0 && !VALID_NAMINGS.includes(namingRaw)) {
|
|
73
|
+
console.error(`css-typed-vars: invalid --naming value "${namingRaw}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
72
74
|
process.exit(1);
|
|
73
75
|
}
|
|
74
76
|
const naming = namingRaw;
|
package/dist/esbuild.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) {
|