@unocss/rule-utils 0.56.5 → 0.57.1
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/index.cjs +44 -4
- package/dist/index.d.cts +5 -1
- package/dist/index.d.mts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.mjs +38 -5
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const core = require('@unocss/core');
|
|
4
|
+
const MagicString = require('magic-string');
|
|
5
|
+
|
|
6
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
7
|
+
|
|
8
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
4
9
|
|
|
5
10
|
function getBracket(str, open, close) {
|
|
6
11
|
if (str === "")
|
|
@@ -108,8 +113,6 @@ function parseCssColor(str = "") {
|
|
|
108
113
|
const type = casedType.toLowerCase();
|
|
109
114
|
if (components.length === 0)
|
|
110
115
|
return;
|
|
111
|
-
if (["rgba", "hsla"].includes(type) && alpha == null)
|
|
112
|
-
return;
|
|
113
116
|
if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
|
|
114
117
|
return;
|
|
115
118
|
return {
|
|
@@ -129,8 +132,8 @@ function colorToString(color, alphaOverride) {
|
|
|
129
132
|
let { alpha, type } = color;
|
|
130
133
|
alpha = alphaOverride ?? alpha;
|
|
131
134
|
type = type.toLowerCase();
|
|
132
|
-
if (["hsla", "
|
|
133
|
-
return `${type
|
|
135
|
+
if (["hsla", "rgba"].includes(type))
|
|
136
|
+
return `${type}(${components.join(", ")}${alpha == null ? "" : `, ${alpha}`})`;
|
|
134
137
|
alpha = alpha == null ? "" : ` / ${alpha}`;
|
|
135
138
|
if (cssColorFunctions.includes(type))
|
|
136
139
|
return `${type}(${components.join(" ")}${alpha})`;
|
|
@@ -373,14 +376,51 @@ function variantGetParameter(prefix, matcher, separators) {
|
|
|
373
376
|
}
|
|
374
377
|
}
|
|
375
378
|
|
|
379
|
+
const themeFnRE = /theme\(\s*['"]?(.*?)['"]?\s*\)/g;
|
|
380
|
+
function hasThemeFn(str) {
|
|
381
|
+
return str.includes("theme(") && str.includes(")");
|
|
382
|
+
}
|
|
383
|
+
function transformThemeFn(code, theme, throwOnMissing = true) {
|
|
384
|
+
const matches = Array.from(code.toString().matchAll(themeFnRE));
|
|
385
|
+
if (!matches.length)
|
|
386
|
+
return code;
|
|
387
|
+
const s = new MagicString__default(code);
|
|
388
|
+
for (const match of matches) {
|
|
389
|
+
const rawArg = match[1];
|
|
390
|
+
if (!rawArg)
|
|
391
|
+
throw new Error("theme() expect exact one argument, but got 0");
|
|
392
|
+
const [rawKey, alpha] = rawArg.split("/");
|
|
393
|
+
const keys = rawKey.trim().split(".");
|
|
394
|
+
let value = keys.reduce((t, k) => t?.[k], theme);
|
|
395
|
+
if (typeof value === "string") {
|
|
396
|
+
if (alpha) {
|
|
397
|
+
const color = parseCssColor(value);
|
|
398
|
+
if (color)
|
|
399
|
+
value = colorToString(color, alpha);
|
|
400
|
+
}
|
|
401
|
+
s.overwrite(
|
|
402
|
+
match.index,
|
|
403
|
+
match.index + match[0].length,
|
|
404
|
+
value
|
|
405
|
+
);
|
|
406
|
+
} else if (throwOnMissing) {
|
|
407
|
+
throw new Error(`theme of "${rawArg}" did not found`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return s.toString();
|
|
411
|
+
}
|
|
412
|
+
|
|
376
413
|
exports.colorOpacityToString = colorOpacityToString;
|
|
377
414
|
exports.colorToString = colorToString;
|
|
378
415
|
exports.createValueHandler = createValueHandler;
|
|
379
416
|
exports.getBracket = getBracket;
|
|
380
417
|
exports.getStringComponent = getStringComponent;
|
|
381
418
|
exports.getStringComponents = getStringComponents;
|
|
419
|
+
exports.hasThemeFn = hasThemeFn;
|
|
382
420
|
exports.hex2rgba = hex2rgba;
|
|
383
421
|
exports.parseCssColor = parseCssColor;
|
|
422
|
+
exports.themeFnRE = themeFnRE;
|
|
423
|
+
exports.transformThemeFn = transformThemeFn;
|
|
384
424
|
exports.variantGetBracket = variantGetBracket;
|
|
385
425
|
exports.variantGetParameter = variantGetParameter;
|
|
386
426
|
exports.variantMatcher = variantMatcher;
|
package/dist/index.d.cts
CHANGED
|
@@ -25,4 +25,8 @@ declare function variantParentMatcher(name: string, parent: string): VariantObje
|
|
|
25
25
|
declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
|
|
26
26
|
declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
declare const themeFnRE: RegExp;
|
|
29
|
+
declare function hasThemeFn(str: string): boolean;
|
|
30
|
+
declare function transformThemeFn(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string;
|
|
31
|
+
|
|
32
|
+
export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasThemeFn, hex2rgba, parseCssColor, themeFnRE, transformThemeFn, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
|
package/dist/index.d.mts
CHANGED
|
@@ -25,4 +25,8 @@ declare function variantParentMatcher(name: string, parent: string): VariantObje
|
|
|
25
25
|
declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
|
|
26
26
|
declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
declare const themeFnRE: RegExp;
|
|
29
|
+
declare function hasThemeFn(str: string): boolean;
|
|
30
|
+
declare function transformThemeFn(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string;
|
|
31
|
+
|
|
32
|
+
export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasThemeFn, hex2rgba, parseCssColor, themeFnRE, transformThemeFn, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
|
package/dist/index.d.ts
CHANGED
|
@@ -25,4 +25,8 @@ declare function variantParentMatcher(name: string, parent: string): VariantObje
|
|
|
25
25
|
declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
|
|
26
26
|
declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
declare const themeFnRE: RegExp;
|
|
29
|
+
declare function hasThemeFn(str: string): boolean;
|
|
30
|
+
declare function transformThemeFn(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string;
|
|
31
|
+
|
|
32
|
+
export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasThemeFn, hex2rgba, parseCssColor, themeFnRE, transformThemeFn, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isString, escapeRegExp } from '@unocss/core';
|
|
2
|
+
import MagicString from 'magic-string';
|
|
2
3
|
|
|
3
4
|
function getBracket(str, open, close) {
|
|
4
5
|
if (str === "")
|
|
@@ -106,8 +107,6 @@ function parseCssColor(str = "") {
|
|
|
106
107
|
const type = casedType.toLowerCase();
|
|
107
108
|
if (components.length === 0)
|
|
108
109
|
return;
|
|
109
|
-
if (["rgba", "hsla"].includes(type) && alpha == null)
|
|
110
|
-
return;
|
|
111
110
|
if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
|
|
112
111
|
return;
|
|
113
112
|
return {
|
|
@@ -127,8 +126,8 @@ function colorToString(color, alphaOverride) {
|
|
|
127
126
|
let { alpha, type } = color;
|
|
128
127
|
alpha = alphaOverride ?? alpha;
|
|
129
128
|
type = type.toLowerCase();
|
|
130
|
-
if (["hsla", "
|
|
131
|
-
return `${type
|
|
129
|
+
if (["hsla", "rgba"].includes(type))
|
|
130
|
+
return `${type}(${components.join(", ")}${alpha == null ? "" : `, ${alpha}`})`;
|
|
132
131
|
alpha = alpha == null ? "" : ` / ${alpha}`;
|
|
133
132
|
if (cssColorFunctions.includes(type))
|
|
134
133
|
return `${type}(${components.join(" ")}${alpha})`;
|
|
@@ -371,4 +370,38 @@ function variantGetParameter(prefix, matcher, separators) {
|
|
|
371
370
|
}
|
|
372
371
|
}
|
|
373
372
|
|
|
374
|
-
|
|
373
|
+
const themeFnRE = /theme\(\s*['"]?(.*?)['"]?\s*\)/g;
|
|
374
|
+
function hasThemeFn(str) {
|
|
375
|
+
return str.includes("theme(") && str.includes(")");
|
|
376
|
+
}
|
|
377
|
+
function transformThemeFn(code, theme, throwOnMissing = true) {
|
|
378
|
+
const matches = Array.from(code.toString().matchAll(themeFnRE));
|
|
379
|
+
if (!matches.length)
|
|
380
|
+
return code;
|
|
381
|
+
const s = new MagicString(code);
|
|
382
|
+
for (const match of matches) {
|
|
383
|
+
const rawArg = match[1];
|
|
384
|
+
if (!rawArg)
|
|
385
|
+
throw new Error("theme() expect exact one argument, but got 0");
|
|
386
|
+
const [rawKey, alpha] = rawArg.split("/");
|
|
387
|
+
const keys = rawKey.trim().split(".");
|
|
388
|
+
let value = keys.reduce((t, k) => t?.[k], theme);
|
|
389
|
+
if (typeof value === "string") {
|
|
390
|
+
if (alpha) {
|
|
391
|
+
const color = parseCssColor(value);
|
|
392
|
+
if (color)
|
|
393
|
+
value = colorToString(color, alpha);
|
|
394
|
+
}
|
|
395
|
+
s.overwrite(
|
|
396
|
+
match.index,
|
|
397
|
+
match.index + match[0].length,
|
|
398
|
+
value
|
|
399
|
+
);
|
|
400
|
+
} else if (throwOnMissing) {
|
|
401
|
+
throw new Error(`theme of "${rawArg}" did not found`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return s.toString();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export { colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasThemeFn, hex2rgba, parseCssColor, themeFnRE, transformThemeFn, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/rule-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.57.1",
|
|
4
4
|
"description": "Utilities for UnoCSS",
|
|
5
5
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"node": ">=14"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"
|
|
36
|
+
"magic-string": "^0.30.5",
|
|
37
|
+
"@unocss/core": "^0.57.1"
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|
|
39
40
|
"build": "unbuild",
|