import { readdir, readFile, writeFile } from "node:fs/promises" import path from "node:path" import { gzipSync } from "node:zlib" import tailwindcss from "@tailwindcss/vite" import { tanstackRouter } from "@tanstack/router-plugin/vite" import react from "@vitejs/plugin-react-swc" import { defineConfig, type Plugin } from "vite" /** Compress once at build time, including text copied from public/. */ function precompress(): Plugin { async function compress(directory: string): Promise { const entries = await readdir(directory, { withFileTypes: true }) await Promise.all( entries.map(async (entry) => { const file = path.join(directory, entry.name) if (entry.isDirectory()) return compress(file) if (!/\.(?:js|css|html|svg|json|webmanifest)$/.test(entry.name)) return const original = await readFile(file) if (original.length < 1024) return const compressed = gzipSync(original, { level: 9 }) if (compressed.length < original.length) await writeFile(`${file}.gz`, compressed) }), ) } return { name: "precompressed-assets", apply: "build", async writeBundle({ dir }) { if (dir) await compress(dir) }, } } // https://vitejs.dev/config/ export default defineConfig({ // A portal serves this bundle under a path of its own and caches one copy // for every instance, so asset URLs have to be absolute under that // prefix. Unset — every ordinary build — this stays "/". base: process.env.VITE_BASE || "/", resolve: { alias: { "@": path.resolve(__dirname, "./src"), }, }, build: { // The entry holds React, the router, the query client and the shell, and // cannot get below this. A bigger chunk than that is worth a warning. chunkSizeWarningLimit: 850, rollupOptions: { onwarn(warning, defaultHandler) { // Zod's own comments name `@__PURE__`, which Rollup reads as a // misplaced annotation. Nothing to fix on our side. if ( warning.code === "INVALID_ANNOTATION" && warning.id?.includes("/node_modules/zod/") ) return defaultHandler(warning) }, }, }, plugins: [ tanstackRouter({ target: "react", autoCodeSplitting: true, }), react(), tailwindcss(), precompress(), ], })