Files
superlist/apps/web/vite.config.ts
Jose Selesan d5feb65d62 feat(web): show the build's commit and date in settings
Lets you tell whether an installed PWA picked up a new release. The
commit is read from .git at build time (the Docker build stage has no
git binary), so .dockerignore now lets HEAD and the refs through.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 20:00:33 -03:00

109 lines
3.8 KiB
TypeScript

import { readFileSync } from 'node:fs'
import path from 'node:path'
import tailwindcss from '@tailwindcss/vite'
import { tanstackRouter } from '@tanstack/router-plugin/vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
import { VitePWA } from 'vite-plugin-pwa'
const srcDir = path.resolve(import.meta.dirname, './src')
const gitDir = path.resolve(import.meta.dirname, '../../.git')
/**
* Short hash of the commit being built, read straight from `.git` because the
* Docker build stage has no `git` binary (`.dockerignore` lets `HEAD` and the
* refs through for this). Falls back to `dev` when there is no repository.
*/
function readCommit(): string {
try {
const head = readFileSync(path.join(gitDir, 'HEAD'), 'utf8').trim()
if (!head.startsWith('ref: ')) return head.slice(0, 7)
const ref = head.slice('ref: '.length)
try {
return readFileSync(path.join(gitDir, ref), 'utf8').trim().slice(0, 7)
} catch {
// The ref may only exist in `packed-refs` (fresh clones, after `git gc`).
const packed = readFileSync(path.join(gitDir, 'packed-refs'), 'utf8')
const line = packed.split('\n').find((entry) => entry.endsWith(` ${ref}`))
if (line) return line.slice(0, 7)
}
} catch {
// No repository (or a worktree, where `.git` is a file): not worth failing the build.
}
return 'dev'
}
export default defineConfig({
define: {
__APP_COMMIT__: JSON.stringify(readCommit()),
__APP_BUILT_AT__: JSON.stringify(new Date().toISOString()),
},
plugins: [
// Must run before react(): it watches `src/routes` and generates `routeTree.gen.ts`.
tanstackRouter({ target: 'react', autoCodeSplitting: true }),
react(),
tailwindcss(),
VitePWA({
// `autoUpdate`: a new service worker activates as soon as it is installed
// and the page reloads into the new release. Registration (and the
// periodic update checks) live in `src/lib/register-sw.ts`.
registerType: 'autoUpdate',
injectRegister: false,
includeAssets: ['favicon.svg', 'apple-touch-icon.png'],
manifest: {
name: 'Superlist — Listas de compras compartidas',
short_name: 'Superlist',
description: 'Listas de compras compartidas con las personas de tu hogar.',
lang: 'es',
start_url: '/',
scope: '/',
display: 'standalone',
orientation: 'portrait',
theme_color: '#f9f8f5',
background_color: '#f9f8f5',
icons: [
{ src: 'pwa-192x192.png', sizes: '192x192', type: 'image/png' },
{ src: 'pwa-512x512.png', sizes: '512x512', type: 'image/png' },
{
src: 'maskable-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable',
},
],
},
workbox: {
// Pre-cache the app shell (JS/CSS/HTML + self-hosted Geist fonts).
globPatterns: ['**/*.{js,css,html,svg,png,ico,woff2}'],
// SPA fallback so deep links work offline, but never swallow API calls.
navigateFallback: 'index.html',
navigateFallbackDenylist: [/^\/api\//],
// Drop caches left behind by previous releases once the new one activates.
cleanupOutdatedCaches: true,
// Take control of every open tab as soon as the new worker activates.
clientsClaim: true,
skipWaiting: true,
},
devOptions: {
// The service worker is only built for `vite build`; verify with `preview`.
enabled: false,
},
}),
],
resolve: {
alias: {
'@': srcDir,
},
},
server: {
port: 7153,
// Same-origin API in dev, mirroring nginx in production: the browser calls
// `/api/*` on the Vite server and it is forwarded to the Bun API.
proxy: {
'/api': 'http://localhost:4100',
},
},
})