From 759df140ed690a0cd77d695239464e496310a41f Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 24 Nov 2026 12:49:53 +0100 Subject: [PATCH] fixed vite with traefik https localhost --- .../custom/mathallo/mathallo.libraries.yml | 4 +- .../themes/custom/mathallo/vite.config.js | 40 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/web_main/themes/custom/mathallo/mathallo.libraries.yml b/web_main/themes/custom/mathallo/mathallo.libraries.yml index 677e9c5..1a96e59 100644 --- a/web_main/themes/custom/mathallo/mathallo.libraries.yml +++ b/web_main/themes/custom/mathallo/mathallo.libraries.yml @@ -10,11 +10,11 @@ global: vitehmr: js: - http://localhost:5173/@vite/client: + https://localhost:5173/@vite/client: type: external attributes: type: module - http://localhost:5173/assets/js/main.js: + https://localhost:5173/assets/js/main.js: type: external attributes: type: module diff --git a/web_main/themes/custom/mathallo/vite.config.js b/web_main/themes/custom/mathallo/vite.config.js index 2bb2800..f67d97c 100644 --- a/web_main/themes/custom/mathallo/vite.config.js +++ b/web_main/themes/custom/mathallo/vite.config.js @@ -1,14 +1,52 @@ import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { fileURLToPath, URL } from 'node:url' +import { readFileSync } from 'node:fs' + +// Certificat mkcert wildcard du reverse-proxy (SAN: localhost, 127.0.0.1, +// *.localhost, trusté par la CA locale) : permet de servir le dev server +// en HTTPS sans toucher à la config Traefik. +const TLS_CERT = fileURLToPath(new URL('../../../../../../reverse-proxy/certs/wildcard.localhost.pem', import.meta.url)) +const TLS_KEY = fileURLToPath(new URL('../../../../../../reverse-proxy/certs/wildcard.localhost-key.pem', import.meta.url)) // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], + // Chemins relatifs dans les assets buildés : sinon Vite génère des url() + // absolues depuis la racine du site au lieu du dossier du thème, où Drupal + // sert réellement assets/dist/. + base: './', server: { - + // The dev server must be reachable from the browser on the host. + host: 'localhost', + port: 5173, + strictPort: true, + // HTTPS obligatoire depuis la migration Traefik : la page Drupal est + // servie en https://mathallo.fr.localhost, un dev server en + // http://localhost:5173 serait bloqué en mixed content (assets + WebSocket + // HMR). On réutilise le certificat mkcert de reverse-proxy (SAN + // localhost/127.0.0.1) : aucun changement de config Traefik nécessaire. + https: { + cert: readFileSync(TLS_CERT), + key: readFileSync(TLS_KEY), + }, + // Vite 5.4 crée un serveur HTTP/2 (createSecureServer) dès que `https` + // est défini, sauf si `proxy` est présent — et HTTP/2 ne supporte pas + // l'upgrade WebSocket : le HMR (wss://localhost:5173) serait refusé + // (404 / close 1006). Un `proxy: {}` vide force le serveur HTTPS en + // HTTP/1.1 (node:https) sans changer le comportement des requêtes. + proxy: {}, + // Depuis Vite 5.4.12 (CVE-2025-24010) CORS est désactivé par défaut. + // Autorise explicitement les origines qui servent les pages Drupal. + cors: { + origin: [/^https:\/\/mathallo\.fr\.localhost(:\d+)?$/, /^https:\/\/dev\.mathallo\.fr(:\d+)?$/], + }, + // Absolute base URL used by Vite to build asset/HMR URLs. + origin: 'https://localhost:5173', hmr: { host: 'localhost', + protocol: 'wss', + clientPort: 5173, }, watch: { usePolling: true,