UNPKG

vite-plugin-tauri

Version:

Integrate Tauri in a Vite project to build cross-platform apps.

165 lines (163 loc) 5.11 kB
// src/utils.ts import { readFileSync } from "node:fs"; function getPackageJson() { return JSON.parse(readFileSync("package.json", "utf8")); } var GREEN = "\x1B[32m"; var BOLD = "\x1B[1m"; var DIM = "\x1B[2m"; var RESET = "\x1B[0m"; function confirm(msg) { return new Promise((resolve, reject) => { const question = `${GREEN}? ${RESET}${BOLD}${msg}${RESET} ${DIM}(Y/n)${RESET}`; process.stdout.write(question); process.stdin.setRawMode(true); process.stdin.once("data", (data) => { process.stdout.write(data.toString()); process.stdout.write("\n"); process.stdin.setRawMode(false); const key = data.toString(); if (key === "y" || key === "Y") { resolve(true); } else if (key === "n" || key === "N") { resolve(false); } else { process.exit(1); } }); }); } // src/index.ts import TauriCli from "@tauri-apps/cli"; import { getPackageInfoSync } from "local-pkg"; import path, { dirname } from "path"; import fg from "fast-glob"; function getTauriConfPath() { const tauriDepthEnv = process.env.TAURI_PATH_DEPTH; const deep = tauriDepthEnv ? parseInt(tauriDepthEnv) : 3; return fg.sync("**/(tauri.conf.(json|json5)|Tauri.toml)", { absolute: true, unique: true, ignore: ["**/node_modules/**", "**/target/**"], deep })[0]; } var tauriVersion = Number( getPackageInfoSync("@tauri-apps/cli")?.version?.split(".")[0] ?? 2 ); async function initTauri() { const confirmed = await confirm( "Couldn't find a Tauri project in current directory, would you like to initialize a new one?" ); if (!confirmed) process.exit(0); console.log("Initializing Tauri..."); const pkgName = getPackageJson().name; await TauriCli.run( [ "init", "--app-name", pkgName ?? "tauri-app", "--window-title", (pkgName ?? "tauri-app") + " window", tauriVersion === 1 ? "--dist-dir" : "--frontend-dist", `Injected by vite-plugin-tauri, you can change this if you want to use tauri cli directly`, tauriVersion === 1 ? "--dev-path" : "--dev-url", `Injected: by vite-plugin-tauri, you can change this if you want to use tauri cli directly` ], "vite-tauri" ); console.log("Tauri initialized."); } function parseTauriArgs(args) { const lastDoubleDash = args.lastIndexOf("--"); if (lastDoubleDash !== -1) { const tauriArg = args.indexOf("-t", lastDoubleDash) ?? args.indexOf("--tauri", lastDoubleDash); const tauriArgs = tauriArg !== -1 ? args.slice(tauriArg + 1) : null; return tauriArgs; } return null; } function tauri(_config) { let viteConfig; return [ { name: "vite-plugin-tauri:serve", apply: "serve", enforce: "post", configResolved(config) { viteConfig = config; }, async configureServer(server) { if (!getTauriConfPath()) await initTauri(); server.httpServer?.once("listening", () => { const localhosts = [ "localhost", "127.0.0.1", "::1", "0000:0000:0000:0000:0000:0000:0000:0001" ]; const address = server.httpServer?.address(); if (!address || typeof address === "string") { console.error("Unexpected dev server address", address); process.exit(1); } const protocol = server.config.server.https ? "https" : "http"; const host = localhosts.includes(address.address) ? "localhost" : address.address; const port = address.port; let args = parseTauriArgs(process.argv) ?? []; if (!args.includes("dev") && !args.includes("build")) { args = ["dev", ...args]; } args = [ ...args, "--config", JSON.stringify({ build: { [tauriVersion === 1 ? "devPath" : "devUrl"]: `${protocol}://${host}:${port}` } }) ]; TauriCli.run(args, "vite-plugin-tauri"); }); } }, { name: "vite-plugin-tauri:build", apply: "build", enforce: "post", configResolved(config) { viteConfig = config; }, async closeBundle() { let tauriConfPath = getTauriConfPath(); if (!tauriConfPath) { await initTauri(); tauriConfPath = getTauriConfPath(); } let args = parseTauriArgs(process.argv) ?? []; if (!args.includes("dev") && !args.includes("build")) { args = ["build", ...args]; } args = [ ...args, "--config", JSON.stringify({ build: { // at this point, `tauriConfPath` can't be null [tauriVersion === 1 ? "distDir" : "frontendDist"]: path.relative( dirname(tauriConfPath), path.resolve(viteConfig.build.outDir) ) } }) ]; await TauriCli.run(args, "vite-plugin-tauri"); } } ]; } var index_default = tauri; export { index_default as default, tauri };