优化全局样式和 element ui框架,修改nacos-federation,增加打包依赖分析等

This commit is contained in:
编码猿
2025-09-26 01:50:49 +08:00
parent 537c7ca60e
commit 174af7efc3
43 changed files with 15055 additions and 377212 deletions

View File

@@ -12,6 +12,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"chalk": "^5.6.2",
"nacos": "^2.6.0"
},
"devDependencies": {

View File

@@ -1,9 +1,9 @@
import { NacosConfigClient, NacosNamingClient } from 'nacos';
import { formatServiceUrl, getLocalIP } from "./utils";
import chalk from 'chalk';
const logger = console;
export interface RegisterOptions {
serverList: string;
/** 服务名称(如 'main-app' */
@@ -105,6 +105,13 @@ export class nacosServiceManage {
});
}
async getConfig(dataId: string, group: string) {
const res = await this.configClient.getConfig(dataId, group)
console.log()
console.log(`${ chalk.blue('[nacos-federation] 配置发现:') }\n ${ chalk.green('➜') } ${ chalk.green(dataId) } 的配置为: ${ chalk.green(res) }`)
return res;
}
/**
* 注册服务到Nacos并返回客户端实例
*/
@@ -114,8 +121,8 @@ export class nacosServiceManage {
// @ts-ignore
await this.nacosClient.registerInstance(serviceName, { port, ip })
logger.log()
logger.log("[nacos-federation] 服务注册:")
logger.log(`服务 ${ serviceName } 已注册到配置中心:${ formatServiceUrl(ip, port) }`);
logger.log(chalk.blue("[nacos-federation] 服务注册:"))
logger.log(` ${ chalk.green('➜') } ${ chalk.green(serviceName) } (${ formatServiceUrl(ip, port) }) 已注册到配置中心!`);
}

View File

@@ -1,5 +1,8 @@
import { loadEnv, Plugin, UserConfig } from 'vite';
import { nacosServiceManage, RegisterOptions } from './nacosServiceManage';
import chalk from 'chalk';
const log = console.log;
export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
let nacosInstance: nacosServiceManage | null = null;
@@ -21,13 +24,6 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
},
async configResolved(config) {
// 安全检查:确保 config.define 存在
if (!config.define) {
// @ts-ignore
config.define = {};
}
// 从环境变量和配置中获取Nacos配置环境变量优先级更高
const env = config.env;
const registerOptions: RegisterOptions = {
serverList: env.VITE_NACOS_ADDRESS || options?.serverList!,
@@ -51,13 +47,11 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
// 初始化Nacos实例
nacosInstance = await nacosServiceManage.create(registerOptions);
const baseUrl = await nacosInstance.configClient.getConfig("baseUrl", "DEFAULT_GROUP")
const baseUrl = await nacosInstance.getConfig("baseUrl", "DEFAULT_GROUP")
const baseUrlConfig = baseUrl ? JSON.parse(baseUrl) : {};
console.log()
console.log("[nacos-federation] 配置发现:")
console.log("从Nacos配置中心获得接口地址: ", baseUrlConfig)
console.log()
baseUrlConfig.httpTimeOut = await nacosInstance.getConfig("httpTimeOut", "DEFAULT_GROUP")
const customizeEnv: Record<string, string> = {};
@@ -65,6 +59,7 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
const envKey = `import.meta.env.VITE_${ key.toUpperCase() }`;
customizeEnv[envKey] = JSON.stringify(baseUrlConfig[key]);
});
// @ts-ignore
Object.assign(config.define, customizeEnv);
Logs(env)
@@ -86,16 +81,18 @@ function Logs(env: Record<string, any>) {
}
}
moduleName.length == 0 ? moduleName.push('无') : null
console.log()
console.log(dedentTemplateString(`
微服务工具:
-> 1Naco 配置中心: ${ env.VITE_PREFIX }${ env.VITE_NACOS_ADDRESS }
-> 2Traefik 网关: ${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }
${ chalk.blue.bold('微服务工具:') }
${ chalk.green('➜') } Naco 配置中心: ${ env.VITE_PREFIX }${ env.VITE_NACOS_ADDRESS }
${ chalk.green('➜') } Traefik 网关: ${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }
微前端信息:
-> 主模块:${ env.VITE_SERVER_NAME }
${ chalk.blue.bold('微前端信息:') }
${ chalk.green('➜') } 主模块:${ chalk.green(env.VITE_SERVER_NAME) }
调用模块:
-> [ ${ moduleName } ]
${ chalk.blue.bold('调用模块:') }
${ chalk.green('➜') } [ ${ chalk.yellow(moduleName) } ]
`))
console.log()
}