插件nacos-federation增加对vite版本的支持,简化代码!
This commit is contained in:
@@ -4,20 +4,11 @@ import vueJsx from '@vitejs/plugin-vue-jsx'
|
||||
import vueDevTools from 'vite-plugin-vue-devtools'
|
||||
import federation from '@originjs/vite-plugin-federation';
|
||||
import { fileURLToPath, URL } from "node:url";
|
||||
import { nacosServiceManage } from "nacos-federation";
|
||||
|
||||
import { nacosRegVitePlugin } from "nacos-federation";
|
||||
|
||||
export default defineConfig(async ({ mode }) => {
|
||||
|
||||
const env = loadEnv(mode, process.cwd());
|
||||
|
||||
await nacosServiceManage.create({
|
||||
serverList: env.VITE_NACOS_ADDRESS,
|
||||
serviceName: env.VITE_SERVER_NAME,
|
||||
port: Number(env.VITE_SERVER_PORT)
|
||||
});
|
||||
// const sharedUrl = await nacos.find(env.VITE_MODEL_SHARED_NAME)
|
||||
|
||||
return {
|
||||
base: `/${ env.VITE_SERVER_NAME }/`,
|
||||
server: {
|
||||
@@ -27,6 +18,7 @@ export default defineConfig(async ({ mode }) => {
|
||||
cors: true
|
||||
},
|
||||
plugins: [
|
||||
nacosRegVitePlugin(),
|
||||
federation({
|
||||
name: env.VITE_SERVER_NAME,
|
||||
remotes: {
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
import { defineConfig, loadEnv, UserConfig } from 'vite'
|
||||
import federation from "@originjs/vite-plugin-federation";
|
||||
import { nacosServiceManage } from "nacos-federation";
|
||||
import { nacosRegVitePlugin } from "nacos-federation";
|
||||
|
||||
export default defineConfig(async ({ mode }): Promise<UserConfig> => {
|
||||
const env = loadEnv(mode, process.cwd());
|
||||
await nacosServiceManage.create({
|
||||
serverList: env.VITE_NACOS_ADDRESS,
|
||||
serviceName: env.VITE_SERVER_NAME,
|
||||
port: Number(env.VITE_SERVER_PORT)
|
||||
});
|
||||
|
||||
return {
|
||||
base: `/${ env.VITE_SERVER_NAME }/`,
|
||||
@@ -19,6 +14,7 @@ export default defineConfig(async ({ mode }): Promise<UserConfig> => {
|
||||
cors: true
|
||||
},
|
||||
plugins: [
|
||||
nacosRegVitePlugin(),
|
||||
federation({
|
||||
name: env.VITE_SERVER_NAME,
|
||||
exposes: {
|
||||
|
||||
@@ -3,16 +3,15 @@ import path from 'path'
|
||||
import { defineConfig, loadEnv } from 'vite'
|
||||
import vueJsx from '@vue3-oop/plugin-vue-jsx'
|
||||
import federation from '@originjs/vite-plugin-federation';
|
||||
import { nacosServiceManage } from "nacos-federation";
|
||||
import { nacosRegVitePlugin } from "nacos-federation";
|
||||
|
||||
export default defineConfig(async ({ mode }) => {
|
||||
const env = loadEnv(mode, process.cwd());
|
||||
const nacos = await nacosServiceManage.create({
|
||||
serverList: env.VITE_NACOS_ADDRESS,
|
||||
serviceName: env.VITE_SERVER_NAME,
|
||||
port: Number(env.VITE_SERVER_PORT)
|
||||
});
|
||||
|
||||
// const nacos = await nacosServiceManage.create({
|
||||
// serverList: env.VITE_NACOS_ADDRESS,
|
||||
// serviceName: env.VITE_SERVER_NAME,
|
||||
// port: Number(env.VITE_SERVER_PORT)
|
||||
// });
|
||||
// const productUrl = await nacos.find(env.VITE_MODEL_PRODUCT_NAME)
|
||||
// const sharedUrl = await nacos.find(env.VITE_MODEL_SHARED_NAME)
|
||||
|
||||
@@ -23,6 +22,7 @@ export default defineConfig(async ({ mode }) => {
|
||||
strictPort: true,
|
||||
},
|
||||
plugins: [
|
||||
nacosRegVitePlugin(),
|
||||
vueJsx(),
|
||||
federation({
|
||||
name: env.VITE_SERVER_NAME,
|
||||
@@ -32,7 +32,7 @@ export default defineConfig(async ({ mode }) => {
|
||||
// shared: `${ sharedUrl }/assets/remoteEntry.js`
|
||||
|
||||
// 实现:限流与熔断,重试,IP 白名单,压缩等
|
||||
// 基于 traefik 网关,由网关去寻找 微前端模块的地址
|
||||
// 基于 c 网关,由网关去寻找 微前端模块的地址
|
||||
// 网关的地址是 nodejs脚本 定时10秒访问Nacos api 后,生成Traefik配置文件dynamic.yml,
|
||||
// 并把配置实时同步到Traefik中,如果 xxx微前端模块停止运行那么就会从 Nacos服务中注销,
|
||||
// 那么dynamic.yml配置文件就会自动删减被下线的网关映射
|
||||
|
||||
@@ -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