更新 uniappx 插件

This commit is contained in:
icssoa 2025-09-18 12:27:26 +08:00
parent f670008cb4
commit 5535535439
8 changed files with 124 additions and 89 deletions

View File

@ -31,5 +31,8 @@ export declare const config: {
rpxRatio: number; rpxRatio: number;
darkTextClass: string; darkTextClass: string;
}; };
uniapp: {
isPlugin: boolean;
};
clean: boolean; clean: boolean;
}; };

View File

@ -56,6 +56,9 @@
rpxRatio: 2, rpxRatio: 2,
darkTextClass: "dark:text-surface-50", darkTextClass: "dark:text-surface-50",
}, },
uniapp: {
isPlugin: false,
},
clean: false, clean: false,
}; };
@ -195,26 +198,6 @@
function error(message) { function error(message) {
console.log("\x1B[31m%s\x1B[0m", message); console.log("\x1B[31m%s\x1B[0m", message);
} }
/**
* 比较两个版本号
* @param version1 版本号1 (: "1.2.3")
* @param version2 版本号2 (: "1.2.4")
* @returns 1: version1 > version2, 0: 相等, -1: version1 < version2
*/
function compareVersion(version1, version2) {
const v1Parts = version1.split(".").map(Number);
const v2Parts = version2.split(".").map(Number);
const maxLength = Math.max(v1Parts.length, v2Parts.length);
for (let i = 0; i < maxLength; i++) {
const v1Part = v1Parts[i] || 0;
const v2Part = v2Parts[i] || 0;
if (v1Part > v2Part)
return 1;
if (v1Part < v2Part)
return -1;
}
return 0;
}
/** /**
* 将模板字符串扁平化处理转换为 Service 类型定义 * 将模板字符串扁平化处理转换为 Service 类型定义
@ -2169,27 +2152,30 @@ if (typeof window !== 'undefined') {
if (_node.startsWith("<input")) { if (_node.startsWith("<input")) {
_node = _node.replace("/>", "</input>"); _node = _node.replace("/>", "</input>");
} }
// 为 text 节点添加暗黑模式文本颜色 // uniappx 插件模式
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) { if (!config.uniapp.isPlugin) {
let classIndex = _node.indexOf("class="); // 为 text 节点添加暗黑模式文本颜色
// 处理动态 class if (!_node.includes(darkTextClass) && _node.startsWith("<text")) {
if (classIndex >= 0) { let classIndex = _node.indexOf("class=");
if (_node[classIndex - 1] == ":") { // 处理动态 class
classIndex = _node.lastIndexOf("class="); if (classIndex >= 0) {
if (_node[classIndex - 1] == ":") {
classIndex = _node.lastIndexOf("class=");
}
}
// 添加暗黑模式类名
if (classIndex >= 0) {
_node =
_node.substring(0, classIndex + 7) +
`${darkTextClass} ` +
_node.substring(classIndex + 7, _node.length);
}
else {
_node =
_node.substring(0, 5) +
` class="${darkTextClass}" ` +
_node.substring(5, _node.length);
} }
}
// 添加暗黑模式类名
if (classIndex >= 0) {
_node =
_node.substring(0, classIndex + 7) +
`${darkTextClass} ` +
_node.substring(classIndex + 7, _node.length);
}
else {
_node =
_node.substring(0, 5) +
` class="${darkTextClass}" ` +
_node.substring(5, _node.length);
} }
} }
// 获取所有类名 // 获取所有类名
@ -2209,7 +2195,11 @@ if (typeof window !== 'undefined') {
_node = _node.slice(0, -1) + ` :class="{}"` + ">"; _node = _node.slice(0, -1) + ` :class="{}"` + ">";
} }
// 获取暗黑模式类名 // 获取暗黑模式类名
const darkClassNames = classNames.filter((name) => name.startsWith("dark-colon-")); let darkClassNames = classNames.filter((name) => name.startsWith("dark-colon-"));
// 插件模式,不支持 dark:
if (config.uniapp.isPlugin) {
darkClassNames = [];
}
// 生成暗黑模式类名的动态绑定 // 生成暗黑模式类名的动态绑定
const darkClassContent = darkClassNames const darkClassContent = darkClassNames
.map((name) => { .map((name) => {
@ -2245,7 +2235,9 @@ if (typeof window !== 'undefined') {
if (!modifiedCode.includes("<script")) { if (!modifiedCode.includes("<script")) {
modifiedCode += '<script lang="ts" setup></script>'; modifiedCode += '<script lang="ts" setup></script>';
} }
modifiedCode = addScriptContent(modifiedCode, "\nimport { isDark as __isDark } from '@/cool';"); if (!config.uniapp.isPlugin) {
modifiedCode = addScriptContent(modifiedCode, "\nimport { isDark as __isDark } from '@/cool';");
}
} }
// 清理空的类名绑定 // 清理空的类名绑定
modifiedCode = modifiedCode modifiedCode = modifiedCode
@ -2331,11 +2323,6 @@ if (typeof window !== 'undefined') {
return null; return null;
} }
} }
// 获取版本号
function getVersion() {
const pkg = readFile(rootDir("package.json"), true);
return pkg?.version || "0.0.0";
}
function codePlugin() { function codePlugin() {
return [ return [
{ {
@ -2344,22 +2331,28 @@ if (typeof window !== 'undefined') {
async transform(code, id) { async transform(code, id) {
if (id.includes("/cool/ctx/index.ts")) { if (id.includes("/cool/ctx/index.ts")) {
const ctx = await createCtx(); const ctx = await createCtx();
// 版本
const version = getVersion();
// 主题配置 // 主题配置
const theme = readFile(rootDir("theme.json"), true); const theme = readFile(rootDir("theme.json"), true);
// 主题配置 // 主题配置
ctx["theme"] = theme; ctx["theme"] = theme || {};
if (compareVersion(version, "8.0.2") >= 0) { // 颜色值
// 颜色值 ctx["color"] = getTailwindColor();
ctx["color"] = getTailwindColor(); if (!ctx.subPackages) {
ctx.subPackages = [];
}
if (!ctx.tabBar) {
ctx.tabBar = {};
} }
// 安全字符映射 // 安全字符映射
ctx["SAFE_CHAR_MAP_LOCALE"] = []; ctx["SAFE_CHAR_MAP_LOCALE"] = [];
for (const i in SAFE_CHAR_MAP_LOCALE) { for (const i in SAFE_CHAR_MAP_LOCALE) {
ctx["SAFE_CHAR_MAP_LOCALE"].push([i, SAFE_CHAR_MAP_LOCALE[i]]); ctx["SAFE_CHAR_MAP_LOCALE"].push([i, SAFE_CHAR_MAP_LOCALE[i]]);
} }
code = code.replace("const ctx = {}", `const ctx = ${JSON.stringify(ctx, null, 4)}`); let ctxCode = JSON.stringify(ctx, null, 4);
ctxCode = ctxCode.replace(`"tabBar": {}`, `"tabBar": {} as TabBar`);
ctxCode = ctxCode.replace(`"subPackages": []`, `"subPackages": [] as SubPackage[]`);
code = code.replace("const ctx = {}", `const ctx = ${ctxCode}`);
code = code.replace("const ctx = parse<Ctx>({})!", `const ctx = parse<Ctx>(${ctxCode})!`);
} }
// if (id.includes("/cool/service/index.ts")) { // if (id.includes("/cool/service/index.ts")) {
// const eps = await createEps(); // const eps = await createEps();
@ -2473,6 +2466,10 @@ if (typeof window !== 'undefined') {
if (config.type == "uniapp-x") { if (config.type == "uniapp-x") {
config.eps.enable = false; config.eps.enable = false;
} }
// uniapp
if (options.uniapp) {
lodash.assign(config.uniapp, options.uniapp);
}
// tailwind // tailwind
if (options.tailwind) { if (options.tailwind) {
lodash.assign(config.tailwind, options.tailwind); lodash.assign(config.tailwind, options.tailwind);

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-vue/vite-plugin", "name": "@cool-vue/vite-plugin",
"version": "8.2.10", "version": "8.2.12",
"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

@ -53,5 +53,8 @@ export const config = {
rpxRatio: 2, rpxRatio: 2,
darkTextClass: "dark:text-surface-50", darkTextClass: "dark:text-surface-50",
}, },
uniapp: {
isPlugin: false,
},
clean: false, clean: false,
}; };

View File

@ -60,6 +60,11 @@ export function cool(options: Config.Options) {
config.eps.enable = false; config.eps.enable = false;
} }
// uniapp
if (options.uniapp) {
assign(config.uniapp, options.uniapp);
}
// tailwind // tailwind
if (options.tailwind) { if (options.tailwind) {
assign(config.tailwind, options.tailwind); assign(config.tailwind, options.tailwind);

View File

@ -100,18 +100,21 @@ export function codePlugin(): Plugin[] {
if (id.includes("/cool/ctx/index.ts")) { if (id.includes("/cool/ctx/index.ts")) {
const ctx = await createCtx(); const ctx = await createCtx();
// 版本
const version = getVersion();
// 主题配置 // 主题配置
const theme = readFile(rootDir("theme.json"), true); const theme = readFile(rootDir("theme.json"), true);
// 主题配置 // 主题配置
ctx["theme"] = theme; ctx["theme"] = theme || {};
if (compareVersion(version, "8.0.2") >= 0) { // 颜色值
// 颜色值 ctx["color"] = getTailwindColor();
ctx["color"] = getTailwindColor();
if (!ctx.subPackages) {
ctx.subPackages = [];
}
if (!ctx.tabBar) {
ctx.tabBar = {};
} }
// 安全字符映射 // 安全字符映射
@ -120,9 +123,19 @@ export function codePlugin(): Plugin[] {
ctx["SAFE_CHAR_MAP_LOCALE"].push([i, SAFE_CHAR_MAP_LOCALE[i]]); ctx["SAFE_CHAR_MAP_LOCALE"].push([i, SAFE_CHAR_MAP_LOCALE[i]]);
} }
let ctxCode = JSON.stringify(ctx, null, 4);
ctxCode = ctxCode.replace(`"tabBar": {}`, `"tabBar": {} as TabBar`);
ctxCode = ctxCode.replace(
`"subPackages": []`,
`"subPackages": [] as SubPackage[]`,
);
code = code.replace("const ctx = {}", `const ctx = ${ctxCode}`);
code = code.replace( code = code.replace(
"const ctx = {}", "const ctx = parse<Ctx>({})!",
`const ctx = ${JSON.stringify(ctx, null, 4)}`, `const ctx = parse<Ctx>(${ctxCode})!`,
); );
} }

View File

@ -287,28 +287,31 @@ function transformPlugin(): Plugin {
_node = _node.replace("/>", "</input>"); _node = _node.replace("/>", "</input>");
} }
// 为 text 节点添加暗黑模式文本颜色 // uniappx 插件模式
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) { if (!config.uniapp.isPlugin) {
let classIndex = _node.indexOf("class="); // 为 text 节点添加暗黑模式文本颜色
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) {
let classIndex = _node.indexOf("class=");
// 处理动态 class // 处理动态 class
if (classIndex >= 0) { if (classIndex >= 0) {
if (_node[classIndex - 1] == ":") { if (_node[classIndex - 1] == ":") {
classIndex = _node.lastIndexOf("class="); classIndex = _node.lastIndexOf("class=");
}
} }
}
// 添加暗黑模式类名 // 添加暗黑模式类名
if (classIndex >= 0) { if (classIndex >= 0) {
_node = _node =
_node.substring(0, classIndex + 7) + _node.substring(0, classIndex + 7) +
`${darkTextClass} ` + `${darkTextClass} ` +
_node.substring(classIndex + 7, _node.length); _node.substring(classIndex + 7, _node.length);
} else { } else {
_node = _node =
_node.substring(0, 5) + _node.substring(0, 5) +
` class="${darkTextClass}" ` + ` class="${darkTextClass}" ` +
_node.substring(5, _node.length); _node.substring(5, _node.length);
}
} }
} }
@ -333,10 +336,15 @@ function transformPlugin(): Plugin {
} }
// 获取暗黑模式类名 // 获取暗黑模式类名
const darkClassNames = classNames.filter((name) => let darkClassNames = classNames.filter((name) =>
name.startsWith("dark-colon-"), name.startsWith("dark-colon-"),
); );
// 插件模式,不支持 dark:
if (config.uniapp.isPlugin) {
darkClassNames = [];
}
// 生成暗黑模式类名的动态绑定 // 生成暗黑模式类名的动态绑定
const darkClassContent = darkClassNames const darkClassContent = darkClassNames
.map((name) => { .map((name) => {
@ -388,10 +396,12 @@ function transformPlugin(): Plugin {
modifiedCode += '<script lang="ts" setup></script>'; modifiedCode += '<script lang="ts" setup></script>';
} }
modifiedCode = addScriptContent( if (!config.uniapp.isPlugin) {
modifiedCode, modifiedCode = addScriptContent(
"\nimport { isDark as __isDark } from '@/cool';", modifiedCode,
); "\nimport { isDark as __isDark } from '@/cool';",
);
}
} }
// 清理空的类名绑定 // 清理空的类名绑定

View File

@ -94,7 +94,7 @@ export declare namespace Config {
// 应用类型 // 应用类型
type: Type; type: Type;
// 代理配置 // 代理配置
proxy: any; proxy?: any;
// Eps // Eps
eps?: Partial<Config.Eps>; eps?: Partial<Config.Eps>;
// 是否开启演示模式 // 是否开启演示模式
@ -119,6 +119,10 @@ export declare namespace Config {
// 暗黑模式文本类名 // 暗黑模式文本类名
darkTextClass?: string; darkTextClass?: string;
}; };
// uniapp X
uniapp?: {
isPlugin?: boolean;
};
// 是否纯净版 // 是否纯净版
clean?: boolean; clean?: boolean;
} }