vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import path from "path"
  2. import { execSync } from "node:child_process"
  3. import react from "@vitejs/plugin-react"
  4. // Sello de build visible en el sidebar: diagnostica al instante qué
  5. // código está ejecutando el navegador de cada usuario (#1214: "no veo
  6. // los cambios" era el service worker sirviendo el app shell viejo; con
  7. // el sello, una captura del usuario dice exactamente qué build ve).
  8. let npBuild = "dev"
  9. try {
  10. npBuild = execSync("git rev-parse --short=8 HEAD", { encoding: "utf8" }).trim()
  11. } catch {
  12. /* fuera de repo: deja "dev" */
  13. }
  14. import { defineConfig, type Plugin } from "vite"
  15. import { VitePWA } from "vite-plugin-pwa"
  16. // Tracker GoatCounter SOLO en el build de la demo pública (issue #240):
  17. // VITE_GC_COUNT=https://stats.netpulse.cloudless.club npm run build
  18. // Los builds normales NO lo llevan: una instalación self-hosted nunca debe
  19. // llamar a casa. Los hits se registran con prefijo /demo en el mismo site
  20. // que la landing ("/" = landing, "/demo/..." = demo).
  21. const gcCount = process.env.VITE_GC_COUNT?.replace(/\/$/, "")
  22. function goatcounterPlugin(): Plugin {
  23. return {
  24. name: "netpulse-goatcounter",
  25. transformIndexHtml(html) {
  26. if (!gcCount) return html
  27. const snippet =
  28. ` <script>window.goatcounter={path:function(p){return '/demo'+p}}</script>\n` +
  29. ` <script async data-goatcounter="${gcCount}/count" src="${gcCount}/count.js"></script>\n </head>`
  30. return html.replace("</head>", snippet)
  31. },
  32. }
  33. }
  34. // https://vite.dev/config/
  35. export default defineConfig({
  36. define: { __NP_BUILD__: JSON.stringify(npBuild) },
  37. // Servido desde el backend Hono en la raíz del dominio → rutas absolutas.
  38. // (Con './' las rutas anidadas tipo /routers/:id rompen los assets al recargar.)
  39. base: '/',
  40. plugins: [
  41. react(),
  42. goatcounterPlugin(),
  43. VitePWA({
  44. // injectManifest: SW propio (src/sw.ts) con handlers Web Push;
  45. // generateSW no permite handlers custom (SPEC-PUSH §2).
  46. strategies: 'injectManifest',
  47. srcDir: 'src',
  48. filename: 'sw.ts',
  49. registerType: 'autoUpdate',
  50. includeAssets: ['logo.svg', 'apple-touch-icon.png', 'icon-192.png', 'icon-512.png'],
  51. manifest: {
  52. name: 'NetPulse',
  53. short_name: 'NetPulse',
  54. description: 'Panel de control de tu red doméstica',
  55. lang: 'es',
  56. display: 'standalone',
  57. theme_color: '#070B12',
  58. background_color: '#070B12',
  59. categories: ['utilities'],
  60. icons: [
  61. { src: 'icon-192.png', sizes: '192x192', type: 'image/png', purpose: 'maskable any' },
  62. { src: 'icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable any' },
  63. ],
  64. },
  65. injectManifest: {
  66. maximumFileSizeToCacheInBytes: 3.5 * 1024 * 1024,
  67. // App-shell precache (mismo glob que con generateSW: 16 entradas);
  68. // el fallback de navegación vive ahora en src/sw.ts (NavigationRoute)
  69. globPatterns: ['**/*.{js,css,html,svg,png,woff2}'],
  70. },
  71. // SW también en dev: permite probar el alta push en local (localhost
  72. // es contexto seguro). La ruta de navegación offline se desactiva en
  73. // dev dentro de src/sw.ts para no congelar el index.html.
  74. devOptions: {
  75. enabled: true,
  76. type: 'module',
  77. },
  78. }),
  79. ],
  80. server: {
  81. // El backend Hono usa el puerto 3000 (contrato API); Vite dev va en 5173
  82. port: 5173,
  83. proxy: {
  84. '/api': {
  85. target: 'http://localhost:3000',
  86. changeOrigin: true,
  87. },
  88. },
  89. },
  90. resolve: {
  91. alias: {
  92. "@": path.resolve(__dirname, "./src"),
  93. },
  94. },
  95. });