Files
govoplan-core/webui/vite.config.ts
2026-06-25 19:58:20 +02:00

97 lines
3.2 KiB
TypeScript

import { existsSync } from "node:fs";
import { createRequire } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath, URL } from "node:url";
import { defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
const require = createRequire(import.meta.url);
const webuiRoot = dirname(fileURLToPath(import.meta.url));
const installedModulesVirtualId = "virtual:govoplan-installed-modules";
const resolvedInstalledModulesVirtualId = `\0${installedModulesVirtualId}`;
const defaultWebModulePackages = [
"@govoplan/campaign-webui",
"@govoplan/files-webui",
"@govoplan/mail-webui"
];
function configuredWebModulePackages(): string[] {
const configured = process.env.GOVOPLAN_WEBUI_MODULE_PACKAGES;
if (configured === undefined) return defaultWebModulePackages;
return configured.split(",").map((specifier) => specifier.trim()).filter(Boolean);
}
function packageDirectory(specifier: string): string {
const parts = specifier.startsWith("@") ? specifier.split("/") : [specifier];
return join(webuiRoot, "node_modules", ...parts);
}
function packageInstalled(specifier: string): boolean {
if (existsSync(join(packageDirectory(specifier), "package.json"))) return true;
try {
require.resolve(specifier);
return true;
} catch {
return false;
}
}
function availableWebModuleSpecifiers(): string[] {
return configuredWebModulePackages().filter(packageInstalled);
}
function govoplanInstalledModulesPlugin(): Plugin {
return {
name: "govoplan-installed-modules",
resolveId(id: string) {
if (id === installedModulesVirtualId) return resolvedInstalledModulesVirtualId;
return null;
},
load(id: string) {
if (id !== resolvedInstalledModulesVirtualId) return null;
const imports = availableWebModuleSpecifiers();
return [
...imports.map((specifier, index) => `import module${index} from ${JSON.stringify(specifier)};`),
`export const installedWebModules = [${imports.map((_specifier, index) => `module${index}`).join(", ")}];`,
"export default installedWebModules;"
].join("\n");
}
};
}
export default defineConfig({
plugins: [govoplanInstalledModulesPlugin(), react()],
optimizeDeps: {
exclude: availableWebModuleSpecifiers()
},
resolve: {
preserveSymlinks: true,
dedupe: ["react", "react-dom", "react-router-dom"],
alias: [
{ find: "@govoplan/core-webui/app", replacement: fileURLToPath(new URL("./src/app.ts", import.meta.url)) },
{ find: "@govoplan/core-webui", replacement: fileURLToPath(new URL("./src/index.ts", import.meta.url)) }
]
},
server: {
fs: {
allow: [
fileURLToPath(new URL('.', import.meta.url)),
fileURLToPath(new URL('../../govoplan-files/webui', import.meta.url)),
fileURLToPath(new URL('../../govoplan-mail/webui', import.meta.url)),
fileURLToPath(new URL('../../govoplan-campaign/webui', import.meta.url))
]
},
proxy: {
"/api": {
target: process.env.VITE_API_PROXY_TARGET || "http://127.0.0.1:8000",
changeOrigin: true
},
"/health": {
target: process.env.VITE_API_PROXY_TARGET || "http://127.0.0.1:8000",
changeOrigin: true
}
}
}
});