This commit is contained in:
icssoa 2025-06-24 23:36:15 +08:00
parent 7c0ccb944d
commit 6d00b7bc02
2 changed files with 61 additions and 37 deletions

View File

@ -1816,20 +1816,6 @@ if (typeof window !== 'undefined') {
}; };
// @ts-ignore // @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", postcssPlugin: "vite-cool-uniappx-class-mapping",
prepare() { prepare() {
// 存储 Tailwind 颜色值
const colorValues = {
...TW_DEFAULT_VALUES,
};
return { return {
// 处理选择器规则 // 处理选择器规则
Rule(rule) { Rule(rule) {
@ -1918,9 +1900,13 @@ if (typeof window !== 'undefined') {
// 处理声明规则 // 处理声明规则
Declaration(decl) { Declaration(decl) {
const className = decl.parent.selector || ""; const className = decl.parent.selector || "";
if (!decl.parent._twValues) {
decl.parent._twValues = {};
}
// 处理 Tailwind 自定义属性 // 处理 Tailwind 自定义属性
if (decl.prop.includes("--tw-")) { if (decl.prop.includes("--tw-")) {
colorValues[decl.prop] = decl.value.includes("rem") decl.parent._twValues[decl.prop] =
decl.value.includes("rem")
? remToRpx(decl.value) ? remToRpx(decl.value)
: decl.value; : decl.value;
decl.remove(); decl.remove();
@ -1978,18 +1964,34 @@ if (typeof window !== 'undefined') {
const twKey = node.nodes[0]?.value; const twKey = node.nodes[0]?.value;
// 替换 Tailwind 变量为实际值 // 替换 Tailwind 变量为实际值
if (twKey?.startsWith("--tw-")) { if (twKey?.startsWith("--tw-")) {
if (decl.parent._twValues) {
node.type = "word"; node.type = "word";
node.value = colorValues[twKey]; node.value =
decl.parent._twValues[twKey] ||
"none";
hasChanges = true; hasChanges = true;
} }
} }
}
}); });
// 更新声明值 // 更新声明值
if (hasChanges) { if (hasChanges) {
decl.value = parsed.toString(); decl.value = parsed.toString();
} }
// 移除 undefined // 移除 Tailwind 生成的无效 none 变换
decl.value = decl.value.replaceAll(" undefined", ""); 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", postcssPlugin: "vite-cool-uniappx-class-mapping",
prepare() { prepare() {
// 存储 Tailwind 颜色值 // 存储 Tailwind 颜色值
const colorValues = { const colorValues: Record<string, string> = {};
...TW_DEFAULT_VALUES,
};
return { return {
// 处理选择器规则 // 处理选择器规则
@ -136,11 +134,17 @@ function postcssPlugin(): Plugin {
Declaration(decl: any) { Declaration(decl: any) {
const className = decl.parent.selector || ""; const className = decl.parent.selector || "";
if (!decl.parent._twValues) {
decl.parent._twValues = {};
}
// 处理 Tailwind 自定义属性 // 处理 Tailwind 自定义属性
if (decl.prop.includes("--tw-")) { if (decl.prop.includes("--tw-")) {
colorValues[decl.prop] = decl.value.includes("rem") decl.parent._twValues[decl.prop] =
decl.value.includes("rem")
? remToRpx(decl.value) ? remToRpx(decl.value)
: decl.value; : decl.value;
decl.remove(); decl.remove();
return; return;
} }
@ -214,11 +218,16 @@ function postcssPlugin(): Plugin {
// 替换 Tailwind 变量为实际值 // 替换 Tailwind 变量为实际值
if (twKey?.startsWith("--tw-")) { if (twKey?.startsWith("--tw-")) {
if (decl.parent._twValues) {
node.type = "word"; node.type = "word";
node.value = colorValues[twKey]; node.value =
decl.parent._twValues[twKey] ||
"none";
hasChanges = true; hasChanges = true;
} }
} }
}
}); });
// 更新声明值 // 更新声明值
@ -226,8 +235,21 @@ function postcssPlugin(): Plugin {
decl.value = parsed.toString(); decl.value = parsed.toString();
} }
// 移除 undefined // 移除 Tailwind 生成的无效 none 变换
decl.value = decl.value.replaceAll(" undefined", ""); 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, "");
}
});
}, },
}; };
}, },