web


git clone https://radroots.dev/git/web.git
Log | Files | Refs | Submodules | README | LICENSE

vite.config.ts (2799B)


      1 import { sveltekit } from "@sveltejs/kit/vite";
      2 import tailwindcss from "@tailwindcss/vite";
      3 import { config as dotenv_config } from "dotenv";
      4 import { execSync } from "node:child_process";
      5 import { readFileSync } from "node:fs";
      6 import path from "node:path";
      7 import { defineConfig } from "vite";
      8 
      9 export default defineConfig(({ mode }) => {
     10 	const web_repo_root = path.resolve(__dirname, "..");
     11 	const monorepo_root = path.resolve(web_repo_root, "../../../..");
     12 	const first_party_wasm_pkg_roots = [
     13 		"events_codec_wasm",
     14 		"replica_db_wasm",
     15 		"replica_sync_wasm",
     16 	].map((crate) =>
     17 		path.resolve(monorepo_root, `domains/radroots/lib/crates/${crate}/pkg`)
     18 	);
     19 	const web_app_env_file = process.env.RADROOTS_WEB_APP_ENV_FILE;
     20 	if (!web_app_env_file) throw new Error("Missing env var: RADROOTS_WEB_APP_ENV_FILE");
     21 	dotenv_config({ path: path.resolve(web_app_env_file), override: true });
     22 	const app_package_path = path.resolve(__dirname, "package.json");
     23 	const app_package = JSON.parse(readFileSync(app_package_path, "utf8")) as { name?: string; version?: string };
     24 	const git_hash = (() => {
     25 		try {
     26 			return execSync("git rev-parse HEAD", { cwd: web_repo_root }).toString().trim();
     27 		} catch {
     28 			return "unknown";
     29 		}
     30 	})();
     31 	const required_env = (key: string): string => {
     32 		const value = process.env[key];
     33 		if (!value) throw new Error(`Missing env var: ${key}`);
     34 		return value;
     35 	};
     36 	const required_package_string = (key: "name" | "version"): string => {
     37 		const value = app_package[key];
     38 		if (!value) throw new Error(`app package ${key} must be defined`);
     39 		return value;
     40 	};
     41 	const web_env_keys = [
     42 		"RADROOTS_WEB_APP_ACCENT",
     43 		"RADROOTS_WEB_APP_DESCRIPTION",
     44 		"RADROOTS_WEB_APP_NAME",
     45 		"RADROOTS_WEB_API_BASE_URL",
     46 		"RADROOTS_WEB_DEFAULT_RELAY_URLS",
     47 		"RADROOTS_WEB_GEOCODER_DB_URL",
     48 		"RADROOTS_WEB_KEYVAL_NAME",
     49 		"RADROOTS_WEB_MEDIA_BASE_URL",
     50 		"RADROOTS_WEB_NOSTR_CLIENT",
     51 		"RADROOTS_WEB_RELAY_URL",
     52 		"RADROOTS_WEB_SQL_WASM_URL",
     53 	] as const;
     54 	const web_env_define = Object.fromEntries(
     55 		web_env_keys.map((key) => [`import.meta.env.${key}`, JSON.stringify(required_env(key))])
     56 	);
     57 	return {
     58 		build: {
     59 			sourcemap: true
     60 		},
     61 		envPrefix: "RADROOTS_WEB_",
     62 		plugins: [
     63 			tailwindcss(),
     64 			sveltekit(),
     65 		],
     66 		define: {
     67 			'process.env.NODE_ENV': `"${mode}"`,
     68 			'__APP_GIT_HASH__': JSON.stringify(git_hash),
     69 			'__APP_NAME__': JSON.stringify(`radroots/${required_package_string("name")}`),
     70 			'__APP_VERSION__': JSON.stringify(required_package_string("version")),
     71 			...web_env_define,
     72 		},
     73 		server: {
     74 			host: required_env("RADROOTS_WEB_APP_DEV_HOST"),
     75 			port: Number(required_env("RADROOTS_WEB_APP_DEV_PORT")),
     76 			fs: {
     77 				allow: [
     78 					path.resolve(__dirname, ".."),
     79 					path.resolve(__dirname, "../.."),
     80 					...first_party_wasm_pkg_roots
     81 				]
     82 			}
     83 		}
     84 	};
     85 });