49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
import { defineConfig, loadEnv } from "vite"
|
|
import react from "@vitejs/plugin-react"
|
|
import tailwindcss from "@tailwindcss/vite"
|
|
import { fileURLToPath, URL } from "node:url"
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
// En local, on lit .env à la racine ; dans Compose, env_file injecte
|
|
// directement ces variables au démarrage du conteneur frontend.
|
|
const rootEnv = loadEnv(mode, fileURLToPath(new URL("../", import.meta.url)), "")
|
|
const backendPort = process.env.BACKEND_PORT || rootEnv.BACKEND_PORT
|
|
const frontendPort = process.env.FRONTEND_PORT || rootEnv.FRONTEND_PORT
|
|
const backendHost = process.env.BACKEND_PROXY_HOST || "127.0.0.1"
|
|
|
|
// Aucun backend n'est nécessaire pendant le build Vite (Docker).
|
|
// Le proxy est créé au lancement de Vite preview / dev.
|
|
const proxy = backendPort
|
|
? Object.fromEntries(
|
|
["/api", "/static", "/media"].map((path) => [
|
|
path,
|
|
{
|
|
target: `http://${backendHost}:${backendPort}`,
|
|
// Conserver le Host public pour ALLOWED_HOSTS et les sessions Django.
|
|
changeOrigin: false,
|
|
},
|
|
]),
|
|
)
|
|
: {}
|
|
|
|
const allowedHosts = (process.env.ALLOWED_HOSTS || rootEnv.ALLOWED_HOSTS || "")
|
|
.split(",")
|
|
.map((host) => host.trim())
|
|
.filter((host) => host && host !== "*")
|
|
|
|
const shared = {
|
|
host: "0.0.0.0",
|
|
strictPort: true,
|
|
...(frontendPort ? { port: Number(frontendPort) } : {}),
|
|
allowedHosts,
|
|
proxy,
|
|
}
|
|
|
|
return {
|
|
plugins: [react(), tailwindcss()],
|
|
resolve: { alias: { "@": fileURLToPath(new URL("./src", import.meta.url)) } },
|
|
server: shared,
|
|
preview: shared,
|
|
}
|
|
})
|