From 6d00b7bc021a511aba0d5dca1d932919e7e4849e Mon Sep 17 00:00:00 2001 From: icssoa <2570063477@qq.com> Date: Tue, 24 Jun 2025 23:36:15 +0800 Subject: [PATCH] 1 --- packages/vite-plugin/dist/index.js | 54 ++++++++++--------- packages/vite-plugin/src/uniapp-x/tailwind.ts | 44 +++++++++++---- 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 90799f1..f8972e2 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -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, ""); + } + }); }, }; }, diff --git a/packages/vite-plugin/src/uniapp-x/tailwind.ts b/packages/vite-plugin/src/uniapp-x/tailwind.ts index 5aba44d..3af9cfd 100644 --- a/packages/vite-plugin/src/uniapp-x/tailwind.ts +++ b/packages/vite-plugin/src/uniapp-x/tailwind.ts @@ -110,9 +110,7 @@ function postcssPlugin(): Plugin { postcssPlugin: "vite-cool-uniappx-class-mapping", prepare() { // 存储 Tailwind 颜色值 - const colorValues = { - ...TW_DEFAULT_VALUES, - }; + const colorValues: Record = {}; 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, ""); + } + }); }, }; },