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