This commit is contained in:
icssoa 2025-06-24 23:36:15 +08:00 committed by icssoa
parent 609f909c75
commit 85786156b9
2 changed files with 61 additions and 37 deletions

View File

@ -1816,20 +1816,6 @@ if (typeof window !== 'undefined') {
};
// @ts-ignore
/**
* Tailwind 默认值
*/
const TW_DEFAULT_VALUES = {
"--tw-border-spacing-x": 0,
"--tw-border-spacing-y": 0,
"--tw-translate-x": 0,
"--tw-translate-y": 0,
"--tw-rotate": 0,
"--tw-skew-x": 0,
"--tw-skew-y": 0,
"--tw-scale-x": 1,
"--tw-scale-y": 1,
};
/**
* 转换类名中的特殊字符为安全字符
*/
@ -1901,10 +1887,6 @@ if (typeof window !== 'undefined') {
{
postcssPlugin: "vite-cool-uniappx-class-mapping",
prepare() {
// 存储 Tailwind 颜色值
const colorValues = {
...TW_DEFAULT_VALUES,
};
return {
// 处理选择器规则
Rule(rule) {
@ -1918,11 +1900,15 @@ if (typeof window !== 'undefined') {
// 处理声明规则
Declaration(decl) {
const className = decl.parent.selector || "";
if (!decl.parent._twValues) {
decl.parent._twValues = {};
}
// 处理 Tailwind 自定义属性
if (decl.prop.includes("--tw-")) {
colorValues[decl.prop] = decl.value.includes("rem")
? remToRpx(decl.value)
: decl.value;
decl.parent._twValues[decl.prop] =
decl.value.includes("rem")
? remToRpx(decl.value)
: decl.value;
decl.remove();
return;
}
@ -1978,9 +1964,13 @@ if (typeof window !== 'undefined') {
const twKey = node.nodes[0]?.value;
// 替换 Tailwind 变量为实际值
if (twKey?.startsWith("--tw-")) {
node.type = "word";
node.value = colorValues[twKey];
hasChanges = true;
if (decl.parent._twValues) {
node.type = "word";
node.value =
decl.parent._twValues[twKey] ||
"none";
hasChanges = true;
}
}
}
});
@ -1988,8 +1978,20 @@ if (typeof window !== 'undefined') {
if (hasChanges) {
decl.value = parsed.toString();
}
// 移除 undefined
decl.value = decl.value.replaceAll(" undefined", "");
// 移除 Tailwind 生成的无效 none 变换
const nones = [
"translate(none, none)",
"rotate(none)",
"skewX(none)",
"skewY(none)",
"scaleX(none)",
"scaleY(none)",
];
nones.forEach((noneStr) => {
if (decl.value && decl.value.includes(noneStr)) {
decl.value = decl.value.replace(noneStr, "");
}
});
},
};
},

View File

@ -110,9 +110,7 @@ function postcssPlugin(): Plugin {
postcssPlugin: "vite-cool-uniappx-class-mapping",
prepare() {
// 存储 Tailwind 颜色值
const colorValues = {
...TW_DEFAULT_VALUES,
};
const colorValues: Record<string, string> = {};
return {
// 处理选择器规则
@ -136,11 +134,17 @@ function postcssPlugin(): Plugin {
Declaration(decl: any) {
const className = decl.parent.selector || "";
if (!decl.parent._twValues) {
decl.parent._twValues = {};
}
// 处理 Tailwind 自定义属性
if (decl.prop.includes("--tw-")) {
colorValues[decl.prop] = decl.value.includes("rem")
? remToRpx(decl.value)
: decl.value;
decl.parent._twValues[decl.prop] =
decl.value.includes("rem")
? remToRpx(decl.value)
: decl.value;
decl.remove();
return;
}
@ -214,9 +218,14 @@ function postcssPlugin(): Plugin {
// 替换 Tailwind 变量为实际值
if (twKey?.startsWith("--tw-")) {
node.type = "word";
node.value = colorValues[twKey];
hasChanges = true;
if (decl.parent._twValues) {
node.type = "word";
node.value =
decl.parent._twValues[twKey] ||
"none";
hasChanges = true;
}
}
}
});
@ -226,8 +235,21 @@ function postcssPlugin(): Plugin {
decl.value = parsed.toString();
}
// 移除 undefined
decl.value = decl.value.replaceAll(" undefined", "");
// 移除 Tailwind 生成的无效 none 变换
const nones = [
"translate(none, none)",
"rotate(none)",
"skewX(none)",
"skewY(none)",
"scaleX(none)",
"scaleY(none)",
];
nones.forEach((noneStr) => {
if (decl.value && decl.value.includes(noneStr)) {
decl.value = decl.value.replace(noneStr, "");
}
});
},
};
},