增加控制台日志,方便访问各种服务
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc -w",
|
||||||
"prepublishOnly": "npm run build"
|
"prepublishOnly": "npm run build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -113,7 +113,9 @@ export class nacosServiceManage {
|
|||||||
const ip = getLocalIP();
|
const ip = getLocalIP();
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
await this.nacosClient.registerInstance(serviceName, { port, ip })
|
await this.nacosClient.registerInstance(serviceName, { port, ip })
|
||||||
logger.log(`[nacos-federation] 服务 ${ serviceName } 已注册:${ formatServiceUrl(ip, port) }`);
|
logger.log()
|
||||||
|
logger.log("[nacos-federation] 服务注册:")
|
||||||
|
logger.log(`服务 ${ serviceName } 已注册到配置中心:${ formatServiceUrl(ip, port) }`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,14 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
|||||||
async config(userConfig: UserConfig, { mode }) {
|
async config(userConfig: UserConfig, { mode }) {
|
||||||
// 修改base,增加资源来源地址,修复资源错误加载的问题
|
// 修改base,增加资源来源地址,修复资源错误加载的问题
|
||||||
const env = loadEnv(mode, process.cwd());
|
const env = loadEnv(mode, process.cwd());
|
||||||
|
let baseUrl = ""
|
||||||
|
// 主模块 main-app 不加 base 前缀,其他模块都加,
|
||||||
|
// 也是为了命中 traefik 路由代理
|
||||||
|
if (env.VITE_SERVER_NAME != "main-app") {
|
||||||
|
baseUrl = `${ env.VITE_SERVER_NAME }/`
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
base: `${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }/${ env.VITE_SERVER_NAME }/`,
|
base: `${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }/${ baseUrl }`,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async configResolved(config) {
|
async configResolved(config) {
|
||||||
@@ -20,12 +26,9 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
config.define = {};
|
config.define = {};
|
||||||
}
|
}
|
||||||
console.log("config.define: ", config.define)
|
|
||||||
|
|
||||||
// 从环境变量和配置中获取Nacos配置(环境变量优先级更高)
|
// 从环境变量和配置中获取Nacos配置(环境变量优先级更高)
|
||||||
const env = config.env;
|
const env = config.env;
|
||||||
console.log("you env: ", env)
|
|
||||||
|
|
||||||
const registerOptions: RegisterOptions = {
|
const registerOptions: RegisterOptions = {
|
||||||
serverList: env.VITE_NACOS_ADDRESS || options?.serverList!,
|
serverList: env.VITE_NACOS_ADDRESS || options?.serverList!,
|
||||||
serviceName: env.VITE_SERVER_NAME || options?.serviceName!,
|
serviceName: env.VITE_SERVER_NAME || options?.serviceName!,
|
||||||
@@ -35,14 +38,14 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
|||||||
|
|
||||||
// 验证必要配置
|
// 验证必要配置
|
||||||
if (!registerOptions.serverList) {
|
if (!registerOptions.serverList) {
|
||||||
throw new Error('[vite-plugin-nacos] 缺少Nacos服务列表配置,请设置VITE_NACOS_ADDRESS环境变量或插件选项');
|
throw new Error('[nacos-federation] 缺少Nacos服务列表配置,请设置VITE_NACOS_ADDRESS环境变量或插件选项');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!registerOptions.serviceName) {
|
if (!registerOptions.serviceName) {
|
||||||
throw new Error('[vite-plugin-nacos] 缺少服务名称配置,请设置VITE_SERVER_NAME环境变量或插件选项');
|
throw new Error('[nacos-federation] 缺少服务名称配置,请设置VITE_SERVER_NAME环境变量或插件选项');
|
||||||
}
|
}
|
||||||
if (!registerOptions.port) {
|
if (!registerOptions.port) {
|
||||||
throw new Error('[vite-plugin-nacos] 缺少服务端口配置,请设置VITE_SERVER_PORT环境变量或插件选项');
|
throw new Error('[nacos-federation] 缺少服务端口配置,请设置VITE_SERVER_PORT环境变量或插件选项');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化Nacos实例
|
// 初始化Nacos实例
|
||||||
@@ -50,7 +53,11 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
|||||||
|
|
||||||
const baseUrl = await nacosInstance.configClient.getConfig("baseUrl", "DEFAULT_GROUP")
|
const baseUrl = await nacosInstance.configClient.getConfig("baseUrl", "DEFAULT_GROUP")
|
||||||
const baseUrlConfig = baseUrl ? JSON.parse(baseUrl) : {};
|
const baseUrlConfig = baseUrl ? JSON.parse(baseUrl) : {};
|
||||||
console.log("[vite-plugin-nacos] 从Nacos配置发现获得接口地址: ", baseUrlConfig)
|
|
||||||
|
console.log()
|
||||||
|
console.log("[nacos-federation] 配置发现:")
|
||||||
|
console.log("从Nacos配置中心,获得接口地址: ", baseUrlConfig)
|
||||||
|
console.log()
|
||||||
|
|
||||||
|
|
||||||
const customizeEnv: Record<string, string> = {};
|
const customizeEnv: Record<string, string> = {};
|
||||||
@@ -58,9 +65,9 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
|||||||
const envKey = `import.meta.env.VITE_${ key.toUpperCase() }`;
|
const envKey = `import.meta.env.VITE_${ key.toUpperCase() }`;
|
||||||
customizeEnv[envKey] = JSON.stringify(baseUrlConfig[key]);
|
customizeEnv[envKey] = JSON.stringify(baseUrlConfig[key]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Object.assign(config.define, customizeEnv);
|
Object.assign(config.define, customizeEnv);
|
||||||
|
Logs(env)
|
||||||
},
|
},
|
||||||
async closeBundle() {
|
async closeBundle() {
|
||||||
// // 服务关闭时注销Nacos实例
|
// // 服务关闭时注销Nacos实例
|
||||||
@@ -71,3 +78,43 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Logs(env: Record<string, any>) {
|
||||||
|
const moduleName = []
|
||||||
|
for (const key in env) {
|
||||||
|
if (key.includes("VITE_MODEL_")) {
|
||||||
|
moduleName.push(env[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
moduleName.length == 0 ? moduleName.push('无') : null
|
||||||
|
console.log(dedentTemplateString(`
|
||||||
|
微服务工具:
|
||||||
|
-> 1:Naco 配置中心: ${ env.VITE_PREFIX }${ env.VITE_NACOS_ADDRESS }
|
||||||
|
-> 2:Traefik 网关: ${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }
|
||||||
|
|
||||||
|
微前端信息:
|
||||||
|
-> 主模块:${ env.VITE_SERVER_NAME }
|
||||||
|
|
||||||
|
调用模块:
|
||||||
|
-> [ ${ moduleName } ]
|
||||||
|
`))
|
||||||
|
console.log()
|
||||||
|
}
|
||||||
|
|
||||||
|
function dedentTemplateString(str: string) {
|
||||||
|
// 分割成行
|
||||||
|
const lines = str.split('\n');
|
||||||
|
|
||||||
|
// 移除首尾空行
|
||||||
|
if (lines[0].trim() === '') lines.shift();
|
||||||
|
if (lines[lines.length - 1].trim() === '') lines.pop();
|
||||||
|
|
||||||
|
// 找到最小缩进量
|
||||||
|
const minIndent = lines.reduce((min, line) => {
|
||||||
|
if (line.trim() === '') return min; // 跳过空行
|
||||||
|
const indent = line.match(/^\s*/)[0].length;
|
||||||
|
return indent < min ? indent : min;
|
||||||
|
}, Infinity);
|
||||||
|
|
||||||
|
// 移除统一缩进
|
||||||
|
return lines.map(line => line.slice(minIndent)).join('\n');
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user