import { defineConfig, HmrContext } from "vite"; import react from "@vitejs/plugin-react"; import { resolve } from "path"; import { readFileSync } from "fs"; import { imagetools } from "vite-imagetools"; const host = process.env.TAURI_DEV_HOST; // Read version from workspace Cargo.toml function getVersionFromCargoToml(): string { const cargoToml = readFileSync(resolve(__dirname, "../Cargo.toml"), "utf-8"); const versionMatch = cargoToml.match(/^\[workspace\.package\][\s\S]*?^version\s*=\s*"(.+?)"$/m); if (!versionMatch) { throw new Error("Failed to find version in workspace Cargo.toml"); } return versionMatch[1]; } const fullReloadAlways = { name: "full-reload-always", handleHotUpdate(context: HmrContext): void { context.server.ws.send({ type: "full-reload" }); }, }; // https://vite.dev/config/ export default defineConfig(({ mode }) => ({ base: process.env.GITHUB_PAGES === "true" ? "/borde.rs/" : "/", plugins: [ react(), imagetools(), fullReloadAlways, // Plugin to prevent .meta files from falling back to index.html { name: "block-meta-fallback", configureServer(server) { server.middlewares.use((req, res, next) => { if (req.url?.endsWith(".meta")) { res.statusCode = 404; res.end("Not Found"); return; } next(); }); }, }, ], define: { __DESKTOP__: JSON.stringify(mode !== "browser"), __APP_VERSION__: JSON.stringify(getVersionFromCargoToml()), }, resolve: { alias: { "@": resolve(__dirname, "src"), "@wasm": resolve(__dirname, "pkg"), }, }, build: { outDir: mode === "browser" ? "dist/browser" : "dist", }, // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build` // // 1. prevent Vite from obscuring rust errors clearScreen: false, // 2. tauri expects a fixed port, fail if that port is not available server: { port: 1420, strictPort: true, host: host || false, hmr: host ? { protocol: "ws", host, port: 1421, } : undefined, watch: { // 3. tell Vite to ignore watching `src-tauri` ignored: ["**/crates/borders-desktop/**"], }, // Add headers for SharedArrayBuffer support in browser mode headers: mode === "browser" ? { "Cross-Origin-Opener-Policy": "same-origin", "Cross-Origin-Embedder-Policy": "require-corp", } : undefined, }, }));