import { defineConfig } from "vite";
import { visualizer } from "rollup-plugin-visualizer";
import vue from "@vitejs/plugin-vue";
// import pkg from "./package.json";
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const pkg = require("./package.json");

const subpath = "/bugreport/";
export const baseUrl = "/"; //subpath + "frontend/dist/";

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
	const isBuild = command === "build";
	const isDev = command === "serve";

	return {
		base: isBuild ? baseUrl : "/",
		plugins: [vue()],
		define: {
			__APP_VERSION__: JSON.stringify(pkg.version),
			__BUILD_DATE__: JSON.stringify(new Date().toISOString()),
			__SUBPATH__: JSON.stringify(isBuild ? subpath : "/"),
			__IS_BUILD__: JSON.stringify(isBuild),
			__IS_DEV__: JSON.stringify(isDev),
		},
		server: {
			host: true,
			port: 5173
		},
		build: {
			outDir: "dist",
			chunkSizeWarningLimit: 1000, // zvýšenie limitu na 1000 kB
			rollupOptions: {
				plugins: [
					visualizer({
						open: false, // otvorí report v prehliadači po builde
						filename: "stats.html",
						gzipSize: true,
						brotliSize: true,
					}),
				],
				output: {
					manualChunks: {
						fontawesome: [
							"@fortawesome/free-solid-svg-icons",
							"@fortawesome/fontawesome-svg-core",
						],
					},
				},
			},
		},
	};
});
