css-typed-vars 0.4.16 → 0.5.0
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/README.md +11 -5
- package/dist/chunk-H3T6RMQQ.js +51 -0
- package/dist/{chunk-NS2BULH3.js → chunk-JDZAH2YQ.js} +86 -30
- package/dist/{chunk-YJPHNISO.js → chunk-YBZGHG2G.js} +16 -12
- package/dist/cli.cjs +116 -39
- package/dist/cli.js +18 -3
- package/dist/esbuild.cjs +85 -39
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/generator-t6h8Ic7V.d.cts +6 -0
- package/dist/generator-t6h8Ic7V.d.ts +6 -0
- package/dist/index.cjs +115 -37
- package/dist/index.d.cts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +8 -2
- package/dist/rollup.cjs +85 -39
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +85 -39
- package/dist/unplugin.d.cts +1 -1
- package/dist/unplugin.d.ts +1 -1
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +85 -39
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +85 -39
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-ARQR224L.js +0 -32
- package/dist/generator-Ba8XeISD.d.cts +0 -6
- package/dist/generator-Ba8XeISD.d.ts +0 -6
package/dist/vite.cjs
CHANGED
|
@@ -104,8 +104,9 @@ function extractBlock(text, start) {
|
|
|
104
104
|
for (; i < text.length && depth > 0; i++) {
|
|
105
105
|
const char = text[i];
|
|
106
106
|
if (quote) {
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
const step = stepQuote(text, i, quote);
|
|
108
|
+
if (step.closed) quote = null;
|
|
109
|
+
i += step.consumed - 1;
|
|
109
110
|
} else if (char === '"' || char === "'") {
|
|
110
111
|
quote = char;
|
|
111
112
|
} else if (char === "{") {
|
|
@@ -139,9 +140,27 @@ function maskQuotedContent(text) {
|
|
|
139
140
|
}
|
|
140
141
|
return result;
|
|
141
142
|
}
|
|
142
|
-
function
|
|
143
|
+
function extractValue(text, start) {
|
|
144
|
+
let i = start;
|
|
145
|
+
let quote = null;
|
|
146
|
+
for (; i < text.length; i++) {
|
|
147
|
+
const char = text[i];
|
|
148
|
+
if (quote) {
|
|
149
|
+
const step = stepQuote(text, i, quote);
|
|
150
|
+
if (step.closed) quote = null;
|
|
151
|
+
i += step.consumed - 1;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (char === '"' || char === "'") {
|
|
155
|
+
quote = char;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (char === ";" || char === "}") break;
|
|
159
|
+
}
|
|
160
|
+
return text.slice(start, i).trim();
|
|
161
|
+
}
|
|
162
|
+
function scanDeclarations(css, selectors, onProperty) {
|
|
143
163
|
const stripped = stripComments(css);
|
|
144
|
-
const names = /* @__PURE__ */ new Set();
|
|
145
164
|
const allSelectors = [":root", ...selectors ?? []];
|
|
146
165
|
for (const sel of allSelectors) {
|
|
147
166
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -150,48 +169,62 @@ function parseVarNames(css, selectors) {
|
|
|
150
169
|
while (match = openRegex.exec(stripped)) {
|
|
151
170
|
const start = match.index + match[0].length;
|
|
152
171
|
const block = extractBlock(stripped, start);
|
|
153
|
-
|
|
154
|
-
|
|
172
|
+
const masked = maskQuotedContent(block);
|
|
173
|
+
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
174
|
+
const name = prop[0];
|
|
175
|
+
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
176
|
+
onProperty(name, extractValue(block, colonIndex + 1));
|
|
155
177
|
}
|
|
156
178
|
openRegex.lastIndex = start + block.length + 1;
|
|
157
179
|
}
|
|
158
180
|
}
|
|
159
|
-
|
|
181
|
+
}
|
|
182
|
+
function parseVarDeclarations(css, selectors) {
|
|
183
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
184
|
+
scanDeclarations(css, selectors, (name, value) => declarations.set(name, value));
|
|
185
|
+
return declarations;
|
|
160
186
|
}
|
|
161
187
|
|
|
162
188
|
// src/scanner.ts
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
189
|
+
function normalizePatterns(patterns) {
|
|
190
|
+
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
191
|
+
}
|
|
192
|
+
async function scanVarDeclarations(patterns, exclude, selectors) {
|
|
193
|
+
const normalized = normalizePatterns(patterns);
|
|
194
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
168
195
|
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
169
|
-
const
|
|
170
|
-
await Promise.all(
|
|
196
|
+
const perFile = await Promise.all(
|
|
171
197
|
files.map(async (file) => {
|
|
172
198
|
let css;
|
|
173
199
|
try {
|
|
174
200
|
css = await (0, import_promises.readFile)(file, "utf8");
|
|
175
201
|
} catch (err) {
|
|
176
202
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
for (const name of parseVarNames(css, selectors)) {
|
|
180
|
-
all.add(name);
|
|
203
|
+
return /* @__PURE__ */ new Map();
|
|
181
204
|
}
|
|
205
|
+
return parseVarDeclarations(css, selectors);
|
|
182
206
|
})
|
|
183
207
|
);
|
|
184
|
-
|
|
208
|
+
const all = /* @__PURE__ */ new Map();
|
|
209
|
+
for (const declarations of perFile) {
|
|
210
|
+
for (const [name, value] of declarations) {
|
|
211
|
+
all.set(name, value);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return all;
|
|
185
215
|
}
|
|
186
216
|
|
|
187
217
|
// src/generator.ts
|
|
188
|
-
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
218
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab", "constant", "pascal"];
|
|
189
219
|
function convertCase(value, naming) {
|
|
190
220
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
191
221
|
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
192
222
|
}
|
|
193
223
|
if (naming === "kebab") return value;
|
|
194
|
-
|
|
224
|
+
if (naming === "snake") return value.replace(/-+/g, "_");
|
|
225
|
+
if (naming === "constant") return value.replace(/-+/g, "_").toUpperCase();
|
|
226
|
+
if (naming === "pascal") return value.replace(/(^|-+)([a-z0-9])/g, (_, __, c) => c.toUpperCase()).replace(/-/g, "");
|
|
227
|
+
return value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
195
228
|
}
|
|
196
229
|
function toKey(cssVarName, naming = "camelCase") {
|
|
197
230
|
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
@@ -222,7 +255,7 @@ function applyPrefix(key, prefix, naming = "camelCase") {
|
|
|
222
255
|
}
|
|
223
256
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
224
257
|
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
225
|
-
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
258
|
+
if (naming === "snake" || naming === "constant") return `${normalizedPrefix}_${key}`;
|
|
226
259
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
227
260
|
}
|
|
228
261
|
function formatKey(key, naming = "camelCase") {
|
|
@@ -247,16 +280,22 @@ function warnOnCollisions(varNames, prefix, naming) {
|
|
|
247
280
|
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
281
|
}
|
|
249
282
|
}
|
|
250
|
-
function buildEntries(varNames, prefix, naming) {
|
|
283
|
+
function buildEntries(varNames, prefix, naming, declarations) {
|
|
251
284
|
const byKey = /* @__PURE__ */ new Map();
|
|
252
285
|
for (const name of varNames) {
|
|
253
286
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
254
|
-
byKey.set(key, name);
|
|
287
|
+
byKey.set(key, { name, value: declarations?.get(name) });
|
|
255
288
|
}
|
|
256
|
-
return [...byKey].map(([key,
|
|
289
|
+
return [...byKey].map(([key, entry]) => ({ key, ...entry }));
|
|
290
|
+
}
|
|
291
|
+
function formatDefaultComment(value) {
|
|
292
|
+
return ` /** @default ${value.replace(/\*\//g, "*\\/")} */`;
|
|
257
293
|
}
|
|
258
|
-
function generateJs(varNames, prefix, naming) {
|
|
259
|
-
const entries = buildEntries(varNames, prefix, naming).
|
|
294
|
+
function generateJs(varNames, prefix, naming, declarations) {
|
|
295
|
+
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
296
|
+
...value ? [formatDefaultComment(value)] : [],
|
|
297
|
+
` ${key}: 'var(${name})',`
|
|
298
|
+
]);
|
|
260
299
|
return [
|
|
261
300
|
"// generated \u2014 do not edit",
|
|
262
301
|
"export const cssVars = {",
|
|
@@ -265,8 +304,11 @@ function generateJs(varNames, prefix, naming) {
|
|
|
265
304
|
""
|
|
266
305
|
].join("\n");
|
|
267
306
|
}
|
|
268
|
-
function generateDeclaration(varNames, prefix, naming) {
|
|
269
|
-
const entries = buildEntries(varNames, prefix, naming).
|
|
307
|
+
function generateDeclaration(varNames, prefix, naming, declarations) {
|
|
308
|
+
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
309
|
+
...value ? [formatDefaultComment(value)] : [],
|
|
310
|
+
` ${key}: 'var(${name})';`
|
|
311
|
+
]);
|
|
270
312
|
return [
|
|
271
313
|
"export declare const cssVars: {",
|
|
272
314
|
...entries,
|
|
@@ -284,7 +326,8 @@ function getDtsPath(options) {
|
|
|
284
326
|
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
285
327
|
}
|
|
286
328
|
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
287
|
-
let
|
|
329
|
+
let cachedDeclarations = null;
|
|
330
|
+
const scan = () => scanVarDeclarations(options.input, options.exclude, options.selectors);
|
|
288
331
|
return {
|
|
289
332
|
name: "css-typed-vars",
|
|
290
333
|
vite: {
|
|
@@ -295,14 +338,15 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
295
338
|
let writeQueue = Promise.resolve();
|
|
296
339
|
const rescan = async () => {
|
|
297
340
|
const myGeneration = ++generation;
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
const
|
|
341
|
+
const declarationsPromise = scan();
|
|
342
|
+
cachedDeclarations = declarationsPromise;
|
|
343
|
+
const declarations = await declarationsPromise;
|
|
301
344
|
if (myGeneration !== generation) return;
|
|
345
|
+
const names = [...declarations.keys()].sort();
|
|
302
346
|
warnOnCollisions(names, options.prefix, options.naming);
|
|
303
347
|
const dtsPath = getDtsPath(options);
|
|
304
348
|
if (dtsPath) {
|
|
305
|
-
const content = generateDeclaration(names, options.prefix, options.naming);
|
|
349
|
+
const content = generateDeclaration(names, options.prefix, options.naming, declarations);
|
|
306
350
|
writeQueue = writeQueue.then(async () => {
|
|
307
351
|
if (myGeneration !== generation) return;
|
|
308
352
|
await (0, import_promises2.writeFile)(dtsPath, content, "utf8");
|
|
@@ -330,20 +374,22 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
330
374
|
}
|
|
331
375
|
},
|
|
332
376
|
async buildStart() {
|
|
333
|
-
|
|
334
|
-
const
|
|
377
|
+
cachedDeclarations = scan();
|
|
378
|
+
const declarations = await cachedDeclarations;
|
|
379
|
+
const names = [...declarations.keys()].sort();
|
|
335
380
|
warnOnCollisions(names, options.prefix, options.naming);
|
|
336
381
|
const dtsPath = getDtsPath(options);
|
|
337
382
|
if (!dtsPath) return;
|
|
338
|
-
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
383
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming, declarations), "utf8");
|
|
339
384
|
},
|
|
340
385
|
resolveId(id) {
|
|
341
386
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
342
387
|
},
|
|
343
388
|
async load(id) {
|
|
344
389
|
if (id === RESOLVED_ID) {
|
|
345
|
-
const
|
|
346
|
-
|
|
390
|
+
const declarations = await (cachedDeclarations ?? scan());
|
|
391
|
+
const names = [...declarations.keys()].sort();
|
|
392
|
+
return generateJs(names, options.prefix, options.naming, declarations);
|
|
347
393
|
}
|
|
348
394
|
}
|
|
349
395
|
};
|
package/dist/vite.d.cts
CHANGED
package/dist/vite.d.ts
CHANGED
package/dist/vite.js
CHANGED
package/dist/webpack.cjs
CHANGED
|
@@ -104,8 +104,9 @@ function extractBlock(text, start) {
|
|
|
104
104
|
for (; i < text.length && depth > 0; i++) {
|
|
105
105
|
const char = text[i];
|
|
106
106
|
if (quote) {
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
const step = stepQuote(text, i, quote);
|
|
108
|
+
if (step.closed) quote = null;
|
|
109
|
+
i += step.consumed - 1;
|
|
109
110
|
} else if (char === '"' || char === "'") {
|
|
110
111
|
quote = char;
|
|
111
112
|
} else if (char === "{") {
|
|
@@ -139,9 +140,27 @@ function maskQuotedContent(text) {
|
|
|
139
140
|
}
|
|
140
141
|
return result;
|
|
141
142
|
}
|
|
142
|
-
function
|
|
143
|
+
function extractValue(text, start) {
|
|
144
|
+
let i = start;
|
|
145
|
+
let quote = null;
|
|
146
|
+
for (; i < text.length; i++) {
|
|
147
|
+
const char = text[i];
|
|
148
|
+
if (quote) {
|
|
149
|
+
const step = stepQuote(text, i, quote);
|
|
150
|
+
if (step.closed) quote = null;
|
|
151
|
+
i += step.consumed - 1;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (char === '"' || char === "'") {
|
|
155
|
+
quote = char;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (char === ";" || char === "}") break;
|
|
159
|
+
}
|
|
160
|
+
return text.slice(start, i).trim();
|
|
161
|
+
}
|
|
162
|
+
function scanDeclarations(css, selectors, onProperty) {
|
|
143
163
|
const stripped = stripComments(css);
|
|
144
|
-
const names = /* @__PURE__ */ new Set();
|
|
145
164
|
const allSelectors = [":root", ...selectors ?? []];
|
|
146
165
|
for (const sel of allSelectors) {
|
|
147
166
|
const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -150,48 +169,62 @@ function parseVarNames(css, selectors) {
|
|
|
150
169
|
while (match = openRegex.exec(stripped)) {
|
|
151
170
|
const start = match.index + match[0].length;
|
|
152
171
|
const block = extractBlock(stripped, start);
|
|
153
|
-
|
|
154
|
-
|
|
172
|
+
const masked = maskQuotedContent(block);
|
|
173
|
+
for (const prop of masked.matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
174
|
+
const name = prop[0];
|
|
175
|
+
const colonIndex = block.indexOf(":", prop.index + name.length);
|
|
176
|
+
onProperty(name, extractValue(block, colonIndex + 1));
|
|
155
177
|
}
|
|
156
178
|
openRegex.lastIndex = start + block.length + 1;
|
|
157
179
|
}
|
|
158
180
|
}
|
|
159
|
-
|
|
181
|
+
}
|
|
182
|
+
function parseVarDeclarations(css, selectors) {
|
|
183
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
184
|
+
scanDeclarations(css, selectors, (name, value) => declarations.set(name, value));
|
|
185
|
+
return declarations;
|
|
160
186
|
}
|
|
161
187
|
|
|
162
188
|
// src/scanner.ts
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
189
|
+
function normalizePatterns(patterns) {
|
|
190
|
+
return (Array.isArray(patterns) ? patterns : [patterns]).map((p) => p.replace(/\\/g, "/"));
|
|
191
|
+
}
|
|
192
|
+
async function scanVarDeclarations(patterns, exclude, selectors) {
|
|
193
|
+
const normalized = normalizePatterns(patterns);
|
|
194
|
+
const normalizedExclude = exclude ? normalizePatterns(exclude) : [];
|
|
168
195
|
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
169
|
-
const
|
|
170
|
-
await Promise.all(
|
|
196
|
+
const perFile = await Promise.all(
|
|
171
197
|
files.map(async (file) => {
|
|
172
198
|
let css;
|
|
173
199
|
try {
|
|
174
200
|
css = await (0, import_promises.readFile)(file, "utf8");
|
|
175
201
|
} catch (err) {
|
|
176
202
|
console.warn(`css-typed-vars: skipping "${file}": ${err.message}`);
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
for (const name of parseVarNames(css, selectors)) {
|
|
180
|
-
all.add(name);
|
|
203
|
+
return /* @__PURE__ */ new Map();
|
|
181
204
|
}
|
|
205
|
+
return parseVarDeclarations(css, selectors);
|
|
182
206
|
})
|
|
183
207
|
);
|
|
184
|
-
|
|
208
|
+
const all = /* @__PURE__ */ new Map();
|
|
209
|
+
for (const declarations of perFile) {
|
|
210
|
+
for (const [name, value] of declarations) {
|
|
211
|
+
all.set(name, value);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return all;
|
|
185
215
|
}
|
|
186
216
|
|
|
187
217
|
// src/generator.ts
|
|
188
|
-
var VALID_NAMINGS = ["camelCase", "snake", "kebab"];
|
|
218
|
+
var VALID_NAMINGS = ["camelCase", "snake", "kebab", "constant", "pascal"];
|
|
189
219
|
function convertCase(value, naming) {
|
|
190
220
|
if (!VALID_NAMINGS.includes(naming)) {
|
|
191
221
|
throw new Error(`css-typed-vars: invalid naming "${naming}". Valid values: ${VALID_NAMINGS.join(", ")}`);
|
|
192
222
|
}
|
|
193
223
|
if (naming === "kebab") return value;
|
|
194
|
-
|
|
224
|
+
if (naming === "snake") return value.replace(/-+/g, "_");
|
|
225
|
+
if (naming === "constant") return value.replace(/-+/g, "_").toUpperCase();
|
|
226
|
+
if (naming === "pascal") return value.replace(/(^|-+)([a-z0-9])/g, (_, __, c) => c.toUpperCase()).replace(/-/g, "");
|
|
227
|
+
return value.replace(/-+([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/-/g, "");
|
|
195
228
|
}
|
|
196
229
|
function toKey(cssVarName, naming = "camelCase") {
|
|
197
230
|
const key = convertCase(cssVarName.replace(/^--/, ""), naming);
|
|
@@ -222,7 +255,7 @@ function applyPrefix(key, prefix, naming = "camelCase") {
|
|
|
222
255
|
}
|
|
223
256
|
normalizedPrefix = normalizedPrefix.replace(/[^A-Za-z0-9_$]/g, "");
|
|
224
257
|
if (/^\d/.test(normalizedPrefix)) normalizedPrefix = `_${normalizedPrefix}`;
|
|
225
|
-
if (naming === "snake") return `${normalizedPrefix}_${key}`;
|
|
258
|
+
if (naming === "snake" || naming === "constant") return `${normalizedPrefix}_${key}`;
|
|
226
259
|
return normalizedPrefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
227
260
|
}
|
|
228
261
|
function formatKey(key, naming = "camelCase") {
|
|
@@ -247,16 +280,22 @@ function warnOnCollisions(varNames, prefix, naming) {
|
|
|
247
280
|
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
281
|
}
|
|
249
282
|
}
|
|
250
|
-
function buildEntries(varNames, prefix, naming) {
|
|
283
|
+
function buildEntries(varNames, prefix, naming, declarations) {
|
|
251
284
|
const byKey = /* @__PURE__ */ new Map();
|
|
252
285
|
for (const name of varNames) {
|
|
253
286
|
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
254
|
-
byKey.set(key, name);
|
|
287
|
+
byKey.set(key, { name, value: declarations?.get(name) });
|
|
255
288
|
}
|
|
256
|
-
return [...byKey].map(([key,
|
|
289
|
+
return [...byKey].map(([key, entry]) => ({ key, ...entry }));
|
|
290
|
+
}
|
|
291
|
+
function formatDefaultComment(value) {
|
|
292
|
+
return ` /** @default ${value.replace(/\*\//g, "*\\/")} */`;
|
|
257
293
|
}
|
|
258
|
-
function generateJs(varNames, prefix, naming) {
|
|
259
|
-
const entries = buildEntries(varNames, prefix, naming).
|
|
294
|
+
function generateJs(varNames, prefix, naming, declarations) {
|
|
295
|
+
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
296
|
+
...value ? [formatDefaultComment(value)] : [],
|
|
297
|
+
` ${key}: 'var(${name})',`
|
|
298
|
+
]);
|
|
260
299
|
return [
|
|
261
300
|
"// generated \u2014 do not edit",
|
|
262
301
|
"export const cssVars = {",
|
|
@@ -265,8 +304,11 @@ function generateJs(varNames, prefix, naming) {
|
|
|
265
304
|
""
|
|
266
305
|
].join("\n");
|
|
267
306
|
}
|
|
268
|
-
function generateDeclaration(varNames, prefix, naming) {
|
|
269
|
-
const entries = buildEntries(varNames, prefix, naming).
|
|
307
|
+
function generateDeclaration(varNames, prefix, naming, declarations) {
|
|
308
|
+
const entries = buildEntries(varNames, prefix, naming, declarations).flatMap(({ key, name, value }) => [
|
|
309
|
+
...value ? [formatDefaultComment(value)] : [],
|
|
310
|
+
` ${key}: 'var(${name})';`
|
|
311
|
+
]);
|
|
270
312
|
return [
|
|
271
313
|
"export declare const cssVars: {",
|
|
272
314
|
...entries,
|
|
@@ -284,7 +326,8 @@ function getDtsPath(options) {
|
|
|
284
326
|
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
285
327
|
}
|
|
286
328
|
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
287
|
-
let
|
|
329
|
+
let cachedDeclarations = null;
|
|
330
|
+
const scan = () => scanVarDeclarations(options.input, options.exclude, options.selectors);
|
|
288
331
|
return {
|
|
289
332
|
name: "css-typed-vars",
|
|
290
333
|
vite: {
|
|
@@ -295,14 +338,15 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
295
338
|
let writeQueue = Promise.resolve();
|
|
296
339
|
const rescan = async () => {
|
|
297
340
|
const myGeneration = ++generation;
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
const
|
|
341
|
+
const declarationsPromise = scan();
|
|
342
|
+
cachedDeclarations = declarationsPromise;
|
|
343
|
+
const declarations = await declarationsPromise;
|
|
301
344
|
if (myGeneration !== generation) return;
|
|
345
|
+
const names = [...declarations.keys()].sort();
|
|
302
346
|
warnOnCollisions(names, options.prefix, options.naming);
|
|
303
347
|
const dtsPath = getDtsPath(options);
|
|
304
348
|
if (dtsPath) {
|
|
305
|
-
const content = generateDeclaration(names, options.prefix, options.naming);
|
|
349
|
+
const content = generateDeclaration(names, options.prefix, options.naming, declarations);
|
|
306
350
|
writeQueue = writeQueue.then(async () => {
|
|
307
351
|
if (myGeneration !== generation) return;
|
|
308
352
|
await (0, import_promises2.writeFile)(dtsPath, content, "utf8");
|
|
@@ -330,20 +374,22 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => {
|
|
|
330
374
|
}
|
|
331
375
|
},
|
|
332
376
|
async buildStart() {
|
|
333
|
-
|
|
334
|
-
const
|
|
377
|
+
cachedDeclarations = scan();
|
|
378
|
+
const declarations = await cachedDeclarations;
|
|
379
|
+
const names = [...declarations.keys()].sort();
|
|
335
380
|
warnOnCollisions(names, options.prefix, options.naming);
|
|
336
381
|
const dtsPath = getDtsPath(options);
|
|
337
382
|
if (!dtsPath) return;
|
|
338
|
-
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
383
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming, declarations), "utf8");
|
|
339
384
|
},
|
|
340
385
|
resolveId(id) {
|
|
341
386
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
342
387
|
},
|
|
343
388
|
async load(id) {
|
|
344
389
|
if (id === RESOLVED_ID) {
|
|
345
|
-
const
|
|
346
|
-
|
|
390
|
+
const declarations = await (cachedDeclarations ?? scan());
|
|
391
|
+
const names = [...declarations.keys()].sort();
|
|
392
|
+
return generateJs(names, options.prefix, options.naming, declarations);
|
|
347
393
|
}
|
|
348
394
|
}
|
|
349
395
|
};
|
package/dist/webpack.d.cts
CHANGED
package/dist/webpack.d.ts
CHANGED
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
package/dist/chunk-ARQR224L.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
generateCode,
|
|
3
|
-
generateDeclaration,
|
|
4
|
-
generateJs,
|
|
5
|
-
scanVarNames,
|
|
6
|
-
warnOnCollisions
|
|
7
|
-
} from "./chunk-NS2BULH3.js";
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
import { writeFile, mkdir } from "fs/promises";
|
|
11
|
-
import { dirname, resolve } from "path";
|
|
12
|
-
async function generate(options) {
|
|
13
|
-
const names = await scanVarNames(options.input, options.exclude, options.selectors);
|
|
14
|
-
if (names.length === 0) {
|
|
15
|
-
console.warn("css-typed-vars: no CSS custom properties found.");
|
|
16
|
-
}
|
|
17
|
-
warnOnCollisions(names, options.prefix, options.naming);
|
|
18
|
-
const outPath = resolve(options.output);
|
|
19
|
-
await mkdir(dirname(outPath), { recursive: true });
|
|
20
|
-
const jsExtMatch = /\.(m|c)?js$/i.exec(options.output);
|
|
21
|
-
if (jsExtMatch) {
|
|
22
|
-
await writeFile(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
23
|
-
const dtsPath = outPath.slice(0, -jsExtMatch[0].length) + ".d.ts";
|
|
24
|
-
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
25
|
-
} else {
|
|
26
|
-
await writeFile(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export {
|
|
31
|
-
generate
|
|
32
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type NamingConvention = 'camelCase' | 'snake' | 'kebab';
|
|
2
|
-
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
3
|
-
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
4
|
-
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
5
|
-
|
|
6
|
-
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type NamingConvention = 'camelCase' | 'snake' | 'kebab';
|
|
2
|
-
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
3
|
-
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
4
|
-
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
5
|
-
|
|
6
|
-
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|