import tailwindcss from '@tailwindcss/vite'; import react from '@vitejs/plugin-react'; import { execSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { defineConfig, type Plugin } from 'vite'; const here = path.dirname(fileURLToPath(import.meta.url)); /** * Oznaceni buildu. * * Bez nej se nasazena verze nepozna: aplikace bezi, jen je stara, a z venku * to vypada stejne jako kdyz je nova. Prave to nas jednou stalo pul hodiny * hadani, proc je na produkci porad stary vzhled. * * Cas buildu je hlavni udaj, protoze funguje vzdycky. Commit je bonus: do * image se `.git` nekopiruje (`.dockerignore`), takze v produkcnim buildu * chybi a zustane prazdny. */ const pkg: { version: string } = JSON.parse( fs.readFileSync(path.resolve(here, 'package.json'), 'utf8'), ); function gitCommit(): string { try { return execSync('git rev-parse --short HEAD', { stdio: ['ignore', 'pipe', 'ignore'] }) .toString() .trim(); } catch { // V containeru `.git` neni. Neni to chyba, jen se commit nedozvime. return ''; } } const buildInfo = { version: pkg.version, builtAt: new Date().toISOString(), commit: gitCommit(), }; /** * Vlozi oznaceni buildu i do hlavicky stranky. * * V JS balicku uz je, ale ten se musi stahnout a rozbalit. Meta znacka je * videt na jeden `curl` bez prihlaseni - a presne to clovek potrebuje, kdyz * zjistuje, co vlastne bezi na produkci. */ function buildStamp(): Plugin { return { name: 'build-stamp', transformIndexHtml() { return [ { tag: 'meta', attrs: { name: 'app-build', content: [buildInfo.version, buildInfo.builtAt, buildInfo.commit] .filter(Boolean) .join(' '), }, injectTo: 'head', }, ]; }, }; } export default defineConfig({ root: path.resolve(here, 'web'), /** * Relativni base. Aplikace bezi za reverse proxy na /apps/, ktery * neni znamy pri buildu. Server proto do index.html vklada * podle ROOT_PATH a relativni odkazy se podle nej slozi spravne. */ base: './', plugins: [react(), tailwindcss(), buildStamp()], define: { __BUILD_INFO__: JSON.stringify(buildInfo), }, resolve: { // Musi zustat v souladu s "paths" ve web/tsconfig.json alias: { '@': path.resolve(here, 'web/src'), }, }, server: { port: 5173, proxy: { // Lokalni vyvoj: web na 5173 vola API na 3000, bez reseni CORS. '/api': { target: 'http://localhost:3000', changeOrigin: true }, '/webhook': { target: 'http://localhost:3000', changeOrigin: true }, }, }, build: { // Server tuhle slozku obsluhuje jako statiku, viz src/index.ts. outDir: path.resolve(here, 'dist/public'), emptyOutDir: true, sourcemap: true, }, });