Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

3.3 KB
Raw Blame
import { defineConfig } from "vite";
import { sveltekit } from "@sveltejs/kit/vite";
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";
import { execSync, spawnSync } from "child_process";
import { readFileSync, existsSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";

const host = process.env.TAURI_DEV_HOST;
const configDir = path.dirname(fileURLToPath(import.meta.url));

function getGitInfo() {
  try {
    return {
      hash: execSync("git rev-parse HEAD").toString().trim(),
      branch: execSync("git rev-parse --abbrev-ref HEAD").toString().trim(),
    };
  } catch {
    return { hash: "unknown", branch: "unknown" };
  }
}

function getVersion() {
  try {
    return JSON.parse(readFileSync("package.json", "utf8")).version;
  } catch {
    return "0.0.0";
  }
}

function i18nKeysPlugin({ strict } = { strict: false }) {
  return {
    name: "libreget-i18n-keys",
    buildStart() {
      const script = path.join(configDir, "scripts", "generate-i18n-keys.js");
      if (!existsSync(script)) {
        return;
      }
      const args = strict ? [script, "--strict"] : [script];
      const result = spawnSync(process.execPath, args, {
        cwd: configDir,
        stdio: "inherit",
      });
      if (result.status !== 0) {
        throw new Error(
          `generate-i18n-keys.js exited with code ${result.status}. ` +
            `Fix locale keys or set LIBREGET_I18N_STRICT=0 to bypass.`,
        );
      }
    },
  };
}

export default defineConfig(({ command }) => {
  const gitInfo = getGitInfo();
  const isBuild = command === "build";
  const strictEnv = process.env.LIBREGET_I18N_STRICT;
  const strictI18n =
    strictEnv === "1" ||
    (isBuild && strictEnv !== "0");

  return {
    plugins: [
      i18nKeysPlugin({ strict: strictI18n }),
      wasm(),
      topLevelAwait(),
      sveltekit(),
    ],

    // PlantUML / Graphviz packages ship WASM + workers that need special
    // handling. `optimizeDeps.exclude` keeps the dep optimizer out of the way
    // so vite-plugin-wasm gets to handle the wasm imports. The graphviz
    // package internally spawns a worker — Vite 6 requires worker.format=es
    // for code-splitting builds, otherwise it errors with "Invalid value 'iife'".
    optimizeDeps: {
      exclude: [
        "@kookyleo/plantuml-little-web",
        "@kookyleo/graphviz-anywhere-web",
      ],
    },
    worker: {
      format: "es",
    },

    define: {
      __COMMIT_HASH__: JSON.stringify(gitInfo.hash),
      __GIT_BRANCH__: JSON.stringify(gitInfo.branch),
      __APP_VERSION__: JSON.stringify(getVersion()),
      __BUILD_DATE__: JSON.stringify(new Date().toISOString().split("T")[0]),
    },

    // 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: ["**/src-tauri/**"],
      },
    },
  };
});