插件nacos-federation增加对vite版本的支持,简化代码!
This commit is contained in:
@@ -15,8 +15,9 @@
|
||||
"nacos": "^2.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.19.17",
|
||||
"typescript": "~5.8.3"
|
||||
"@types/node": "^24.5.2",
|
||||
"typescript": "~5.8.3",
|
||||
"vite": "^7.1.6"
|
||||
},
|
||||
"keywords": [
|
||||
"nacos",
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
// 从插件实现文件导入
|
||||
export * from './vite-plugin-nacos';
|
||||
export * from './nacosServiceManage';
|
||||
export * from './utils';
|
||||
export * from './utils';
|
||||
45
front-end/nacos-federation/src/vite-plugin-nacos.ts
Normal file
45
front-end/nacos-federation/src/vite-plugin-nacos.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Plugin, ResolvedConfig } from 'vite';
|
||||
import { nacosServiceManage, RegisterOptions } from './nacosServiceManage';
|
||||
|
||||
export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
|
||||
let nacosInstance: nacosServiceManage | null = null;
|
||||
|
||||
return {
|
||||
name: 'vite-plugin-nacos',
|
||||
async configResolved(config: ResolvedConfig) {
|
||||
// 从环境变量和配置中获取Nacos配置(环境变量优先级更高)
|
||||
const env = config.env;
|
||||
|
||||
console.log("you env: ", env)
|
||||
const registerOptions: RegisterOptions = {
|
||||
serverList: env.VITE_NACOS_ADDRESS || options?.serverList!,
|
||||
serviceName: env.VITE_SERVER_NAME || options?.serviceName!,
|
||||
port: Number(env.VITE_SERVER_PORT || options?.port),
|
||||
namespace: options?.namespace || 'public'
|
||||
};
|
||||
|
||||
// 验证必要配置
|
||||
if (!registerOptions.serverList) {
|
||||
throw new Error('[vite-plugin-nacos] 缺少Nacos服务列表配置,请设置VITE_NACOS_ADDRESS环境变量或插件选项');
|
||||
}
|
||||
|
||||
if (!registerOptions.serviceName) {
|
||||
throw new Error('[vite-plugin-nacos] 缺少服务名称配置,请设置VITE_SERVER_NAME环境变量或插件选项');
|
||||
}
|
||||
if (!registerOptions.port) {
|
||||
throw new Error('[vite-plugin-nacos] 缺少服务端口配置,请设置VITE_SERVER_PORT环境变量或插件选项');
|
||||
}
|
||||
|
||||
// 初始化Nacos实例
|
||||
nacosInstance = await nacosServiceManage.create(registerOptions);
|
||||
|
||||
},
|
||||
async closeBundle() {
|
||||
// // 服务关闭时注销Nacos实例
|
||||
// if (nacosInstance) {
|
||||
// await nacosInstance.destroy();
|
||||
// }
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user