/// import { build as viteBuild, defineConfig, type Plugin } from "vite"; import react from "@vitejs/plugin-react"; import path from "node:path"; import { fileURLToPath } from "node:url"; import process from "node:process"; const host = process.env.TAURI_DEV_HOST; const webServer = process.env.OPENIDEA_WEB_SERVER; const VIRTUAL_MDX_GUEST = "virtual:mdx-guest-bundle"; const RESOLVED_MDX_GUEST = `\0${VIRTUAL_MDX_GUEST}`; function mdxGuestBundlePlugin(): Plugin { let cachedBundlePromise: Promise | null = null; const guestDir = path.resolve(import.meta.dirname, "src/features/viewers/markdown"); const guestEntry = path.join(guestDir, "mdxGuest.tsx"); const buildGuestIife = () => { if (!cachedBundlePromise) { cachedBundlePromise = viteBuild({ configFile: false, mode: "production", logLevel: "silent", oxc: { jsx: { runtime: "automatic", development: false, }, }, define: { "process.env.NODE_ENV": JSON.stringify("production"), }, build: { write: false, target: "safari15.4", minify: "oxc", sourcemap: false, lib: { entry: guestEntry, name: "__OpenIdeaMdxGuest", formats: ["iife"], fileName: () => "mdx-guest.js", }, }, }).then((output) => { const roll = Array.isArray(output) ? output[0] : output; if (!roll || !("output" in roll)) { throw new Error("Failed to bundle mdxGuest.tsx"); } const chunk = roll.output.find((item) => item.type === "chunk"); if (!chunk) throw new Error("No JS chunk produced for mdxGuest.tsx"); return `window.__OPENIDEA_MDX_SANDBOX__ = true;\n${chunk.code}`; }); } return cachedBundlePromise; }; return { name: "openidea:mdx-guest-bundle", resolveId(id) { if (id === VIRTUAL_MDX_GUEST) return RESOLVED_MDX_GUEST; return null; }, handleHotUpdate(ctx) { if (ctx.file.startsWith(`${guestDir}${path.sep}`) || ctx.file === guestEntry) { cachedBundlePromise = null; } }, async load(id) { if (id !== RESOLVED_MDX_GUEST) return null; const code = await buildGuestIife(); return `export default ${JSON.stringify(code)};`; }, }; } export default defineConfig(() => ({ // Relative asset URLs keep both desktop bundles and reverse-proxied web deployments subpath-agnostic. base: "./", plugins: [react(), mdxGuestBundlePlugin()], resolve: { alias: [ { find: "@", replacement: fileURLToPath(new URL("./src", import.meta.url)) }, // `invoke` with remote-development routing (src/platform/tauriCore.ts). { find: /^@tauri-apps\/api\/core$/, replacement: fileURLToPath(new URL("./src/platform/tauriCore.ts", import.meta.url)), }, ], }, // Tauri: keep Rust errors visible, fixed port, don't watch the backend. clearScreen: false, server: { port: 1420, strictPort: true, host: host || false, hmr: host ? { protocol: "ws", host } : undefined, ...(webServer ? { proxy: { "/api": { target: webServer, ws: true, }, }, } : {}), watch: { ignored: [ "**/src-tauri/**", "**/crates/**", "**/target/**", // The packages' sources and builds (not `src/features/plugins`). `${fileURLToPath(new URL("./plugins", import.meta.url))}/**`, ], }, }, // The app's own entry only: plugins' web pages (`plugins/**/web-src`) are // separate projects with their own dependencies, never part of this bundle. optimizeDeps: { entries: ["index.html"] }, build: { target: "safari15.4", sourcemap: true, }, test: { environment: "jsdom", globals: false, setupFiles: ["./src/test/setup.ts"], include: ["src/**/*.test.{ts,tsx}", "perf/**/*.test.ts", "evals/**/*.test.ts", "scripts/**/*.test.ts"], }, }));