diff --git a/packages/vite-plugin/dist/config.d.ts b/packages/vite-plugin/dist/config.d.ts index 1d9a006..052e1c3 100644 --- a/packages/vite-plugin/dist/config.d.ts +++ b/packages/vite-plugin/dist/config.d.ts @@ -31,5 +31,8 @@ export declare const config: { rpxRatio: number; darkTextClass: string; }; + uniapp: { + isPlugin: boolean; + }; clean: boolean; }; diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 876e8b1..4812833 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -56,6 +56,9 @@ rpxRatio: 2, darkTextClass: "dark:text-surface-50", }, + uniapp: { + isPlugin: false, + }, clean: false, }; @@ -195,26 +198,6 @@ function error(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 类型定义 @@ -2169,27 +2152,30 @@ if (typeof window !== 'undefined') { if (_node.startsWith("", ""); } - // 为 text 节点添加暗黑模式文本颜色 - if (!_node.includes(darkTextClass) && _node.startsWith("= 0) { - if (_node[classIndex - 1] == ":") { - classIndex = _node.lastIndexOf("class="); + // uniappx 插件模式 + if (!config.uniapp.isPlugin) { + // 为 text 节点添加暗黑模式文本颜色 + if (!_node.includes(darkTextClass) && _node.startsWith("= 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="{}"` + ">"; } // 获取暗黑模式类名 - 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 .map((name) => { @@ -2245,7 +2235,9 @@ if (typeof window !== 'undefined') { if (!modifiedCode.includes("= 0) { - // 颜色值 - ctx["color"] = getTailwindColor(); + ctx["theme"] = theme || {}; + // 颜色值 + ctx["color"] = getTailwindColor(); + if (!ctx.subPackages) { + ctx.subPackages = []; + } + if (!ctx.tabBar) { + ctx.tabBar = {}; } // 安全字符映射 ctx["SAFE_CHAR_MAP_LOCALE"] = []; for (const i in SAFE_CHAR_MAP_LOCALE) { 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({})!", `const ctx = parse(${ctxCode})!`); } // if (id.includes("/cool/service/index.ts")) { // const eps = await createEps(); @@ -2473,6 +2466,10 @@ if (typeof window !== 'undefined') { if (config.type == "uniapp-x") { config.eps.enable = false; } + // uniapp + if (options.uniapp) { + lodash.assign(config.uniapp, options.uniapp); + } // tailwind if (options.tailwind) { lodash.assign(config.tailwind, options.tailwind); diff --git a/packages/vite-plugin/package.json b/packages/vite-plugin/package.json index bca33fb..b928694 100644 --- a/packages/vite-plugin/package.json +++ b/packages/vite-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cool-vue/vite-plugin", - "version": "8.2.10", + "version": "8.2.12", "description": "cool-admin、cool-uni builder", "types": "./dist/index.d.ts", "main": "/dist/index.js", diff --git a/packages/vite-plugin/src/config.ts b/packages/vite-plugin/src/config.ts index cfa839a..1425491 100644 --- a/packages/vite-plugin/src/config.ts +++ b/packages/vite-plugin/src/config.ts @@ -53,5 +53,8 @@ export const config = { rpxRatio: 2, darkTextClass: "dark:text-surface-50", }, + uniapp: { + isPlugin: false, + }, clean: false, }; diff --git a/packages/vite-plugin/src/index.ts b/packages/vite-plugin/src/index.ts index b1fecb0..8eb6188 100644 --- a/packages/vite-plugin/src/index.ts +++ b/packages/vite-plugin/src/index.ts @@ -60,6 +60,11 @@ export function cool(options: Config.Options) { config.eps.enable = false; } + // uniapp + if (options.uniapp) { + assign(config.uniapp, options.uniapp); + } + // tailwind if (options.tailwind) { assign(config.tailwind, options.tailwind); diff --git a/packages/vite-plugin/src/uniapp-x/code.ts b/packages/vite-plugin/src/uniapp-x/code.ts index 5c95568..072a176 100644 --- a/packages/vite-plugin/src/uniapp-x/code.ts +++ b/packages/vite-plugin/src/uniapp-x/code.ts @@ -100,18 +100,21 @@ export function codePlugin(): Plugin[] { if (id.includes("/cool/ctx/index.ts")) { const ctx = await createCtx(); - // 版本 - const version = getVersion(); - // 主题配置 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]]); } + 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 = {}", - `const ctx = ${JSON.stringify(ctx, null, 4)}`, + "const ctx = parse({})!", + `const ctx = parse(${ctxCode})!`, ); } diff --git a/packages/vite-plugin/src/uniapp-x/tailwind.ts b/packages/vite-plugin/src/uniapp-x/tailwind.ts index 3916c7d..89b03c3 100644 --- a/packages/vite-plugin/src/uniapp-x/tailwind.ts +++ b/packages/vite-plugin/src/uniapp-x/tailwind.ts @@ -287,28 +287,31 @@ function transformPlugin(): Plugin { _node = _node.replace("/>", ""); } - // 为 text 节点添加暗黑模式文本颜色 - if (!_node.includes(darkTextClass) && _node.startsWith("= 0) { - if (_node[classIndex - 1] == ":") { - classIndex = _node.lastIndexOf("class="); + // 处理动态 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); + } } } @@ -333,10 +336,15 @@ function transformPlugin(): Plugin { } // 获取暗黑模式类名 - const darkClassNames = classNames.filter((name) => + let darkClassNames = classNames.filter((name) => name.startsWith("dark-colon-"), ); + // 插件模式,不支持 dark: + if (config.uniapp.isPlugin) { + darkClassNames = []; + } + // 生成暗黑模式类名的动态绑定 const darkClassContent = darkClassNames .map((name) => { @@ -388,10 +396,12 @@ function transformPlugin(): Plugin { modifiedCode += ''; } - modifiedCode = addScriptContent( - modifiedCode, - "\nimport { isDark as __isDark } from '@/cool';", - ); + if (!config.uniapp.isPlugin) { + modifiedCode = addScriptContent( + modifiedCode, + "\nimport { isDark as __isDark } from '@/cool';", + ); + } } // 清理空的类名绑定 diff --git a/packages/vite-plugin/types/index.d.ts b/packages/vite-plugin/types/index.d.ts index 335e68b..42814b3 100644 --- a/packages/vite-plugin/types/index.d.ts +++ b/packages/vite-plugin/types/index.d.ts @@ -94,7 +94,7 @@ export declare namespace Config { // 应用类型 type: Type; // 代理配置 - proxy: any; + proxy?: any; // Eps eps?: Partial; // 是否开启演示模式 @@ -119,6 +119,10 @@ export declare namespace Config { // 暗黑模式文本类名 darkTextClass?: string; }; + // uniapp X + uniapp?: { + isPlugin?: boolean; + }; // 是否纯净版 clean?: boolean; }