|
| 1 | +import react from "@vitejs/plugin-react"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import { visualizer } from "rollup-plugin-visualizer"; |
| 4 | +import { defineConfig, type PluginOption } from "vite"; |
| 5 | +import checker from "vite-plugin-checker"; |
| 6 | + |
| 7 | +const plugins: PluginOption[] = [ |
| 8 | + react(), |
| 9 | + checker({ |
| 10 | + typescript: true, |
| 11 | + }), |
| 12 | +]; |
| 13 | + |
| 14 | +if (process.env.STATS !== undefined) { |
| 15 | + plugins.push( |
| 16 | + visualizer({ |
| 17 | + filename: "./stats/index.html", |
| 18 | + }), |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +export default defineConfig({ |
| 23 | + plugins: plugins, |
| 24 | + publicDir: path.resolve(__dirname, "./static"), |
| 25 | + build: { |
| 26 | + outDir: path.resolve(__dirname, "./out"), |
| 27 | + // We need to keep the /bin folder and GITKEEP files |
| 28 | + emptyOutDir: false, |
| 29 | + // 'hidden' works like true except that the corresponding sourcemap comments in the bundled files are suppressed |
| 30 | + sourcemap: "hidden", |
| 31 | + }, |
| 32 | + define: { |
| 33 | + "process.env": { |
| 34 | + NODE_ENV: process.env.NODE_ENV, |
| 35 | + STORYBOOK: process.env.STORYBOOK, |
| 36 | + INSPECT_XSTATE: process.env.INSPECT_XSTATE, |
| 37 | + }, |
| 38 | + }, |
| 39 | + server: { |
| 40 | + host: "127.0.0.1", |
| 41 | + port: process.env.PORT ? Number(process.env.PORT) : 8080, |
| 42 | + headers: { |
| 43 | + // This header corresponds to "src/api/api.ts"'s hardcoded FE token. |
| 44 | + // This is the secret side of the CSRF double cookie submit method. |
| 45 | + // This should be sent on **every** response from the webserver. |
| 46 | + // |
| 47 | + // This is required because in production, the Golang webserver generates |
| 48 | + // this "Set-Cookie" header. The Vite webserver needs to replicate this |
| 49 | + // behavior. Instead of implementing CSRF though, we just use static |
| 50 | + // values for simplicity. |
| 51 | + "Set-Cookie": |
| 52 | + "csrf_token=JXm9hOUdZctWt0ZZGAy9xiS/gxMKYOThdxjjMnMUyn4=; Path=/; HttpOnly; SameSite=Lax", |
| 53 | + }, |
| 54 | + proxy: { |
| 55 | + "/api": { |
| 56 | + ws: true, |
| 57 | + changeOrigin: true, |
| 58 | + target: process.env.CODER_HOST || "http://localhost:3000", |
| 59 | + secure: process.env.NODE_ENV === "production", |
| 60 | + configure: (proxy) => { |
| 61 | + // Vite does not catch socket errors, and stops the webserver. |
| 62 | + // As /logs endpoint can return HTTP 4xx status, we need to embrace |
| 63 | + // Vite with a custom error handler to prevent from quitting. |
| 64 | + proxy.on("proxyReqWs", (proxyReq, req, socket) => { |
| 65 | + if (process.env.NODE_ENV === "development") { |
| 66 | + proxyReq.setHeader( |
| 67 | + "origin", |
| 68 | + process.env.CODER_HOST || "http://localhost:3000", |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + socket.on("error", (error) => { |
| 73 | + console.error(error); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }, |
| 77 | + }, |
| 78 | + "/swagger": { |
| 79 | + target: process.env.CODER_HOST || "http://localhost:3000", |
| 80 | + secure: process.env.NODE_ENV === "production", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + resolve: { |
| 85 | + alias: { |
| 86 | + api: path.resolve(__dirname, "./src/api"), |
| 87 | + components: path.resolve(__dirname, "./src/components"), |
| 88 | + contexts: path.resolve(__dirname, "./src/contexts"), |
| 89 | + hooks: path.resolve(__dirname, "./src/hooks"), |
| 90 | + modules: path.resolve(__dirname, "./src/modules"), |
| 91 | + pages: path.resolve(__dirname, "./src/pages"), |
| 92 | + testHelpers: path.resolve(__dirname, "./src/testHelpers"), |
| 93 | + theme: path.resolve(__dirname, "./src/theme"), |
| 94 | + utils: path.resolve(__dirname, "./src/utils"), |
| 95 | + }, |
| 96 | + }, |
| 97 | +}); |
0 commit comments