Release v0.1.2
This commit is contained in:
@@ -1,17 +1,69 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath, URL } from "node:url";
|
||||
import { defineConfig } from "vite";
|
||||
import { defineConfig, type Plugin } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
const linkedModulePackages = [
|
||||
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: [react()],
|
||||
plugins: [govoplanInstalledModulesPlugin(), react()],
|
||||
optimizeDeps: {
|
||||
exclude: linkedModulePackages
|
||||
exclude: availableWebModuleSpecifiers()
|
||||
},
|
||||
resolve: {
|
||||
preserveSymlinks: true,
|
||||
|
||||
Reference in New Issue
Block a user