From da26abc4d78ca26230345132066771c0479b0776 Mon Sep 17 00:00:00 2001 From: icssoa <615206459@qq.com> Date: Sun, 8 Jun 2025 10:33:20 +0800 Subject: [PATCH] unix update --- packages/vite-plugin/dist/index.js | 41 ++++++++++++++----- packages/vite-plugin/src/uniapp-x/tailwind.ts | 1 - packages/vite-plugin/src/uniapp-x/utils.ts | 34 ++++++++++++++- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 26386a0..47ff2d2 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -7,7 +7,6 @@ const config = { type: "admin", reqUrl: "", - demo: false, nameTag: true, eps: { enable: true, @@ -245,13 +244,36 @@ while ((ternaryMatch = ternaryRegex.exec(value)) !== null) { ternaryMatch[2].trim() && names.add(ternaryMatch[2]); } + // 匹配反引号模板字符串 + const templateRegex = /`([^`]*)`/g; + let templateMatch; + while ((templateMatch = templateRegex.exec(value)) !== null) { + const templateContent = templateMatch[1]; + // 提取模板字符串中的普通文本部分(排除 ${} 表达式) + const textParts = templateContent.split(/\$\{[^}]*\}/); + textParts.forEach((part) => { + part.trim() + .split(/\s+/) + .forEach((className) => { + className.trim() && names.add(className.trim()); + }); + }); + // 提取模板字符串中 ${} 表达式内的字符串 + const expressionRegex = /\$\{([^}]*)\}/g; + let expressionMatch; + while ((expressionMatch = expressionRegex.exec(templateContent)) !== null) { + const expression = expressionMatch[1]; + // 递归处理表达式中的动态类名 + getDynamicClassNames(expression).forEach((name) => names.add(name)); + } + } return Array.from(names); }; /** * 获取类名 */ function getClassNames(html) { - const classRegex = /(?:class|:class)\s*=\s*(["'])([\s\S]*?)\1/gi; + const classRegex = /(?:class|:class|:pt)\s*=\s*(['"`])([\s\S]*?)\1/gi; const classNames = new Set(); let match; while ((match = classRegex.exec(html)) !== null) { @@ -272,7 +294,7 @@ * 获取 class 内容 */ function getClassContent(html) { - const regex = /(?:class|:class)\s*=\s*(['"])([\s\S]*?)\1/g; + const regex = /(?:class|:class|:pt)\s*=\s*(['"`])([\s\S]*?)\1/g; const texts = []; let match; while ((match = regex.exec(html)) !== null) { @@ -410,6 +432,9 @@ "group-hover:", ]; const statePrefixes = ["dark:", "light:", "sm:", "md:", "lg:", "xl:", "2xl:"]; + if (className.includes("!")) { + return true; + } for (const prefix of prefixes) { if (className.startsWith(prefix)) { return true; @@ -1165,13 +1190,8 @@ if (match) { const value = match[1]; try { - if (config.type == "uniapp-x") { - return proxy[value].target; - } - else { - const { target, rewrite } = proxy[`/${value}/`]; - return target + rewrite(`/${value}`); - } + const { target, rewrite } = proxy[`/${value}/`]; + return target + rewrite(`/${value}`); } catch (err) { error(`[cool-proxy] Error:${value} → ` + getPath()); @@ -1823,7 +1843,6 @@ if (typeof window !== 'undefined') { .replaceAll(':class="{}"', "") .replaceAll('class=""', "") .replaceAll('class=" "', ""); - // console.log(modifiedCode); return { code: modifiedCode, map: { mappings: "" }, diff --git a/packages/vite-plugin/src/uniapp-x/tailwind.ts b/packages/vite-plugin/src/uniapp-x/tailwind.ts index b3d4474..0db0adb 100644 --- a/packages/vite-plugin/src/uniapp-x/tailwind.ts +++ b/packages/vite-plugin/src/uniapp-x/tailwind.ts @@ -331,7 +331,6 @@ function transformPlugin(): Plugin { .replaceAll('class=""', "") .replaceAll('class=" "', ""); - // console.log(modifiedCode); return { code: modifiedCode, map: { mappings: "" }, diff --git a/packages/vite-plugin/src/uniapp-x/utils.ts b/packages/vite-plugin/src/uniapp-x/utils.ts index 81610fd..e9dce4f 100644 --- a/packages/vite-plugin/src/uniapp-x/utils.ts +++ b/packages/vite-plugin/src/uniapp-x/utils.ts @@ -25,6 +25,32 @@ export const getDynamicClassNames = (value: string): string[] => { ternaryMatch[2].trim() && names.add(ternaryMatch[2]); } + // 匹配反引号模板字符串 + const templateRegex = /`([^`]*)`/g; + let templateMatch; + while ((templateMatch = templateRegex.exec(value)) !== null) { + const templateContent = templateMatch[1]; + + // 提取模板字符串中的普通文本部分(排除 ${} 表达式) + const textParts = templateContent.split(/\$\{[^}]*\}/); + textParts.forEach((part) => { + part.trim() + .split(/\s+/) + .forEach((className) => { + className.trim() && names.add(className.trim()); + }); + }); + + // 提取模板字符串中 ${} 表达式内的字符串 + const expressionRegex = /\$\{([^}]*)\}/g; + let expressionMatch; + while ((expressionMatch = expressionRegex.exec(templateContent)) !== null) { + const expression = expressionMatch[1]; + // 递归处理表达式中的动态类名 + getDynamicClassNames(expression).forEach((name) => names.add(name)); + } + } + return Array.from(names); }; @@ -32,7 +58,7 @@ export const getDynamicClassNames = (value: string): string[] => { * 获取类名 */ export function getClassNames(html: string): string[] { - const classRegex = /(?:class|:class)\s*=\s*(["'])([\s\S]*?)\1/gi; + const classRegex = /(?:class|:class|:pt)\s*=\s*(['"`])([\s\S]*?)\1/gi; const classNames = new Set(); let match; @@ -56,7 +82,7 @@ export function getClassNames(html: string): string[] { * 获取 class 内容 */ export function getClassContent(html: string) { - const regex = /(?:class|:class)\s*=\s*(['"])([\s\S]*?)\1/g; + const regex = /(?:class|:class|:pt)\s*=\s*(['"`])([\s\S]*?)\1/g; const texts: string[] = []; let match; @@ -222,6 +248,10 @@ export function isTailwindClass(className: string): boolean { const statePrefixes = ["dark:", "light:", "sm:", "md:", "lg:", "xl:", "2xl:"]; + if (className.includes("!")) { + return true; + } + for (const prefix of prefixes) { if (className.startsWith(prefix)) { return true;