import { existsSync } from 'node:fs' import { createRequire } from 'node:module' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import tailwindcss from '@tailwindcss/vite' import vue from '@vitejs/plugin-vue' import { defineConfig } from 'vite' // In development the client talks to a homeserver through Vite's proxy, the // same paths warp-web proxies in production, so the code never needs to // know where the homeserver is. WARP_WEB_HOMESERVER points it elsewhere. const homeserver = process.env.WARP_WEB_HOMESERVER ?? 'http://localhost:8008' // The Rust crypto loads its .wasm relative to its own module // (new URL(..., import.meta.url)). Vite's dependency pre-bundling moves the // module into node_modules/.vite/deps without the .wasm, so the dev server // answered with index.html and initRustCrypto failed. The dev server serves // the package as it is instead. It is only matrix-js-sdk's dependency, which // pnpm doesn't make reachable from the pre-bundled matrix-js-sdk, so it is // aliased to the very copy matrix-js-sdk resolves. A production build is // bundled by Rollup and needs neither. const cryptoWasm = '@matrix-org/matrix-sdk-crypto-wasm' function cryptoWasmDir(): string { const sdk = createRequire(import.meta.url).resolve('matrix-js-sdk') let dir = dirname(createRequire(sdk).resolve(cryptoWasm)) while (!existsSync(join(dir, 'package.json'))) dir = dirname(dir) return dir } export default defineConfig(({ command }) => ({ plugins: [vue(), tailwindcss()], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), ...(command === 'serve' ? { [cryptoWasm]: cryptoWasmDir() } : {}), }, }, optimizeDeps: { exclude: [cryptoWasm], }, build: { // Embedded into the warp-web binary by client/ (and the desktop app). outDir: 'client/build/dist', emptyOutDir: true, assetsInlineLimit: 0, // matrix-js-sdk is most of the bundle; it is one chunk on purpose. chunkSizeWarningLimit: 1500, }, server: { port: 5175, proxy: { '/_matrix/client': { target: homeserver, changeOrigin: true }, '/_matrix/media': { target: homeserver, changeOrigin: true }, // Where the client finds the group call backend (rtc_foci). '/.well-known/matrix/client': { target: homeserver, changeOrigin: true }, }, }, }))