mirror of
https://github.com/cool-team-official/cool-admin-vue.git
synced 2025-12-11 21:12:50 +00:00
更新 uniappx 插件
This commit is contained in:
parent
f670008cb4
commit
5535535439
3
packages/vite-plugin/dist/config.d.ts
vendored
3
packages/vite-plugin/dist/config.d.ts
vendored
@ -31,5 +31,8 @@ export declare const config: {
|
|||||||
rpxRatio: number;
|
rpxRatio: number;
|
||||||
darkTextClass: string;
|
darkTextClass: string;
|
||||||
};
|
};
|
||||||
|
uniapp: {
|
||||||
|
isPlugin: boolean;
|
||||||
|
};
|
||||||
clean: boolean;
|
clean: boolean;
|
||||||
};
|
};
|
||||||
|
|||||||
59
packages/vite-plugin/dist/index.js
vendored
59
packages/vite-plugin/dist/index.js
vendored
@ -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,6 +2152,8 @@ if (typeof window !== 'undefined') {
|
|||||||
if (_node.startsWith("<input")) {
|
if (_node.startsWith("<input")) {
|
||||||
_node = _node.replace("/>", "</input>");
|
_node = _node.replace("/>", "</input>");
|
||||||
}
|
}
|
||||||
|
// uniappx 插件模式
|
||||||
|
if (!config.uniapp.isPlugin) {
|
||||||
// 为 text 节点添加暗黑模式文本颜色
|
// 为 text 节点添加暗黑模式文本颜色
|
||||||
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) {
|
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) {
|
||||||
let classIndex = _node.indexOf("class=");
|
let classIndex = _node.indexOf("class=");
|
||||||
@ -2192,6 +2177,7 @@ if (typeof window !== 'undefined') {
|
|||||||
_node.substring(5, _node.length);
|
_node.substring(5, _node.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 获取所有类名
|
// 获取所有类名
|
||||||
const classNames = getClassNames(_node);
|
const classNames = getClassNames(_node);
|
||||||
// 转换 Tailwind 类名为安全类名
|
// 转换 Tailwind 类名为安全类名
|
||||||
@ -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,8 +2235,10 @@ if (typeof window !== 'undefined') {
|
|||||||
if (!modifiedCode.includes("<script")) {
|
if (!modifiedCode.includes("<script")) {
|
||||||
modifiedCode += '<script lang="ts" setup></script>';
|
modifiedCode += '<script lang="ts" setup></script>';
|
||||||
}
|
}
|
||||||
|
if (!config.uniapp.isPlugin) {
|
||||||
modifiedCode = addScriptContent(modifiedCode, "\nimport { isDark as __isDark } from '@/cool';");
|
modifiedCode = addScriptContent(modifiedCode, "\nimport { isDark as __isDark } from '@/cool';");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 清理空的类名绑定
|
// 清理空的类名绑定
|
||||||
modifiedCode = modifiedCode
|
modifiedCode = modifiedCode
|
||||||
.replaceAll(':class="{}"', "")
|
.replaceAll(':class="{}"', "")
|
||||||
@ -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);
|
||||||
|
|||||||
@ -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",
|
||||||
|
|||||||
@ -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,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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})!`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -287,6 +287,8 @@ function transformPlugin(): Plugin {
|
|||||||
_node = _node.replace("/>", "</input>");
|
_node = _node.replace("/>", "</input>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// uniappx 插件模式
|
||||||
|
if (!config.uniapp.isPlugin) {
|
||||||
// 为 text 节点添加暗黑模式文本颜色
|
// 为 text 节点添加暗黑模式文本颜色
|
||||||
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) {
|
if (!_node.includes(darkTextClass) && _node.startsWith("<text")) {
|
||||||
let classIndex = _node.indexOf("class=");
|
let classIndex = _node.indexOf("class=");
|
||||||
@ -311,6 +313,7 @@ function transformPlugin(): Plugin {
|
|||||||
_node.substring(5, _node.length);
|
_node.substring(5, _node.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获取所有类名
|
// 获取所有类名
|
||||||
const classNames = getClassNames(_node);
|
const classNames = getClassNames(_node);
|
||||||
@ -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,11 +396,13 @@ function transformPlugin(): Plugin {
|
|||||||
modifiedCode += '<script lang="ts" setup></script>';
|
modifiedCode += '<script lang="ts" setup></script>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!config.uniapp.isPlugin) {
|
||||||
modifiedCode = addScriptContent(
|
modifiedCode = addScriptContent(
|
||||||
modifiedCode,
|
modifiedCode,
|
||||||
"\nimport { isDark as __isDark } from '@/cool';",
|
"\nimport { isDark as __isDark } from '@/cool';",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 清理空的类名绑定
|
// 清理空的类名绑定
|
||||||
modifiedCode = modifiedCode
|
modifiedCode = modifiedCode
|
||||||
|
|||||||
6
packages/vite-plugin/types/index.d.ts
vendored
6
packages/vite-plugin/types/index.d.ts
vendored
@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user