uniappx 添加编译平台判断

This commit is contained in:
icssoa 2025-12-17 11:21:02 +08:00
parent 01a0917555
commit a2d4ee9bbf
6 changed files with 21 additions and 8 deletions

View File

@ -35,4 +35,5 @@ export declare const config: {
isPlugin: boolean; isPlugin: boolean;
}; };
clean: boolean; clean: boolean;
utsPlatform: string;
}; };

View File

@ -60,6 +60,7 @@
isPlugin: false, isPlugin: false,
}, },
clean: false, clean: false,
utsPlatform: "web",
}; };
// 根目录 // 根目录
@ -1942,6 +1943,9 @@ if (typeof window !== 'undefined') {
* 转换类名中的特殊字符为安全字符 * 转换类名中的特殊字符为安全字符
*/ */
function toSafeClass(className) { function toSafeClass(className) {
if (config.utsPlatform == "web") {
return className;
}
if (className.includes(":host")) { if (className.includes(":host")) {
return className; return className;
} }
@ -2022,7 +2026,7 @@ if (typeof window !== 'undefined') {
return; return;
} }
// 转换选择器为安全的类名格式 // 转换选择器为安全的类名格式
rule.selector = toSafeClass(rule.selector.replace(/\\/g, "")); rule.selector = toSafeClass(rule.selector);
}, },
// 处理声明规则 // 处理声明规则
Declaration(decl) { Declaration(decl) {
@ -2209,7 +2213,7 @@ if (typeof window !== 'undefined') {
_node.slice(0, insertIndex) + ` :class="{}"` + _node.slice(insertIndex); _node.slice(0, insertIndex) + ` :class="{}"` + _node.slice(insertIndex);
} }
// 获取暗黑模式类名 // 获取暗黑模式类名
let darkClassNames = classNames.filter((name) => name.startsWith("dark-colon-")); let darkClassNames = classNames.filter((name) => name.startsWith("dark-colon-") || name.startsWith("dark:"));
// 插件模式,不支持 dark: // 插件模式,不支持 dark:
if (config.uniapp.isPlugin) { if (config.uniapp.isPlugin) {
darkClassNames = []; darkClassNames = [];
@ -2448,6 +2452,8 @@ if (typeof window !== 'undefined') {
// 请求地址 // 请求地址
config.reqUrl = getProxyTarget(options.proxy); config.reqUrl = getProxyTarget(options.proxy);
if (config.type == "uniapp-x") { if (config.type == "uniapp-x") {
// 编译平台
config.utsPlatform = process.env.UNI_UTS_PLATFORM ?? "web";
// 是否纯净版 // 是否纯净版
config.clean = options.clean ?? true; config.clean = options.clean ?? true;
if (config.clean) { if (config.clean) {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-vue/vite-plugin", "name": "@cool-vue/vite-plugin",
"version": "8.2.19", "version": "8.2.20",
"description": "cool-admin、cool-uni builder", "description": "cool-admin、cool-uni builder",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"main": "/dist/index.js", "main": "/dist/index.js",

View File

@ -57,4 +57,5 @@ export const config = {
isPlugin: false, isPlugin: false,
}, },
clean: false, clean: false,
utsPlatform: "web",
}; };

View File

@ -15,6 +15,9 @@ export function cool(options: Config.Options) {
config.reqUrl = getProxyTarget(options.proxy); config.reqUrl = getProxyTarget(options.proxy);
if (config.type == "uniapp-x") { if (config.type == "uniapp-x") {
// 编译平台
config.utsPlatform = process.env.UNI_UTS_PLATFORM ?? "web";
// 是否纯净版 // 是否纯净版
config.clean = options.clean ?? true; config.clean = options.clean ?? true;

View File

@ -15,6 +15,10 @@ import {
* *
*/ */
export function toSafeClass(className: string): string { export function toSafeClass(className: string): string {
if (config.utsPlatform == "web") {
return className;
}
if (className.includes(":host")) { if (className.includes(":host")) {
return className; return className;
} }
@ -110,9 +114,7 @@ function postcssPlugin(): Plugin {
} }
// 转换选择器为安全的类名格式 // 转换选择器为安全的类名格式
rule.selector = toSafeClass( rule.selector = toSafeClass(rule.selector);
rule.selector.replace(/\\/g, ""),
);
}, },
// 处理声明规则 // 处理声明规则
@ -343,8 +345,8 @@ function transformPlugin(): Plugin {
} }
// 获取暗黑模式类名 // 获取暗黑模式类名
let darkClassNames = classNames.filter((name) => let darkClassNames = classNames.filter(
name.startsWith("dark-colon-"), (name) => name.startsWith("dark-colon-") || name.startsWith("dark:"),
); );
// 插件模式,不支持 dark: // 插件模式,不支持 dark: